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 substitutions from "falsy" values #432

Merged
merged 2 commits into from
Aug 28, 2021
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
48 changes: 20 additions & 28 deletions __snapshots__/get-config.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ schema {
query: Query
mutation: Mutation
subscription: Subscription
}
",
}",
"substitutions": Object {},
"xrayEnabled": false,
},
Expand Down Expand Up @@ -146,6 +145,12 @@ type TweetConnection {
nextToken: String
}

schema {
query: Query
mutation: Mutation
subscription: Subscription
}

type User {
name: String!
handle: String!
Expand All @@ -160,14 +165,7 @@ type User {

# search functionality is available in elasticsearch integration
searchTweetsByKeyword(keyword: String!): TweetConnection
}

schema {
query: Query
mutation: Mutation
subscription: Subscription
}
",
}",
"substitutions": Object {},
"xrayEnabled": false,
},
Expand Down Expand Up @@ -233,6 +231,12 @@ type TweetConnection {
nextToken: String
}

schema {
query: Query
mutation: Mutation
subscription: Subscription
}

type User {
name: String!
handle: String!
Expand All @@ -247,14 +251,7 @@ type User {

# search functionality is available in elasticsearch integration
searchTweetsByKeyword(keyword: String!): TweetConnection
}

schema {
query: Query
mutation: Mutation
subscription: Subscription
}
",
}",
"substitutions": Object {},
"xrayEnabled": false,
},
Expand Down Expand Up @@ -340,8 +337,7 @@ schema {
query: Query
mutation: Mutation
subscription: Subscription
}
",
}",
"substitutions": Object {},
"xrayEnabled": false,
},
Expand Down Expand Up @@ -428,8 +424,7 @@ schema {
query: Query
mutation: Mutation
subscription: Subscription
}
",
}",
"substitutions": Object {},
"xrayEnabled": false,
},
Expand Down Expand Up @@ -528,8 +523,7 @@ schema {
query: Query
mutation: Mutation
subscription: Subscription
}
",
}",
"substitutions": Object {},
"xrayEnabled": false,
},
Expand Down Expand Up @@ -632,8 +626,7 @@ schema {
query: Query
mutation: Mutation
subscription: Subscription
}
",
}",
"substitutions": Object {},
"xrayEnabled": false,
},
Expand Down Expand Up @@ -728,8 +721,7 @@ schema {
query: Query
mutation: Mutation
subscription: Subscription
}
",
}",
"substitutions": Object {},
"xrayEnabled": false,
},
Expand Down
42 changes: 40 additions & 2 deletions __snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2781,7 +2781,7 @@ Object {
}
`;

exports[`iamRoleStatements individual template substitutions Substitutions for individual template should override global substitutions. 1`] = `
exports[`individual template substitutions Substitutions for individual template should override global substitutions. 1`] = `
Object {
"Fn::Join": Array [
"",
Expand Down Expand Up @@ -2827,7 +2827,45 @@ Object {
}
`;

exports[`iamRoleStatements template substitutions Templates with substitutions should be transformed into Fn::Join with Fn::Sub objects 1`] = `
exports[`template substitutions Falsy substitutions work 1`] = `
Object {
"Fn::Join": Array [
"",
Array [
"emptyString=",
Object {
"Fn::Sub": Array [
"\${emptyString}",
Object {
"emptyString": "",
},
],
},
"booleanFalse=",
Object {
"Fn::Sub": Array [
"\${booleanFalse}",
Object {
"booleanFalse": false,
},
],
},
"numberZero=",
Object {
"Fn::Sub": Array [
"\${numberZero}",
Object {
"numberZero": 0,
},
],
},
"",
],
],
}
`;

exports[`template substitutions Templates with substitutions should be transformed into Fn::Join with Fn::Sub objects 1`] = `
Object {
"Fn::Join": Array [
"",
Expand Down
120 changes: 69 additions & 51 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1064,64 +1064,82 @@ describe('iamRoleStatements', () => {
const roles = plugin.getDataSourceIamRolesResouces(config);
expect(roles).toEqual({});
});
});

