Skip to content

Commit ef8d825

Browse files
authored
Support JMESPath flatten in OperationContextParams (#1537)
1 parent dbced06 commit ef8d825

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/RuleSetParameterFinder.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,16 @@ private String getJmesPathExpression(String separator, String value, String path
316316
value += getJmesPathExpression(separator, "obj", part) + ",";
317317
if (commaIndex == -1) {
318318
// Remove trailing comma and close bracket.
319-
value = value.substring(0, value.length() - 1) + "].filter((i) => i))";
319+
value = value.substring(0, value.length() - 1) + "].filter((i) => i)";
320320
break;
321321
}
322322
}
323+
324+
// Process Flatten operator https://jmespath.org/specification.html#flatten-operator
325+
if (path.startsWith("[]")) {
326+
value += ".flat()";
327+
path = path.substring(2);
328+
}
323329
continue;
324330
}
325331

@@ -359,6 +365,21 @@ private String getJmesPathExpressionSection(String separator, String value, Stri
359365
return value;
360366
}
361367

368+
// Process Flatten operator https://jmespath.org/specification.html#flatten-operator
369+
if (part.endsWith("[]")) {
370+
// Get key to run hash wildcard on.
371+
String key = part.substring(0, part.length() - 2);
372+
373+
// If key is on list item
374+
if (key.endsWith("[*]")) {
375+
value = value + separator + key.substring(0, key.length() - 3) + ".flat()";
376+
} else {
377+
// key is on object
378+
value = value + separator + key + ").flat()";
379+
}
380+
return value;
381+
}
382+
362383
// Treat remaining part as identifier without spaces https://jmespath.org/specification.html#identifiers
363384
value += separator + part;
364385
return value;

smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/CommandGeneratorTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ public void writesOperationContextParamValues() {
4242
"opContextParamIdentifier: { type: \"operationContextParams\", get: (input?: any) => input?.fooString }",
4343
"opContextParamSubExpression: { type: \"operationContextParams\", get: (input?: any) => input?.fooObj?.bar }",
4444
"opContextParamWildcardExpressionList: { type: \"operationContextParams\", get: (input?: any) => input?.fooList }",
45+
"opContextParamWildcardExpressionListFlatten: { type: \"operationContextParams\", get: (input?: any) => input?.fooListList.flat() }",
4546
"opContextParamWildcardExpressionListObj: { type: \"operationContextParams\", get: (input?: any) => input?.fooListObj?.map((obj: any) => obj?.key) }",
47+
"opContextParamWildcardExpressionListObjListFlatten: { type: \"operationContextParams\", get: (input?: any) => input?.fooListObjList?.map((obj: any) => obj?.key).flat() }",
4648
"opContextParamWildcardExpressionHash: { type: \"operationContextParams\", get: (input?: any) => Object.values(input?.fooObjObj ?? {}).map((obj: any) => obj?.bar) }",
4749
"opContextParamMultiSelectList: { type: \"operationContextParams\", get: (input?: any) => input?.fooListObjObj?.map((obj: any) => [obj?.fooList[0],obj?.fooObject?.bar,obj?.fooString].filter((i) => i)) }",
50+
"opContextParamMultiSelectListFlatten: { type: \"operationContextParams\", get: (input?: any) => input?.fooListObjObj?.map((obj: any) => [obj?.fooList].filter((i) => i).flat()) }",
4851
"opContextParamKeys: { type: \"operationContextParams\", get: (input?: any) => Object.keys(input?.fooKeys ?? {}) }",
4952
}
5053
);

smithy-typescript-codegen/src/test/resources/software/amazon/smithy/typescript/codegen/endpointsV2/endpoints-operation-context-params.smithy

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,24 @@ namespace smithy.example
1414
opContextParamWildcardExpressionList: {
1515
type: "stringArray",
1616
},
17+
opContextParamWildcardExpressionListFlatten: {
18+
type: "stringArray",
19+
},
1720
opContextParamWildcardExpressionListObj: {
1821
type: "stringArray",
1922
},
23+
opContextParamWildcardExpressionListObjListFlatten: {
24+
type: "stringArray",
25+
},
2026
opContextParamWildcardExpressionHash: {
2127
type: "stringArray",
2228
},
2329
opContextParamMultiSelectList: {
2430
type: "stringArray",
2531
},
32+
opContextParamMultiSelectListFlatten: {
33+
type: "stringArray",
34+
},
2635
opContextParamKeys: {
2736
type: "stringArray",
2837
},
@@ -38,9 +47,12 @@ service Example {
3847
"opContextParamIdentifier": { path: "fooString" }
3948
"opContextParamSubExpression": { path: "fooObj.bar" }
4049
"opContextParamWildcardExpressionList": { path: "fooList[*]" }
50+
"opContextParamWildcardExpressionListFlatten": { path: "fooListList[*][]" }
4151
"opContextParamWildcardExpressionListObj": { path: "fooListObj[*].key" }
52+
"opContextParamWildcardExpressionListObjListFlatten": { path: "fooListObjList[*].key[]" }
4253
"opContextParamWildcardExpressionHash": { path: "fooObjObj.*.bar" }
4354
"opContextParamMultiSelectList": { path: "fooListObjObj[*].[fooList[0], fooObject.bar, fooString]" }
55+
"opContextParamMultiSelectListFlatten": { path: "fooListObjObj[*].[fooList][]" }
4456
"opContextParamKeys": { path: "keys(fooKeys)" }
4557
)
4658
operation GetFoo {
@@ -52,7 +64,9 @@ operation GetFoo {
5264
structure GetFooInput {
5365
fooKeys: FooObject,
5466
fooList: FooList,
67+
fooListList: FooListList,
5568
fooListObj: FooListObject,
69+
fooListObjList: FooListObjectList,
5670
fooListObjObj: FooListObjectObject,
5771
fooObj: FooObject,
5872
fooObjObj: FooObjectObject,
@@ -77,6 +91,10 @@ structure FooObjectObject {
7791
baz: FooObject
7892
}
7993

94+
list FooListList {
95+
member: FooList
96+
}
97+
8098
list FooList {
8199
member: String
82100
}
@@ -89,6 +107,14 @@ structure FooListObjectMember {
89107
key: String
90108
}
91109

110+
list FooListObjectList {
111+
member: FooListObjectListMember
112+
}
113+
114+
structure FooListObjectListMember {
115+
key: FooList
116+
}
117+
92118
structure GetFooOutput {}
93119

94120
@error("client")

0 commit comments

Comments
 (0)