Skip to content

Commit e27d42d

Browse files
authored
fix for operationContextParam codegen (#1475)
* fix for operationContextParam codegen * no-throw on invoking operationContextParams getter
1 parent 7fa6e5e commit e27d42d

File tree

7 files changed

+41
-16
lines changed

7 files changed

+41
-16
lines changed

.changeset/thick-socks-cross.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/middleware-endpoint": patch
3+
---
4+
5+
fix for operation context params

build.gradle.kts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@ subprojects {
198198
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
199199
}
200200
}
201+
} else {
202+
tasks.test {
203+
testLogging {
204+
events("failed")
205+
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
206+
}
207+
}
201208
}
202209

203210
/*

packages/middleware-endpoint/src/adaptors/getEndpointFromInstructions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ export const resolveParams = async <
9292
case "builtInParams":
9393
endpointParams[name] = await createConfigValueProvider<Config>(instruction.name, name, clientConfig)();
9494
break;
95+
case "operationContextParams":
96+
endpointParams[name] = instruction.get(commandInput);
97+
break;
9598
default:
9699
throw new Error("Unrecognized endpoint parameter instruction: " + JSON.stringify(instruction));
97100
}

packages/middleware-endpoint/src/types.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export interface EndpointParameterInstructions {
66
| BuiltInParamInstruction
77
| ClientContextParamInstruction
88
| StaticContextParamInstruction
9-
| ContextParamInstruction;
9+
| ContextParamInstruction
10+
| OperationContextParamInstruction;
1011
}
1112

1213
/**
@@ -40,3 +41,11 @@ export interface ContextParamInstruction {
4041
type: "contextParams";
4142
name: string; // The input structure's member name that has contextParams trait
4243
}
44+
45+
/**
46+
* @internal
47+
*/
48+
export interface OperationContextParamInstruction {
49+
type: "operationContextParams";
50+
get(input: any): any;
51+
}

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\", name: $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: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -274,36 +274,36 @@ 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 = ".";
278-
String value = "this" + separator + "input";
277+
String separator = "?.";
278+
String value = "input";
279279
String path = definition.getPath();
280280

281281
// Split JMESPath expression string on separator and add JavaScript equivalent.
282282
for (String part : path.split("[" + separator + "]")) {
283283
if (value.endsWith(")")) {
284284
// The value is an object, which needs to run on map.
285-
value += ".map(obj => obj";
285+
value += ".map((obj: any) => obj";
286286
}
287287

288288
// Process keys https://jmespath.org/specification.html#keys
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

302302
// Process hash wildcard expression https://jmespath.org/specification.html#wildcard-expressions
303303
if (part.endsWith("[*]")) {
304304
// Get key to run hash wildcard on.
305305
String key = part.substring(0, part.length() - 3);
306-
value = value + separator + key + separator + "map(obj => obj";
306+
value = value + separator + key + separator + "map((obj: any) => obj";
307307
continue;
308308
}
309309

@@ -312,8 +312,9 @@ public Map<String, String> getOperationContextParamValues(OperationShape operati
312312
}
313313

314314
// Remove no-op map, if it exists.
315-
if (value.endsWith(separator + "map(obj => obj")) {
316-
value = value.substring(0, value.length() - 15);
315+
final String noOpMap = "map((obj: any) => obj";
316+
if (value.endsWith(separator + noOpMap)) {
317+
value = value.substring(0, value.length() - noOpMap.length() - separator.length());
317318
}
318319

319320
// Close all open brackets.

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\", name: this.input.fooString }",
43-
"opContextParamSubExpression: { type: \"operationContextParams\", name: this.input.fooObj.bar }",
44-
"opContextParamWildcardExpressionList: { type: \"operationContextParams\", name: this.input.fooList }",
45-
"opContextParamWildcardExpressionListObj: { type: \"operationContextParams\", name: this.input.fooListObj.map(obj => obj.key) }",
46-
"opContextParamWildcardExpressionHash: { type: \"operationContextParams\", name: Object.values(this.input.fooObjObj).map(obj => obj.bar) }",
47-
"opContextParamKeys: { type: \"operationContextParams\", name: Object.keys(this.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)