Skip to content

Commit

Permalink
fix(core): nested Fn.join with token fails
Browse files Browse the repository at this point in the history
Fn.join has an optimization to flatten nested joins with the same delimiter:

    Fn.join(",", [ Fn.join(",", [ "a", "b" ]), "c" ]) == Fn.join(",", [ "a", "b", "c" ])

The logic in `isSplicableFnJoinIntrinsic` checks if the object is an Fn::Join which uses the same delimiter, and then splices (`...`) the inner value onto the outer Fn::Join instead of nesting the inner Fn::Join. This can only work if the inner value is a real array (otherwise, we get `Found non-callable @@iterator`�).

The fix is to add an additional check to `isSplicableFnJoinIntrinsic` which verifies the the inner value is indeed an array.

Fixes #5655
  • Loading branch information
Elad Ben-Israel committed Jan 7, 2020
1 parent 96b802b commit 48efa24
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
15 changes: 11 additions & 4 deletions packages/@aws-cdk/core/lib/private/cloudformation-lang.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Lazy } from "../lazy";
import { Reference } from "../reference";
import { DefaultTokenResolver, IFragmentConcatenator, IPostProcessor, IResolvable, IResolveContext } from "../resolvable";
import { DefaultTokenResolver, IFragmentConcatenator, IPostProcessor, IResolvable, IResolveContext } from "../resolvable";
import { TokenizedStringFragments } from "../string-fragments";
import { Token } from "../token";
import { Intrinsic } from "./intrinsic";
Expand Down Expand Up @@ -171,9 +171,16 @@ export function minimalCloudFormationJoin(delimiter: string, values: any[]): any
}

function isSplicableFnJoinIntrinsic(obj: any): boolean {
return isIntrinsic(obj)
&& Object.keys(obj)[0] === 'Fn::Join'
&& obj['Fn::Join'][0] === delimiter;
if (!isIntrinsic(obj)) { return false; }
if (Object.keys(obj)[0] !== 'Fn::Join') { return false; }

const [ delim, list ] = obj['Fn::Join'];
if (delim !== delimiter) { return false; }

if (Token.isUnresolved(list)) { return false; }
if (!Array.isArray(list)) { return false; }

return true;
}
}

Expand Down
17 changes: 16 additions & 1 deletion packages/@aws-cdk/core/test/test.fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,22 @@ export = nodeunit.testCase({
});
test.done();
}
}
},
'nested Fn::Join with list token'(test: nodeunit.Test) {
const stack = new Stack();
const inner = Fn.join(',', Token.asList({ NotReallyList: true }));
const outer = Fn.join(',', [ inner, 'Foo' ]);
test.deepEqual(stack.resolve(outer), {
'Fn::Join': [
',',
[
{ 'Fn::Join': [ ',', { NotReallyList: true } ] },
'Foo'
]
]
});
test.done();
},
});

function stringListToken(o: any): string[] {
Expand Down

0 comments on commit 48efa24

Please sign in to comment.