describe('template substitutions', () => {
test('Templates with substitutions should be transformed into Fn::Join with Fn::Sub objects', () => {
const template = '#set($partitionKey = "${globalPK}")\n' +
'{\n' +
'"version" : "2018-05-29",\n' +
'"operation" : "GetItem",\n' +
'"key" : {\n' +
'"partitionKey": { "S": "${globalPK}" },\n' +
'"sortKey": { "S": "${globalSK}" },\n' +
'}\n' +
'}';

const variables =
{
globalPK: 'PK',
globalSK: 'SK',
};
describe('template substitutions', () => {
test('Templates with substitutions should be transformed into Fn::Join with Fn::Sub objects', () => {
const template = '#set($partitionKey = "${globalPK}")\n' +
'{\n' +
'"version" : "2018-05-29",\n' +
'"operation" : "GetItem",\n' +
'"key" : {\n' +
'"partitionKey": { "S": "${globalPK}" },\n' +
'"sortKey": { "S": "${globalSK}" },\n' +
'}\n' +
'}';

const variables =
{
globalPK: 'PK',
globalSK: 'SK',
};

const transformedTemplate = plugin.substituteGlobalTemplateVariables(template, variables);
expect(transformedTemplate).toMatchSnapshot();
});
const transformedTemplate = plugin.substituteGlobalTemplateVariables(template, variables);
expect(transformedTemplate).toMatchSnapshot();
});

describe('individual template substitutions', () => {
test('Substitutions for individual template should override global substitutions.', () => {
const template = '#set($partitionKey = "${globalPK}")\n' +
'{\n' +
'"version" : "2018-05-29",\n' +
'"operation" : "GetItem",\n' +
'"key" : {\n' +
'"partitionKey": { "S": "${globalPK}" },\n' +
'"sortKey": { "S": "${globalSK}" },\n' +
'}\n' +
'}';
test('Falsy substitutions work', () => {
const template = [
'emptyString=${emptyString}',
'booleanFalse=${booleanFalse}',
'numberZero=${numberZero}'
].join('');

const configuration =
{
substitutions:
{
globalPK: 'WrongValue',
globalSK: 'WrongValue',
},
};
const variables =
{
emptyString: '',
booleanFalse: false,
numberZero: 0,
};

const transformedTemplate = plugin.substituteGlobalTemplateVariables(template, variables);
expect(transformedTemplate).toMatchSnapshot();
});
});

const individualSubstitutions =
describe('individual template substitutions', () => {
test('Substitutions for individual template should override global substitutions.', () => {
const template = '#set($partitionKey = "${globalPK}")\n' +
'{\n' +
'"version" : "2018-05-29",\n' +
'"operation" : "GetItem",\n' +
'"key" : {\n' +
'"partitionKey": { "S": "${globalPK}" },\n' +
'"sortKey": { "S": "${globalSK}" },\n' +
'}\n' +
'}';

const configuration =
{
substitutions:
{
globalPK: 'PK',
globalSK: 'SK',
};

const transformedTemplate = plugin.processTemplate(
template,
configuration,
individualSubstitutions,
);
expect(transformedTemplate).toMatchSnapshot();
});
globalPK: 'WrongValue',
globalSK: 'WrongValue',
},
};

const individualSubstitutions =
{
globalPK: 'PK',
globalSK: 'SK',
};

const transformedTemplate = plugin.processTemplate(
template,
configuration,
individualSubstitutions,
);
expect(transformedTemplate).toMatchSnapshot();
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1543,7 +1543,7 @@ class ServerlessAppsyncPlugin {

const templateJoin = substituteTemplate.split('|||');
for (let i = 0; i < templateJoin.length; i += 1) {
if (substitutions[templateJoin[i]]) {
if (typeof substitutions[templateJoin[i]] !== 'undefined') {
const subs = { [templateJoin[i]]: substitutions[templateJoin[i]] };
templateJoin[i] = { 'Fn::Sub': [`\${${templateJoin[i]}}`, subs] };
}
Expand Down