Skip to content

Commit b021701

Browse files
Support deserializing query maps
1 parent 21c0bca commit b021701

File tree

1 file changed

+43
-17
lines changed

1 file changed

+43
-17
lines changed

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

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,29 +1308,54 @@ private void readQueryString(
13081308
HttpBindingIndex bindingIndex
13091309
) {
13101310
TypeScriptWriter writer = context.getWriter();
1311-
List<HttpBinding> queryBindings = bindingIndex.getRequestBindings(operation, Location.QUERY);
1312-
if (queryBindings.isEmpty()) {
1311+
List<HttpBinding> directQueryBindings = bindingIndex.getRequestBindings(operation, Location.QUERY);
1312+
List<HttpBinding> mappedQueryBindings = bindingIndex.getRequestBindings(operation, Location.QUERY_PARAMS);
1313+
if (directQueryBindings.isEmpty() && mappedQueryBindings.isEmpty()) {
13131314
return;
13141315
}
13151316
writer.write("const query = output.query");
13161317
writer.openBlock("if (query !== undefined && query !== null) {", "}", () -> {
1317-
for (HttpBinding binding : queryBindings) {
1318-
String memberName = context.getSymbolProvider().toMemberName(binding.getMember());
1319-
writer.openBlock("if (query['$L'] !== undefined) {", "}", binding.getLocationName(), () -> {
1320-
Shape target = context.getModel().expectShape(binding.getMember().getTarget());
1321-
String expectedType = "string";
1322-
if (target instanceof CollectionShape) {
1323-
expectedType = "string[]";
1324-
}
1325-
String headerValue = getOutputValue(context, binding.getLocation(),
1326-
"query['" + binding.getLocationName() + "'] as " + expectedType,
1327-
binding.getMember(), target);
1328-
writer.write("contents.$L = $L;", memberName, headerValue);
1329-
});
1318+
readDirectQueryBindings(context, directQueryBindings);
1319+
if (!mappedQueryBindings.isEmpty()) {
1320+
// There can only ever be one of these bindings on a given operation.
1321+
readMappedQueryBindings(context, mappedQueryBindings.get(0));
13301322
}
13311323
});
13321324
}
13331325

1326+
private void readDirectQueryBindings(GenerationContext context, List<HttpBinding> directQueryBindings) {
1327+
TypeScriptWriter writer = context.getWriter();
1328+
for (HttpBinding binding : directQueryBindings) {
1329+
String memberName = context.getSymbolProvider().toMemberName(binding.getMember());
1330+
writer.openBlock("if (query['$L'] !== undefined) {", "}", binding.getLocationName(), () -> {
1331+
Shape target = context.getModel().expectShape(binding.getMember().getTarget());
1332+
String queryValue = getOutputValue(context, binding.getLocation(),
1333+
"query['" + binding.getLocationName() + "'] as string",
1334+
binding.getMember(), target);
1335+
writer.write("contents.$L = $L;", memberName, queryValue);
1336+
});
1337+
}
1338+
}
1339+
1340+
private void readMappedQueryBindings(GenerationContext context, HttpBinding mappedBinding) {
1341+
TypeScriptWriter writer = context.getWriter();
1342+
MapShape target = context.getModel()
1343+
.expectShape(mappedBinding.getMember().getTarget()).asMapShape().get();
1344+
Shape valueShape = context.getModel().expectShape(target.getValue().getTarget());
1345+
String valueType = "string";
1346+
if (valueShape instanceof CollectionShape) {
1347+
valueType = "string[]";
1348+
}
1349+
writer.write("let parsedQuery: { [key: string]: $L } = {}", valueType);
1350+
String parsedValue = getOutputValue(context, mappedBinding.getLocation(), "value as string",
1351+
target.getValue(), valueShape);
1352+
writer.openBlock("for (const [key, value] of Object.entries(query)) {", "}", () -> {
1353+
writer.write("parsedQuery[key] = $L;", parsedValue);
1354+
});
1355+
String memberName = context.getSymbolProvider().toMemberName(mappedBinding.getMember());
1356+
writer.write("contents.$L = parsedQuery;", memberName);
1357+
}
1358+
13341359
private void readPath(
13351360
GenerationContext context,
13361361
OperationShape operation,
@@ -2080,9 +2105,10 @@ private String getCollectionOutputParam(
20802105
targetMember, collectionTarget);
20812106
String outputParam;
20822107
switch (bindingType) {
2083-
// TODO: make sure this actually works
2108+
case QUERY_PARAMS:
20842109
case QUERY:
2085-
return "(" + dataSource + ").map(_entry => " + collectionTargetValue + ")";
2110+
return String.format("(Array.isArray(%1$s) ? (%1$s[]) : [%1$s]).map(_entry => %2$s)",
2111+
dataSource, collectionTargetValue);
20862112
case LABEL:
20872113
dataSource = "(" + dataSource + " || \"\")";
20882114
// Split these values on commas.

0 commit comments

Comments
 (0)