diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java index 83e740ec0c0..38968e2fdd4 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java @@ -344,7 +344,7 @@ private void generateEndpointParameterInstructionProvider() { operationContextParamValues.forEach((name, jmesPathForInputInJs) -> { writer.write( """ - $L: { type: \"operationContextParams\", get: (input: any) => $L }, + $L: { type: \"operationContextParams\", get: (input?: any) => $L }, """, name, jmesPathForInputInJs); }); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/RuleSetParameterFinder.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/RuleSetParameterFinder.java index 8825f2e7ae2..ffb84fd27d5 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/RuleSetParameterFinder.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/RuleSetParameterFinder.java @@ -274,7 +274,7 @@ public Map getOperationContextParamValues(OperationShape operati Optional trait = operation.getTrait(OperationContextParamsTrait.class); if (trait.isPresent()) { trait.get().getParameters().forEach((name, definition) -> { - String separator = "."; + String separator = "?."; String value = "input"; String path = definition.getPath(); @@ -289,13 +289,13 @@ public Map getOperationContextParamValues(OperationShape operati if (part.startsWith("keys(")) { // Get provided object for which keys are to be extracted. String object = part.substring(5, part.length() - 1); - value = "Object.keys(" + value + separator + object + ")"; + value = "Object.keys(" + value + separator + object + " ?? {})"; continue; } // Process list wildcard expression https://jmespath.org/specification.html#wildcard-expressions if (part.equals("*") || part.equals("[*]")) { - value = "Object.values(" + value + ")"; + value = "Object.values(" + value + " ?? {})"; continue; } diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/CommandGeneratorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/CommandGeneratorTest.java index 85557fffcdb..e9f8c24ab6b 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/CommandGeneratorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/CommandGeneratorTest.java @@ -39,12 +39,12 @@ public void writesOperationContextParamValues() { testCommmandCodegen( "endpointsV2/endpoints-operation-context-params.smithy", new String[] { - "opContextParamIdentifier: { type: \"operationContextParams\", get: (input: any) => input.fooString }", - "opContextParamSubExpression: { type: \"operationContextParams\", get: (input: any) => input.fooObj.bar }", - "opContextParamWildcardExpressionList: { type: \"operationContextParams\", get: (input: any) => input.fooList }", - "opContextParamWildcardExpressionListObj: { type: \"operationContextParams\", get: (input: any) => input.fooListObj.map((obj: any) => obj.key) }", - "opContextParamWildcardExpressionHash: { type: \"operationContextParams\", get: (input: any) => Object.values(input.fooObjObj).map((obj: any) => obj.bar) }", - "opContextParamKeys: { type: \"operationContextParams\", get: (input: any) => Object.keys(input.fooKeys) }", + "opContextParamIdentifier: { type: \"operationContextParams\", get: (input?: any) => input?.fooString }", + "opContextParamSubExpression: { type: \"operationContextParams\", get: (input?: any) => input?.fooObj?.bar }", + "opContextParamWildcardExpressionList: { type: \"operationContextParams\", get: (input?: any) => input?.fooList }", + "opContextParamWildcardExpressionListObj: { type: \"operationContextParams\", get: (input?: any) => input?.fooListObj?.map((obj: any) => obj?.key) }", + "opContextParamWildcardExpressionHash: { type: \"operationContextParams\", get: (input?: any) => Object.values(input?.fooObjObj ?? {}).map((obj: any) => obj?.bar) }", + "opContextParamKeys: { type: \"operationContextParams\", get: (input?: any) => Object.keys(input?.fooKeys ?? {}) }", } ); }