Skip to content

Commit

Permalink
fix(cdk): CfnMapping.findInMap with tokens
Browse files Browse the repository at this point in the history
Do not validate existence of second key if the first
key is a token.

Fixes #1363
  • Loading branch information
Elad Ben-Israel committed May 13, 2019
1 parent 6fc3718 commit 6e7a1a9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/@aws-cdk/cdk/lib/cfn-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export class CfnMapping extends CfnRefElement {
throw new Error(`Mapping doesn't contain top-level key '${key1}'`);
}

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

Expand Down
43 changes: 42 additions & 1 deletion packages/@aws-cdk/cdk/test/test.mappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,46 @@ export = {
test.deepEqual(stack.node.resolve(v1), expected);
test.deepEqual(stack.node.resolve(v2), expected);
test.done();
}
},

'no validation if first key is token and second is a static string'(test: Test) {
// GIVEN
const stack = new Stack();
const mapping = new CfnMapping(stack, 'mapping', {
mapping: {
'us-east-1': {
size: 12
}
}
});

// WHEN
const v = mapping.findInMap(Aws.region, 'size');

// THEN
test.deepEqual(stack.node.resolve(v), {
"Fn::FindInMap": [ 'mapping', { Ref: "AWS::Region" }, "size" ]
});
test.done();
},

'validate first key if it is a string and second is a token'(test: Test) {
// GIVEN
const stack = new Stack();
const mapping = new CfnMapping(stack, 'mapping', {
mapping: {
size: {
'us-east-1': 12
}
}
});

// WHEN
const v = mapping.findInMap('size', Aws.region);

// THEN
test.throws(() => mapping.findInMap('not-found', Aws.region), /Mapping doesn't contain top-level key 'not-found'/);
test.deepEqual(stack.node.resolve(v), { 'Fn::FindInMap': [ 'mapping', 'size', { Ref: 'AWS::Region' } ] });
test.done();
},
};

0 comments on commit 6e7a1a9

Please sign in to comment.