Skip to content

Commit 756e2b6

Browse files
author
Elad Ben-Israel
authored
fix(cdk): CfnMapping.findInMap with tokens (#2531)
Do not validate existence of second key if the first key is a token. Fixes #1363
1 parent 7863ad3 commit 756e2b6

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

packages/@aws-cdk/cdk/lib/cfn-mapping.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ export class CfnMapping extends CfnRefElement {
3838
throw new Error(`Mapping doesn't contain top-level key '${key1}'`);
3939
}
4040

41-
// opportunistically check that the key exists (if the key does not contain tokens)
42-
if (!Token.isToken(key2) && !(key2 in this.mapping[key1])) {
41+
// opportunistically check that the second key exists (if the key does not contain tokens)
42+
if (!Token.isToken(key1) && !Token.isToken(key2) && !(key2 in this.mapping[key1])) {
4343
throw new Error(`Mapping doesn't contain second-level key '${key2}'`);
4444
}
4545

packages/@aws-cdk/cdk/test/test.mappings.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,46 @@ export = {
6262
test.deepEqual(stack.node.resolve(v1), expected);
6363
test.deepEqual(stack.node.resolve(v2), expected);
6464
test.done();
65-
}
65+
},
66+
67+
'no validation if first key is token and second is a static string'(test: Test) {
68+
// GIVEN
69+
const stack = new Stack();
70+
const mapping = new CfnMapping(stack, 'mapping', {
71+
mapping: {
72+
'us-east-1': {
73+
size: 12
74+
}
75+
}
76+
});
77+
78+
// WHEN
79+
const v = mapping.findInMap(Aws.region, 'size');
80+
81+
// THEN
82+
test.deepEqual(stack.node.resolve(v), {
83+
"Fn::FindInMap": [ 'mapping', { Ref: "AWS::Region" }, "size" ]
84+
});
85+
test.done();
86+
},
87+
88+
'validate first key if it is a string and second is a token'(test: Test) {
89+
// GIVEN
90+
const stack = new Stack();
91+
const mapping = new CfnMapping(stack, 'mapping', {
92+
mapping: {
93+
size: {
94+
'us-east-1': 12
95+
}
96+
}
97+
});
98+
99+
// WHEN
100+
const v = mapping.findInMap('size', Aws.region);
101+
102+
// THEN
103+
test.throws(() => mapping.findInMap('not-found', Aws.region), /Mapping doesn't contain top-level key 'not-found'/);
104+
test.deepEqual(stack.node.resolve(v), { 'Fn::FindInMap': [ 'mapping', 'size', { Ref: 'AWS::Region' } ] });
105+
test.done();
106+
},
66107
};

0 commit comments

Comments
 (0)