Skip to content

Commit 5926e10

Browse files
fix: VTL string escaping
$util.escapeJavaScript() escapes the characters in a string using JavaScript string rules. This function will turn any regular single quotes (') into escaped ones (\'). However, the escaped single quotes are not valid in JSON. Thus, when the output from this function is used in a JSON property, you must turn any escaped single quotes (\') back to regular single quotes (').
1 parent b0fa5b8 commit 5926e10

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/deploy/events/apiGateway/methods.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ module.exports = {
288288
return {
289289
'Fn::Sub': [
290290
`
291-
#set( $body = $util.escapeJavaScript($input.json('$')).replaceAll(\"\\\\'\", \"'\") )
291+
#set( $body = $util.escapeJavaScript($input.json('$')).replaceAll("\\\\'", "'") )
292292
{"input": "$body", "name": "$context.requestId", "stateMachineArn":"\${StateMachineArn}"}`,
293293
{ StateMachineArn: { Ref: stateMachineLogicalId } },
294294
],

lib/deploy/events/apiGateway/methods.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,25 @@ describe('#methods()', () => {
265265
});
266266
});
267267

268+
describe('#buildDefaultRequestTemplate()', () => {
269+
it('should return the default request template', () => {
270+
const defaultRequestTemplate = serverlessStepFunctions
271+
.buildDefaultRequestTemplate('StateMachineStepFunctionsStateMachine');
272+
expect(defaultRequestTemplate['Fn::Sub'][0])
273+
.to.contain(
274+
'#set( $body = $util.escapeJavaScript($input.json(\'$\')).replaceAll("\\\\\'", "\'") )',
275+
);
276+
expect(defaultRequestTemplate['Fn::Sub'][0])
277+
.to.contain(
278+
'{"input": "$body", "name": "$context.requestId", "stateMachineArn":"${StateMachineArn}"}',
279+
);
280+
expect(defaultRequestTemplate['Fn::Sub'][1])
281+
.to.be.deep.equal({
282+
StateMachineArn: { Ref: 'StateMachineStepFunctionsStateMachine' },
283+
});
284+
});
285+
});
286+
268287
describe('#getMethodResponses()', () => {
269288
it('should return a corresponding methodResponses resource', () => {
270289
expect(serverlessStepFunctions.getMethodResponses().Properties)

0 commit comments

Comments
 (0)