Skip to content

Commit 766a9bd

Browse files
committed
Handle multi-value query parameters appropriately
We use APIGateway multi-value query parameters which will always use an array value for query parameters, even if they're specified only once, and even if that single specification does not have a value. This changes our logic to not reject an array with a single value when parsing the value for a scalar-bound query parameter, which fixes our handling of query parameters.
1 parent d31adc2 commit 766a9bd

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,13 +1829,22 @@ private void readDirectQueryBindings(GenerationContext context, List<HttpBinding
18291829
writer.addImport("SerializationException",
18301830
"__SerializationException",
18311831
"@aws-smithy/server-common");
1832-
writer.openBlock("if (Array.isArray(query[$1S])) {", "}",
1832+
writer.write("let queryValue: string;");
1833+
writer.openBlock("if (Array.isArray(query[$S])) {", "}",
18331834
binding.getLocationName(),
18341835
() -> {
1835-
writer.write("throw new __SerializationException();");
1836+
writer.openBlock("if (query[$S].length === 1) {", "}",
1837+
binding.getLocationName(),
1838+
() -> {
1839+
writer.write("queryValue = query[$S][0];", binding.getLocationName());
1840+
});
1841+
writer.openBlock("else {", "}", () -> {
1842+
writer.write("throw new __SerializationException();");
1843+
});
18361844
});
1837-
writer.write("const queryValue = query[$1S] as string;",
1838-
binding.getLocationName());
1845+
writer.openBlock("else {", "}", () -> {
1846+
writer.write("queryValue = query[$S] as string;", binding.getLocationName());
1847+
});
18391848
}
18401849
String queryValue = getOutputValue(context, binding.getLocation(), "queryValue",
18411850
binding.getMember(), target);
@@ -1855,6 +1864,7 @@ private void readMappedQueryBindings(GenerationContext context, HttpBinding mapp
18551864
}
18561865
writer.write("let parsedQuery: { [key: string]: $L } = {}", valueType);
18571866
writer.openBlock("for (const [key, value] of Object.entries(query)) {", "}", () -> {
1867+
writer.write("let queryValue: string;");
18581868
final String parsedValue;
18591869
if (valueShape instanceof CollectionShape) {
18601870
writer.write("const valueArray = Array.isArray(value) ? (value as string[]) : [value as string];");
@@ -1866,10 +1876,19 @@ private void readMappedQueryBindings(GenerationContext context, HttpBinding mapp
18661876
"@aws-smithy/server-common");
18671877
writer.openBlock("if (Array.isArray(value)) {", "}",
18681878
() -> {
1869-
writer.write("throw new __SerializationException();");
1879+
writer.openBlock("if (value.length === 1) {", "}",
1880+
() -> {
1881+
writer.write("queryValue = value[0];");
1882+
});
1883+
writer.openBlock("else {", "}", () -> {
1884+
writer.write("throw new __SerializationException();");
1885+
});
18701886
});
1887+
writer.openBlock("else {", "}", () -> {
1888+
writer.write("queryValue = value as string;");
1889+
});
18711890
parsedValue = getOutputValue(context, mappedBinding.getLocation(),
1872-
"value as string", target.getValue(), valueShape);
1891+
"queryValue", target.getValue(), valueShape);
18731892
}
18741893
writer.write("parsedQuery[key] = $L;", parsedValue);
18751894
});

0 commit comments

Comments
 (0)