Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Fn.select incorrectly short-circuits complex expressions #19680

Merged
merged 2 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"Parameters": {
"DoIt": {
"Type": "String"
}
},
"Conditions": {
"MyCondition": {
"Fn::Equals": [{ "Ref": "DoIt" }, "Yes"]
}
},
"Resources": {
"Bucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": { "Fn::Select": [0, [
{ "Fn::If": ["MyCondition", "doing-it", { "Ref": "AWS::NoValue" }] },
"not-doingit"
]]}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,14 @@ describe('CDK Include', () => {
loadTestFileToJsObject('properties-not-in-cfn-spec.json'),
);
});

test('roundtrip a fn-select with a fn-if/ref-novalue in it', () => {
includeTestTemplate(stack, 'fn-select-with-novalue.json');

Template.fromStack(stack).templateMatches(
loadTestFileToJsObject('fn-select-with-novalue.json'),
);
});
});

interface IncludeTestTemplateProps {
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/core/lib/cfn-fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class Fn {
* @returns a token represented as a string
*/
public static select(index: number, array: string[]): string {
if (!Token.isUnresolved(array)) {
if (!Token.isUnresolved(index) && !Token.isUnresolved(array) && !array.some(Token.isUnresolved)) {
return array[index];
}

Expand Down
18 changes: 17 additions & 1 deletion packages/@aws-cdk/core/test/fn.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fc from 'fast-check';
import * as _ from 'lodash';
import { App, CfnOutput, Fn, Stack, Token } from '../lib';
import { App, Aws, CfnOutput, Fn, Stack, Token } from '../lib';
import { Intrinsic } from '../lib/private/intrinsic';

function asyncTest(cb: () => Promise<void>): () => void {
Expand All @@ -27,8 +27,24 @@ describe('fn', () => {
describe('eager resolution for non-tokens', () => {
test('Fn.select', () => {
expect(Fn.select(2, ['hello', 'you', 'dude'])).toEqual('dude');
});

test('Fn.select does not short-circuit if there are tokens in the array', () => {
const stack = new Stack();

expect(stack.resolve(Fn.select(2, [
Fn.conditionIf('xyz', 'yep', Aws.NO_VALUE).toString(),
'you',
'dude',
]))).toEqual({
'Fn::Select': [2, [
{ 'Fn::If': ['xyz', 'yep', { Ref: 'AWS::NoValue' }] },
'you',
'dude',
]],
});
});

test('Fn.split', () => {
expect(Fn.split(':', 'hello:world:yeah')).toEqual(['hello', 'world', 'yeah']);

Expand Down