@@ -1308,29 +1308,54 @@ private void readQueryString(
1308
1308
HttpBindingIndex bindingIndex
1309
1309
) {
1310
1310
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 ()) {
1313
1314
return ;
1314
1315
}
1315
1316
writer .write ("const query = output.query" );
1316
1317
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 ));
1330
1322
}
1331
1323
});
1332
1324
}
1333
1325
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
+
1334
1359
private void readPath (
1335
1360
GenerationContext context ,
1336
1361
OperationShape operation ,
@@ -2080,9 +2105,10 @@ private String getCollectionOutputParam(
2080
2105
targetMember , collectionTarget );
2081
2106
String outputParam ;
2082
2107
switch (bindingType ) {
2083
- // TODO: make sure this actually works
2108
+ case QUERY_PARAMS :
2084
2109
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 );
2086
2112
case LABEL :
2087
2113
dataSource = "(" + dataSource + " || \" \" )" ;
2088
2114
// Split these values on commas.
0 commit comments