Skip to content

Commit

Permalink
fix(AWS Deploy): Ensure to strip all null properties (#10304)
Browse files Browse the repository at this point in the history
  • Loading branch information
GurmeharS committed Dec 6, 2021
1 parent 9faf37a commit 1f58760
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/plugins/aws/package/lib/stripNullPropsFromTemplateResources.js
@@ -1,16 +1,22 @@
'use strict';

const stripNullPropsFromObj = (obj) => {
Object.entries(obj).forEach(([propName, propVal]) => {
if (propVal === null) {
delete obj[propName];
} else if (typeof propVal === 'object') {
stripNullPropsFromObj(propVal);
}
});
};

module.exports = {
stripNullPropsFromTemplateResources() {
const resources = this.serverless.service.provider.compiledCloudFormationTemplate.Resources;

for (const resource of Object.values(resources)) {
if (resource.Properties) {
for (const [propName, propVal] of Object.entries(resource.Properties)) {
if (propVal === null) {
delete resource.Properties[propName];
}
}
stripNullPropsFromObj(resource.Properties);
}
}
},
Expand Down
Expand Up @@ -28,6 +28,24 @@ describe('test/unit/lib/plugins/aws/package/lib/stripNullPropsFromTemplateResour
ObjectLockEnabled: false,
},
},
myLambdaFunction: {
Type: 'AWS::Lambda::Function',
Properties: {
Environment: {
Variables: {
MYNULLVAR: null,
},
},
Handler: 'index.handler',
Role: {
'Fn::GetAtt': ['MyLambdaRole', 'Arn'],
},
Code: {
S3Bucket: 's3-containing-lambda',
},
Runtime: 'nodejs12.x',
},
},
},
},
},
Expand All @@ -39,6 +57,12 @@ describe('test/unit/lib/plugins/aws/package/lib/stripNullPropsFromTemplateResour
expect(Object.keys(finalTemplate.Resources.myBucket.Properties).length).to.equal(0);
});

it('Should remove null values within nested objects in resource properties', async () => {
expect(
Object.keys(finalTemplate.Resources.myLambdaFunction.Properties.Environment.Variables).length
).to.equal(0);
});

it('Should not affect resources without null props', async () => {
expect(Object.keys(finalTemplate.Resources.anotherBucket.Properties).length).to.equal(1);
});
Expand Down

0 comments on commit 1f58760

Please sign in to comment.