Skip to content

Commit

Permalink
fix(core): CFN version and description template sections were merged …
Browse files Browse the repository at this point in the history
…incorrectly

In the merge logic in Stack when rendering the template,
it was mistakenly assumed that all CFN sections are objects.
However, there are some sections, like Description and AWSTemplateFormatVersion,
that are in fact strings.
Add special logic for those cases in the merge functionality
(multiple provided CFN versions are checked for being identical,
and mutliple descriptions are merged together, with a newline in between).

Fixes aws#8151
  • Loading branch information
skinny85 committed May 28, 2020
1 parent ac001b8 commit b084416
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
30 changes: 25 additions & 5 deletions packages/@aws-cdk/core/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -848,12 +848,32 @@ function merge(template: any, part: any) {
if (!dest) {
template[section] = dest = src;
} else {
// add all entities from source section to destination section
for (const id of Object.keys(src)) {
if (id in dest) {
throw new Error(`section '${section}' already contains '${id}'`);
if (typeof dest === 'object') {
// add all entities from source section to destination section
for (const id of Object.keys(src)) {
if (id in dest) {
throw new Error(`section '${section}' already contains '${id}'`);
}
dest[id] = src[id];
}
} else {
// the sections are strings, like Description and AWSTemplateFormatVersion
// treat each differently
switch (section) {
case 'Description':
// merge the two descriptions, with a newline in between
template[section] = `${dest}\n${src}`;
break;
case 'AWSTemplateFormatVersion':
// check if the version are the same, and error out if not
if (dest !== src) {
throw new Error(`Conflicting template versions provided: '${dest}' and '${src}'`);
}
break;
default:
// let's error out by default - we can add other special cases later
throw new Error(`Duplicate section '${section}'`);
}
dest[id] = src[id];
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions packages/@aws-cdk/core/test/test.include.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,30 @@ export = {
test.throws(() => toCloudFormation(stack));
test.done();
},

'correctly merges template sections that contain strings'(test: Test) {
const stack = new Stack();

new CfnInclude(stack, 'T1', {
template: {
AWSTemplateFormatVersion: '2010-09-09',
Description: 'Test 1',
},
});
new CfnInclude(stack, 'T2', {
template: {
AWSTemplateFormatVersion: '2010-09-09',
Description: 'Test 2',
},
});

test.deepEqual(toCloudFormation(stack), {
AWSTemplateFormatVersion: '2010-09-09',
Description: 'Test 1\nTest 2',
});

test.done();
},
};

const template = {
Expand Down

0 comments on commit b084416

Please sign in to comment.