Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for operationContextParam codegen #1475

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can I assume that the path expression is solely based on the input? No other parameters are needed, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. As per internal documentation

The operationContextParams trait defines one or more context parameters that MUST be bound to values specified in the operation input using a JMESPath expression

}
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 },
Copy link
Contributor Author

@kuhe kuhe Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These need to be functions because there is no this at the time the instruction is evaluated. The instruction sits in the global/module scope due to the use of classBuilder to create commands.

export class XCommand extends $Command.
  classBuilder<...>()
  .ep({
    ...commonParams,
    MemberName: { type: "operationContextParams", get: (input?: any) => ... },
  })
  ...
  .build() {
  protected declare static __types: { ... }
}

""",
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
Loading