Skip to content

Commit 80c32fb

Browse files
JordonPhillipssrchase
authored andcommitted
Add expectations for default member deser (smithy-lang#365)
This adds expectations for the default member deserialization so that an error will be thrown if the type being deserialized isn't correct.
1 parent b2ce2b8 commit 80c32fb

File tree

4 files changed

+53
-23
lines changed

4 files changed

+53
-23
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/DocumentMemberDeserVisitor.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,46 +124,48 @@ public String blobShape(BlobShape shape) {
124124

125125
@Override
126126
public String booleanShape(BooleanShape shape) {
127-
return deserializeUnmodified();
127+
context.getWriter().addImport("expectBoolean", "__expectBoolean", "@aws-sdk/smithy-client");
128+
return "__expectBoolean(" + dataSource + ")";
128129
}
129130

130131
@Override
131132
public String byteShape(ByteShape shape) {
132-
return deserializeUnmodified();
133+
return expectNumber();
133134
}
134135

135136
@Override
136137
public String shortShape(ShortShape shape) {
137-
return deserializeUnmodified();
138+
return expectNumber();
138139
}
139140

140141
@Override
141142
public String integerShape(IntegerShape shape) {
142-
return deserializeUnmodified();
143+
return expectNumber();
143144
}
144145

145146
@Override
146147
public String longShape(LongShape shape) {
147-
return deserializeUnmodified();
148+
return expectNumber();
148149
}
149150

150151
@Override
151152
public String floatShape(FloatShape shape) {
152-
return deserializeUnmodified();
153+
return expectNumber();
153154
}
154155

155156
@Override
156157
public String doubleShape(DoubleShape shape) {
157-
return deserializeUnmodified();
158+
return expectNumber();
158159
}
159160

160-
@Override
161-
public String stringShape(StringShape shape) {
162-
return HttpProtocolGeneratorUtils.getStringOutputParam(context, shape, deserializeUnmodified());
161+
private String expectNumber() {
162+
context.getWriter().addImport("expectNumber", "__expectNumber", "@aws-sdk/smithy-client");
163+
return "__expectNumber(" + dataSource + ")";
163164
}
164165

165-
private String deserializeUnmodified() {
166-
return dataSource;
166+
@Override
167+
public String stringShape(StringShape shape) {
168+
return HttpProtocolGeneratorUtils.getStringOutputParam(context, shape, dataSource);
167169
}
168170

169171
@Override

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2470,7 +2470,12 @@ private String getStringOutputParam(
24702470
dataSource = "Buffer.from(" + dataSource + ", 'base64').toString('ascii')";
24712471
}
24722472

2473-
return HttpProtocolGeneratorUtils.getStringOutputParam(context, target, dataSource);
2473+
return HttpProtocolGeneratorUtils.getStringOutputParam(
2474+
context, target, dataSource, !isGuaranteedString(bindingType));
2475+
}
2476+
2477+
private boolean isGuaranteedString(Location bindingType) {
2478+
return bindingType != Location.PAYLOAD && bindingType != Location.DOCUMENT;
24742479
}
24752480

24762481
/**

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpProtocolGeneratorUtils.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,11 @@ static String getStringInputParam(GenerationContext context, Shape shape, String
158158
* @param shape The shape that represents the value being received.
159159
* @param dataSource The in-code location of the data to provide an output of
160160
* ({@code output.foo}, {@code entry}, etc.)
161+
* @param useExpect Whether or not to wrap the string in expectString. This should
162+
* only be false if the value is guaranteed to be a string already.
161163
* @return Returns a value or expression of the output string.
162164
*/
163-
static String getStringOutputParam(GenerationContext context, Shape shape, String dataSource) {
165+
static String getStringOutputParam(GenerationContext context, Shape shape, String dataSource, boolean useExpect) {
164166
// Handle media type generation, defaulting to a standard String.
165167
Optional<MediaTypeTrait> mediaTypeTrait = shape.getTrait(MediaTypeTrait.class);
166168
if (mediaTypeTrait.isPresent()) {
@@ -173,7 +175,28 @@ static String getStringOutputParam(GenerationContext context, Shape shape, Strin
173175
LOGGER.warning(() -> "Found unsupported mediatype " + mediaType + " on String shape: " + shape);
174176
}
175177
}
176-
return dataSource;
178+
179+
if (!useExpect) {
180+
return dataSource;
181+
}
182+
context.getWriter().addImport("expectString", "__expectString", "@aws-sdk/smithy-client");
183+
return "__expectString(" + dataSource + ")";
184+
}
185+
186+
/**
187+
* Given a String output, determine its media type and generate an output value
188+
* provider for it.
189+
*
190+
* <p>This currently only supports using the LazyJsonString for {@code "application/json"}.
191+
*
192+
* @param context The generation context.
193+
* @param shape The shape that represents the value being received.
194+
* @param dataSource The in-code location of the data to provide an output of
195+
* ({@code output.foo}, {@code entry}, etc.)
196+
* @return Returns a value or expression of the output string.
197+
*/
198+
static String getStringOutputParam(GenerationContext context, Shape shape, String dataSource) {
199+
return getStringOutputParam(context, shape, dataSource, true);
177200
}
178201

179202
/**

smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/DocumentMemberDeserVisitorTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ public static Collection<Object[]> validMemberTargetTypes() {
6868
+ "(" + DATA_SOURCE + ", context)";
6969

7070
return ListUtils.of(new Object[][]{
71-
{BooleanShape.builder().id(id).build(), DATA_SOURCE},
72-
{ByteShape.builder().id(id).build(), DATA_SOURCE},
73-
{DoubleShape.builder().id(id).build(), DATA_SOURCE},
74-
{FloatShape.builder().id(id).build(), DATA_SOURCE},
75-
{IntegerShape.builder().id(id).build(), DATA_SOURCE},
76-
{LongShape.builder().id(id).build(), DATA_SOURCE},
77-
{ShortShape.builder().id(id).build(), DATA_SOURCE},
78-
{StringShape.builder().id(id).build(), DATA_SOURCE},
71+
{BooleanShape.builder().id(id).build(), "__expectBoolean(" + DATA_SOURCE + ")"},
72+
{ByteShape.builder().id(id).build(), "__expectNumber(" + DATA_SOURCE + ")"},
73+
{DoubleShape.builder().id(id).build(), "__expectNumber(" + DATA_SOURCE + ")"},
74+
{FloatShape.builder().id(id).build(), "__expectNumber(" + DATA_SOURCE + ")"},
75+
{IntegerShape.builder().id(id).build(), "__expectNumber(" + DATA_SOURCE + ")"},
76+
{LongShape.builder().id(id).build(), "__expectNumber(" + DATA_SOURCE + ")"},
77+
{ShortShape.builder().id(id).build(), "__expectNumber(" + DATA_SOURCE + ")"},
78+
{StringShape.builder().id(id).build(), "__expectString(" + DATA_SOURCE + ")"},
7979
{
8080
StringShape.builder().id(id).addTrait(new MediaTypeTrait("foo+json")).build(),
8181
"new __LazyJsonString(" + DATA_SOURCE + ")"

0 commit comments

Comments
 (0)