Skip to content
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
31 changes: 31 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,37 @@ export class APIGatewayVTL extends VTL {
return super.eval(node as any, returnVar);
}

/**
* Attempt to return the expression as a valid escaped json string.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Does it fail sometimes?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

No idea. Don't have exhaustive tests or runtime tests, but it works for the two cases I tried. I assume it will need to get more complex as more cases are found or we re-architect to support the various output cases (ex: late resolution with a sub-ast instead of in place transform that can account for the need to be a string instead of an object.)

*
* ```ts
* {
* x: input
* }
* ```
*
* =>
*
* ```ts
* { "x": $input.json('$') }
* ```
*
* =>
*
* ```ts
* "{ \"x\": $util.escapeJavaScript($input.json('$')) }"
* ```
*/
public stringify(expr: Expr): string {
const json = this.exprToJson(expr);
return `"${json
.replace(/"/g, '\\"')
.replace(
/\$input\.json\('([^']*)'\)/g,
"$util.escapeJavaScript($input.json('$1'))"
)}"`;
}

/**
* Renders a VTL string that will emit a JSON String representation of the {@link expr} to the VTL output.
*
Expand Down
4 changes: 1 addition & 3 deletions src/step-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,9 +571,7 @@ abstract class BaseStepFunction<
.map(([argName, argVal]) => {
if (argName === "input") {
// stringify the JSON input
return `"${argName}":"$util.escapeJavaScript(${context.exprToJson(
argVal
)})"`;
return `"${argName}":${context.stringify(argVal)}`;
} else {
return `"${argName}":${context.exprToJson(argVal)}`;
}
Expand Down
23 changes: 21 additions & 2 deletions test/__snapshots__/api.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions test/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,52 @@ test("AWS integration with Standard Step Function", () => {
expect(getTemplates(method)).toMatchSnapshot();
});

test("AWS integration with Standard Step Function using input data", () => {
const api = new aws_apigateway.RestApi(stack, "API");

const sfn = new StepFunction(
stack,
"SFN",
(_input: {
num: number;
obj: {
value: string;
};
}) => {
return "done";
}
);

const method = new AwsMethod(
{
httpMethod: "GET",
resource: api.root,
},
(
$input: ApiGatewayInput<{
query: {
num: string;
str: string;
};
body: {
value: string;
};
}>
) =>
sfn({
input: {
num: Number($input.params("num")),
obj: $input.data,
},
}),
($input) => {
return $input.data.executionArn;
}
);

expect(getTemplates(method)).toMatchSnapshot();
});

test("AWS integration with DynamoDB Table", () => {
const api = new aws_apigateway.RestApi(stack, "API");
const table = Table.fromTable(
Expand Down