Skip to content

Commit adf914b

Browse files
Update float/int parsing and expectations (#397)
This updates number parsing in http bindings to be more strict. It also updates the expectation methods used for numbers to be more specific.
1 parent 441ae1f commit adf914b

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,42 +130,42 @@ public String booleanShape(BooleanShape shape) {
130130

131131
@Override
132132
public String byteShape(ByteShape shape) {
133-
return expectNumber();
133+
return expectInteger();
134134
}
135135

136136
@Override
137137
public String shortShape(ShortShape shape) {
138-
return expectNumber();
138+
return expectInteger();
139139
}
140140

141141
@Override
142142
public String integerShape(IntegerShape shape) {
143-
return expectNumber();
143+
return expectInteger();
144144
}
145145

146146
@Override
147147
public String longShape(LongShape shape) {
148-
return expectNumber();
148+
return expectInteger();
149149
}
150150

151151
@Override
152152
public String floatShape(FloatShape shape) {
153-
return handleFloat();
153+
return limitedParseFloat();
154154
}
155155

156156
@Override
157157
public String doubleShape(DoubleShape shape) {
158-
return handleFloat();
158+
return limitedParseFloat();
159159
}
160160

161-
private String expectNumber() {
162-
context.getWriter().addImport("expectNumber", "__expectNumber", "@aws-sdk/smithy-client");
163-
return "__expectNumber(" + dataSource + ")";
161+
private String expectInteger() {
162+
context.getWriter().addImport("expectInt", "__expectInt", "@aws-sdk/smithy-client");
163+
return "__expectInt(" + dataSource + ")";
164164
}
165165

166-
private String handleFloat() {
167-
context.getWriter().addImport("handleFloat", "__handleFloat", "@aws-sdk/smithy-client");
168-
return "__handleFloat(" + dataSource + ")";
166+
private String limitedParseFloat() {
167+
context.getWriter().addImport("limitedParseFloat", "__limitedParseFloat", "@aws-sdk/smithy-client");
168+
return "__limitedParseFloat(" + dataSource + ")";
169169
}
170170

171171
@Override

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ private String getCollectionInputParam(
11351135
dataSource = "Array.from(" + dataSource + ".values())";
11361136
}
11371137
String collectionTargetValue = getInputValue(context, bindingType, "_entry", targetMember, collectionTarget);
1138-
String iteratedParam = "(" + dataSource + " || []).map(_entry => " + collectionTargetValue + ")";
1138+
String iteratedParam = "(" + dataSource + " || []).map(_entry => " + collectionTargetValue + " as any)";
11391139
switch (bindingType) {
11401140
case HEADER:
11411141
return iteratedParam + ".join(', ')";
@@ -2565,7 +2565,7 @@ private String getOutputValue(
25652565
Shape target
25662566
) {
25672567
if (target instanceof NumberShape) {
2568-
return getNumberOutputParam(bindingType, dataSource, target);
2568+
return getNumberOutputParam(context, bindingType, dataSource, target);
25692569
} else if (target instanceof BooleanShape) {
25702570
return getBooleanOutputParam(context, bindingType, dataSource);
25712571
} else if (target instanceof StringShape) {
@@ -2691,15 +2691,15 @@ private String getCollectionOutputParam(
26912691
switch (bindingType) {
26922692
case QUERY_PARAMS:
26932693
case QUERY:
2694-
return String.format("%1$s.map(_entry => %2$s)",
2694+
return String.format("%1$s.map(_entry => %2$s as any)",
26952695
dataSource, collectionTargetValue);
26962696
case LABEL:
26972697
dataSource = "(" + dataSource + " || \"\")";
26982698
// Split these values on slashes.
26992699
outputParam = "" + dataSource + ".split('/')";
27002700

27012701
// Iterate over each entry and do deser work.
2702-
outputParam += ".map(_entry => " + collectionTargetValue + ")";
2702+
outputParam += ".map(_entry => " + collectionTargetValue + " as any)";
27032703

27042704
return outputParam;
27052705
case HEADER:
@@ -2722,7 +2722,7 @@ private String getCollectionOutputParam(
27222722
}
27232723

27242724
// Iterate over each entry and do deser work.
2725-
outputParam += ".map(_entry => " + collectionTargetValue + ")";
2725+
outputParam += ".map(_entry => " + collectionTargetValue + " as any)";
27262726

27272727
return outputParam;
27282728
default:
@@ -2770,15 +2770,22 @@ private String getNamedMembersOutputParam(
27702770
* @param target The shape of the value being provided.
27712771
* @return Returns a value or expression of the output number.
27722772
*/
2773-
private String getNumberOutputParam(Location bindingType, String dataSource, Shape target) {
2773+
private String getNumberOutputParam(
2774+
GenerationContext context,
2775+
Location bindingType,
2776+
String dataSource,
2777+
Shape target
2778+
) {
27742779
switch (bindingType) {
27752780
case QUERY:
27762781
case LABEL:
27772782
case HEADER:
27782783
if (target instanceof FloatShape || target instanceof DoubleShape) {
2779-
return "parseFloat(" + dataSource + ")";
2784+
context.getWriter().addImport("strictParseFloat", "__strictParseFloat", "@aws-sdk/smithy-client");
2785+
return "__strictParseFloat(" + dataSource + ")";
27802786
}
2781-
return "parseInt(" + dataSource + ", 10)";
2787+
context.getWriter().addImport("strictParseInt", "__strictParseInt", "@aws-sdk/smithy-client");
2788+
return "__strictParseInt(" + dataSource + ")";
27822789
default:
27832790
throw new CodegenException("Unexpected number binding location `" + bindingType + "`");
27842791
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ public static Collection<Object[]> validMemberTargetTypes() {
6969

7070
return ListUtils.of(new Object[][]{
7171
{BooleanShape.builder().id(id).build(), "__expectBoolean(" + DATA_SOURCE + ")"},
72-
{ByteShape.builder().id(id).build(), "__expectNumber(" + DATA_SOURCE + ")"},
73-
{DoubleShape.builder().id(id).build(), "__handleFloat(" + DATA_SOURCE + ")"},
74-
{FloatShape.builder().id(id).build(), "__handleFloat(" + 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 + ")"},
72+
{ByteShape.builder().id(id).build(), "__expectInt(" + DATA_SOURCE + ")"},
73+
{DoubleShape.builder().id(id).build(), "__limitedParseFloat(" + DATA_SOURCE + ")"},
74+
{FloatShape.builder().id(id).build(), "__limitedParseFloat(" + DATA_SOURCE + ")"},
75+
{IntegerShape.builder().id(id).build(), "__expectInt(" + DATA_SOURCE + ")"},
76+
{LongShape.builder().id(id).build(), "__expectInt(" + DATA_SOURCE + ")"},
77+
{ShortShape.builder().id(id).build(), "__expectInt(" + DATA_SOURCE + ")"},
7878
{StringShape.builder().id(id).build(), "__expectString(" + DATA_SOURCE + ")"},
7979
{
8080
StringShape.builder().id(id).addTrait(new MediaTypeTrait("foo+json")).build(),

0 commit comments

Comments
 (0)