Skip to content

Commit a821f24

Browse files
authored
fix(codegen): fix jmespath flatten level (#1559)
* fix(codegen): fix jmespath flatten level * unused import
1 parent 76264f4 commit a821f24

File tree

4 files changed

+62
-20
lines changed

4 files changed

+62
-20
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ private void generateEndpointParameterInstructionProvider() {
439439
operationContextParamValues.forEach((name, jmesPathForInputInJs) -> {
440440
writer.write(
441441
"""
442-
$L: { type: \"operationContextParams\", get: (input?: any) => $L },
442+
$L: { type: "operationContextParams", get: (input?: any) => $L },
443443
""",
444444
name, jmesPathForInputInJs);
445445
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ public Map<String, String> getOperationContextParamValues(OperationShape operati
296296
return map;
297297
}
298298

299-
private String getJmesPathExpression(String separator, String value, String path) {
299+
String getJmesPathExpression(String separator, String value, String path) {
300300
// Split JMESPath expression string on separator and add JavaScript equivalent.
301301
while (path.length() > 0) {
302302
if (path.startsWith("[") && !path.startsWith("[*]")) {
@@ -316,7 +316,7 @@ 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
}

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

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,58 @@
1212
public class CommandGeneratorTest {
1313
@Test
1414
public void addsCommandSpecificPlugins() {
15-
testCommmandCodegen(
15+
testCommandCodegen(
1616
"output-structure.smithy",
1717
new String[] {"getSerdePlugin(config, this.serialize, this.deserialize)"}
1818
);
1919
}
2020

2121
@Test
2222
public void writesSerializer() {
23-
testCommmandCodegen(
23+
testCommandCodegen(
2424
"output-structure.smithy",
2525
new String[] {".ser("}
2626
);
2727
}
2828

2929
@Test
3030
public void writesDeserializer() {
31-
testCommmandCodegen(
31+
testCommandCodegen(
3232
"output-structure.smithy",
3333
new String[] {".de("}
3434
);
3535
}
3636

3737
@Test
3838
public void writesOperationContextParamValues() {
39-
testCommmandCodegen(
39+
testCommandCodegen(
4040
"endpointsV2/endpoints-operation-context-params.smithy",
4141
new String[] {
42-
"opContextParamIdentifier: { type: \"operationContextParams\", get: (input?: any) => input?.fooString }",
43-
"opContextParamSubExpression: { type: \"operationContextParams\", get: (input?: any) => input?.fooObj?.bar }",
44-
"opContextParamWildcardExpressionList: { type: \"operationContextParams\", get: (input?: any) => input?.fooList }",
45-
"opContextParamWildcardExpressionListFlatten: { type: \"operationContextParams\", get: (input?: any) => input?.fooListList.flat() }",
46-
"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() }",
48-
"opContextParamWildcardExpressionHash: { type: \"operationContextParams\", get: (input?: any) => Object.values(input?.fooObjObj ?? {}).map((obj: any) => obj?.bar) }",
49-
"opContextParamMultiSelectList: { type: \"operationContextParams\", get: (input?: any) => input?.fooListObjObj?.map((obj: any) => [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()) }",
51-
"opContextParamKeys: { type: \"operationContextParams\", get: (input?: any) => Object.keys(input?.fooKeys ?? {}) }",
42+
"""
43+
opContextParamIdentifier: { type: "operationContextParams", get: (input?: any) => input?.fooString }""",
44+
"""
45+
opContextParamSubExpression: { type: "operationContextParams", get: (input?: any) => input?.fooObj?.bar }""",
46+
"""
47+
opContextParamWildcardExpressionList: { type: "operationContextParams", get: (input?: any) => input?.fooList }""",
48+
"""
49+
opContextParamWildcardExpressionListFlatten: { type: "operationContextParams", get: (input?: any) => input?.fooListList.flat() }""",
50+
"""
51+
opContextParamWildcardExpressionListObj: { type: "operationContextParams", get: (input?: any) => input?.fooListObj?.map((obj: any) => obj?.key) }""",
52+
"""
53+
opContextParamWildcardExpressionListObjListFlatten: { type: "operationContextParams", get: (input?: any) => input?.fooListObjList?.map((obj: any) => obj?.key).flat() }""",
54+
"""
55+
opContextParamWildcardExpressionHash: { type: "operationContextParams", get: (input?: any) => Object.values(input?.fooObjObj ?? {}).map((obj: any) => obj?.bar) }""",
56+
"""
57+
opContextParamMultiSelectList: { type: "operationContextParams", get: (input?: any) => input?.fooListObjObj?.map((obj: any) => [obj?.fooObject?.bar,obj?.fooString].filter((i) => i)) }""",
58+
"""
59+
opContextParamMultiSelectListFlatten: { type: "operationContextParams", get: (input?: any) => input?.fooListObjObj?.map((obj: any) => [obj?.fooList].filter((i) => i)).flat() }""",
60+
"""
61+
opContextParamKeys: { type: "operationContextParams", get: (input?: any) => Object.keys(input?.fooKeys ?? {}) }""",
5262
}
5363
);
5464
}
5565

56-
private void testCommmandCodegen(String filename, String[] expectedTypeArray) {
66+
private void testCommandCodegen(String filename, String[] expectedTypeArray) {
5767
MockManifest manifest = new MockManifest();
5868
PluginContext context = PluginContext.builder()
5969
.pluginClassLoader(getClass().getClassLoader())

smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/endpointsV2/RuleSetParameterFinderTest.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.List;
44
import java.util.Optional;
5+
56
import org.junit.jupiter.api.Test;
67
import org.junit.jupiter.api.extension.ExtendWith;
78
import org.mockito.Mock;
@@ -16,6 +17,7 @@
1617

1718
@ExtendWith(MockitoExtension.class)
1819
class RuleSetParameterFinderTest {
20+
RuleSetParameterFinder subject;
1921

2022
Node ruleSet = Node.parse("""
2123
{
@@ -111,16 +113,46 @@ class RuleSetParameterFinderTest {
111113
}
112114
""");
113115

116+
114117
@Test
115118
void getEffectiveParams(@Mock ServiceShape serviceShape, @Mock EndpointRuleSetTrait endpointRuleSetTrait) {
116119
EndpointRuleSet endpointRuleSet = EndpointRuleSet.fromNode(ruleSet);
117120
when(serviceShape.getTrait(EndpointRuleSetTrait.class)).thenReturn(Optional.of(endpointRuleSetTrait));
118121
when(endpointRuleSetTrait.getEndpointRuleSet()).thenReturn(endpointRuleSet);
119-
120-
RuleSetParameterFinder subject = new RuleSetParameterFinder(serviceShape);
122+
subject = new RuleSetParameterFinder(serviceShape);
121123

122124
List<String> effectiveParams = subject.getEffectiveParams();
123125

124126
assertEquals(List.of("BasicParameter", "NestedParameter", "ShorthandParameter", "UrlOnlyParameter"), effectiveParams);
125127
}
128+
129+
@Test
130+
void getJmesPathExpression(@Mock ServiceShape serviceShape, @Mock EndpointRuleSetTrait endpointRuleSetTrait) {
131+
when(serviceShape.getTrait(EndpointRuleSetTrait.class)).thenReturn(Optional.of(endpointRuleSetTrait));
132+
subject = new RuleSetParameterFinder(serviceShape);
133+
134+
assertEquals(
135+
"""
136+
Object.keys(input?.RequestItems ?? {})""",
137+
subject.getJmesPathExpression("?.", "input", "keys(RequestItems)")
138+
);
139+
140+
assertEquals(
141+
"""
142+
input?.TableCreationParameters?.TableName""",
143+
subject.getJmesPathExpression("?.", "input", "TableCreationParameters.TableName")
144+
);
145+
146+
assertEquals(
147+
"""
148+
input?.TransactItems?.map((obj: any) => obj?.Get?.TableName""",
149+
subject.getJmesPathExpression("?.", "input", "TransactItems[*].Get.TableName")
150+
);
151+
152+
assertEquals(
153+
"""
154+
input?.TransactItems?.map((obj: any) => [obj?.ConditionCheck?.TableName,obj?.Put?.TableName,obj?.Delete?.TableName,obj?.Update?.TableName].filter((i) => i)).flat()""",
155+
subject.getJmesPathExpression("?.", "input", "TransactItems[*].[ConditionCheck.TableName, Put.TableName, Delete.TableName, Update.TableName][]")
156+
);
157+
}
126158
}

0 commit comments

Comments
 (0)