Skip to content

Commit 3b3ba74

Browse files
committed
no-throw on invoking operationContextParams getter
1 parent f7ada55 commit 3b3ba74

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
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
@@ -344,7 +344,7 @@ private void generateEndpointParameterInstructionProvider() {
344344
operationContextParamValues.forEach((name, jmesPathForInputInJs) -> {
345345
writer.write(
346346
"""
347-
$L: { type: \"operationContextParams\", get: (input: any) => $L },
347+
$L: { type: \"operationContextParams\", get: (input?: any) => $L },
348348
""",
349349
name, jmesPathForInputInJs);
350350
});

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public Map<String, String> getOperationContextParamValues(OperationShape operati
274274
Optional<OperationContextParamsTrait> trait = operation.getTrait(OperationContextParamsTrait.class);
275275
if (trait.isPresent()) {
276276
trait.get().getParameters().forEach((name, definition) -> {
277-
String separator = ".";
277+
String separator = "?.";
278278
String value = "input";
279279
String path = definition.getPath();
280280

@@ -289,13 +289,13 @@ public Map<String, String> getOperationContextParamValues(OperationShape operati
289289
if (part.startsWith("keys(")) {
290290
// Get provided object for which keys are to be extracted.
291291
String object = part.substring(5, part.length() - 1);
292-
value = "Object.keys(" + value + separator + object + ")";
292+
value = "Object.keys(" + value + separator + object + " ?? {})";
293293
continue;
294294
}
295295

296296
// Process list wildcard expression https://jmespath.org/specification.html#wildcard-expressions
297297
if (part.equals("*") || part.equals("[*]")) {
298-
value = "Object.values(" + value + ")";
298+
value = "Object.values(" + value + " ?? {})";
299299
continue;
300300
}
301301

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ public void writesOperationContextParamValues() {
3939
testCommmandCodegen(
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-
"opContextParamWildcardExpressionListObj: { type: \"operationContextParams\", get: (input: any) => input.fooListObj.map((obj: any) => obj.key) }",
46-
"opContextParamWildcardExpressionHash: { type: \"operationContextParams\", get: (input: any) => Object.values(input.fooObjObj).map((obj: any) => obj.bar) }",
47-
"opContextParamKeys: { type: \"operationContextParams\", get: (input: any) => Object.keys(input.fooKeys) }",
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+
"opContextParamWildcardExpressionListObj: { type: \"operationContextParams\", get: (input?: any) => input?.fooListObj?.map((obj: any) => obj?.key) }",
46+
"opContextParamWildcardExpressionHash: { type: \"operationContextParams\", get: (input?: any) => Object.values(input?.fooObjObj ?? {}).map((obj: any) => obj?.bar) }",
47+
"opContextParamKeys: { type: \"operationContextParams\", get: (input?: any) => Object.keys(input?.fooKeys ?? {}) }",
4848
}
4949
);
5050
}

0 commit comments

Comments
 (0)