Skip to content

Commit

Permalink
fix for operationContextParam codegen (smithy-lang#1475)
Browse files Browse the repository at this point in the history
* fix for operationContextParam codegen

* no-throw on invoking operationContextParams getter
  • Loading branch information
kuhe authored Dec 19, 2024
1 parent 7fa6e5e commit e27d42d
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-socks-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/middleware-endpoint": patch
---

fix for operation context params
7 changes: 7 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ subprojects {
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
}
}
} else {
tasks.test {
testLogging {
events("failed")
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
}
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ export const resolveParams = async <
case "builtInParams":
endpointParams[name] = await createConfigValueProvider<Config>(instruction.name, name, clientConfig)();
break;
case "operationContextParams":
endpointParams[name] = instruction.get(commandInput);
break;
default:
throw new Error("Unrecognized endpoint parameter instruction: " + JSON.stringify(instruction));
}
Expand Down
11 changes: 10 additions & 1 deletion packages/middleware-endpoint/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export interface EndpointParameterInstructions {
| BuiltInParamInstruction
| ClientContextParamInstruction
| StaticContextParamInstruction
| ContextParamInstruction;
| ContextParamInstruction
| OperationContextParamInstruction;
}

/**
Expand Down Expand Up @@ -40,3 +41,11 @@ export interface ContextParamInstruction {
type: "contextParams";
name: string; // The input structure's member name that has contextParams trait
}

/**
* @internal
*/
export interface OperationContextParamInstruction {
type: "operationContextParams";
get(input: any): any;
}
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ private void generateEndpointParameterInstructionProvider() {
operationContextParamValues.forEach((name, jmesPathForInputInJs) -> {
writer.write(
"""
$L: { type: \"operationContextParams\", name: $L },
$L: { type: \"operationContextParams\", get: (input?: any) => $L },
""",
name, jmesPathForInputInJs);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,36 +274,36 @@ public Map<String, String> getOperationContextParamValues(OperationShape operati
Optional<OperationContextParamsTrait> trait = operation.getTrait(OperationContextParamsTrait.class);
if (trait.isPresent()) {
trait.get().getParameters().forEach((name, definition) -> {
String separator = ".";
String value = "this" + separator + "input";
String separator = "?.";
String value = "input";
String path = definition.getPath();

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

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

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

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

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

// Close all open brackets.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public void writesOperationContextParamValues() {
testCommmandCodegen(
"endpointsV2/endpoints-operation-context-params.smithy",
new String[] {
"opContextParamIdentifier: { type: \"operationContextParams\", name: this.input.fooString }",
"opContextParamSubExpression: { type: \"operationContextParams\", name: this.input.fooObj.bar }",
"opContextParamWildcardExpressionList: { type: \"operationContextParams\", name: this.input.fooList }",
"opContextParamWildcardExpressionListObj: { type: \"operationContextParams\", name: this.input.fooListObj.map(obj => obj.key) }",
"opContextParamWildcardExpressionHash: { type: \"operationContextParams\", name: Object.values(this.input.fooObjObj).map(obj => obj.bar) }",
"opContextParamKeys: { type: \"operationContextParams\", name: Object.keys(this.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 ?? {}) }",
}
);
}
Expand Down

0 comments on commit e27d42d

Please sign in to comment.