Skip to content

Commit 71cb421

Browse files
authored
fix(code): make CfnResource#_toCloudFormation null-safe (#3121)
The `CfnResource#_toCloudFormation` method creates a `PostResolveToken` with a post-processor that was not ready to handle the absence of `Properties` on the resolved value. It is however possible that `Properties` are missing when an object is created with the default configuration (e.g: by `new sqs.CfnQueue(this, 'Queue');`). This change makes the post-processor function correctly handle `undefined` in this case. Related #3093
1 parent d40fd05 commit 71cb421

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

packages/@aws-cdk/core/lib/cfn-resource.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ export class CfnResource extends CfnRefElement {
230230
Metadata: ignoreEmpty(this.cfnOptions.metadata),
231231
Condition: this.cfnOptions.condition && this.cfnOptions.condition.logicalId
232232
}, props => {
233-
props.Properties = this.renderProperties(props.Properties);
233+
const renderedProps = this.renderProperties(props.Properties || {});
234+
props.Properties = renderedProps && (Object.values(renderedProps).find(v => !!v) ? renderedProps : undefined);
234235
return deepMerge(props, this.rawOverrides);
235236
})
236237
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import nodeunit = require('nodeunit');
2+
import core = require('../lib');
3+
4+
export = nodeunit.testCase({
5+
'._toCloudFormation': {
6+
'does not call renderProperties with an undefined value'(test: nodeunit.Test) {
7+
const app = new core.App();
8+
const stack = new core.Stack(app, 'TestStack');
9+
const resource = new core.CfnResource(stack, 'DefaultResource', { type: 'Test::Resource::Fake' });
10+
11+
let called = false;
12+
(resource as any).renderProperties = (val: any) => {
13+
called = true;
14+
test.notEqual(val, null);
15+
};
16+
17+
test.deepEqual(app.synth().getStack(stack.stackName).template, {
18+
Resources: {
19+
DefaultResource: {
20+
Type: 'Test::Resource::Fake'
21+
}
22+
}
23+
});
24+
test.ok(called, `renderProperties must be called called`);
25+
26+
test.done();
27+
}
28+
}
29+
});

0 commit comments

Comments
 (0)