From 187e847764934091208f647ccbfecffa100934ba Mon Sep 17 00:00:00 2001 From: Adam Ruka Date: Fri, 5 Mar 2021 17:27:51 -0800 Subject: [PATCH] fix(cfn-include): allow boolean values for string-typed properties CloudFormation is pretty lax when it comes to the types it accepts, and allows passing a boolean where a string is expected. Allow that possibility in cloudformation-include. --- .../test/test-templates/boolean-for-string.json | 10 ++++++++++ .../test/valid-templates.test.ts | 8 ++++++++ packages/@aws-cdk/core/lib/cfn-parse.ts | 6 ++++++ 3 files changed, 24 insertions(+) create mode 100644 packages/@aws-cdk/cloudformation-include/test/test-templates/boolean-for-string.json diff --git a/packages/@aws-cdk/cloudformation-include/test/test-templates/boolean-for-string.json b/packages/@aws-cdk/cloudformation-include/test/test-templates/boolean-for-string.json new file mode 100644 index 0000000000000..1ef8d59e40b89 --- /dev/null +++ b/packages/@aws-cdk/cloudformation-include/test/test-templates/boolean-for-string.json @@ -0,0 +1,10 @@ +{ + "Resources": { + "Bucket": { + "Type": "AWS::S3::Bucket", + "Properties": { + "AccessControl": true + } + } + } +} diff --git a/packages/@aws-cdk/cloudformation-include/test/valid-templates.test.ts b/packages/@aws-cdk/cloudformation-include/test/valid-templates.test.ts index 34b403a2e7a99..3d8b51098d293 100644 --- a/packages/@aws-cdk/cloudformation-include/test/valid-templates.test.ts +++ b/packages/@aws-cdk/cloudformation-include/test/valid-templates.test.ts @@ -116,6 +116,14 @@ describe('CDK Include', () => { ); }); + test('accepts booleans for properties with type string', () => { + includeTestTemplate(stack, 'boolean-for-string.json'); + + expect(stack).toMatchTemplate( + loadTestFileToJsObject('boolean-for-string.json'), + ); + }); + test('correctly changes the logical IDs, including references, if imported with preserveLogicalIds=false', () => { const cfnTemplate = includeTestTemplate(stack, 'bucket-with-encryption-key.json', { preserveLogicalIds: false, diff --git a/packages/@aws-cdk/core/lib/cfn-parse.ts b/packages/@aws-cdk/core/lib/cfn-parse.ts index 93376ae19d365..8f5d820584d98 100644 --- a/packages/@aws-cdk/core/lib/cfn-parse.ts +++ b/packages/@aws-cdk/core/lib/cfn-parse.ts @@ -132,6 +132,12 @@ export class FromCloudFormation { return new FromCloudFormationResult(value.toString()); } + // CloudFormation treats booleans and strings interchangeably; + // so, if we get a boolean here, convert it to a string + if (typeof value === 'boolean') { + return new FromCloudFormationResult(value.toString()); + } + // in all other cases, just return the input, // and let a validator handle it if it's not a string return new FromCloudFormationResult(value);