Skip to content

Commit

Permalink
fix(core): prevent the error when the condition is split into groups …
Browse files Browse the repository at this point in the history
…of 10 and 1 in `Fn.conditionAnd()` (aws#25999)

Closes aws#25696 (comment)

Same solution as aws#25708

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
wafuwafu13 authored and lukey-aleios committed Jun 30, 2023
1 parent ef8f277 commit 52f2d4a
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/aws-cdk-lib/core/lib/cfn-fn.ts
Expand Up @@ -275,7 +275,10 @@ export class Fn {
if (conditions.length === 1) {
return conditions[0] as ICfnRuleConditionExpression;
}
return Fn.conditionAnd(..._inGroupsOf(conditions, 10).map(group => new FnAnd(...group)));
if (conditions.length <= 10) {
return new FnAnd(...conditions);
}
return Fn.conditionAnd(..._inGroupsOf(conditions, 10).map(group => Fn.conditionAnd(...group)));
}

/**
Expand Down
100 changes: 100 additions & 0 deletions packages/aws-cdk-lib/core/test/condition.test.ts
Expand Up @@ -60,6 +60,106 @@ describe('condition', () => {
});
});

test('condition length is 10n + 1 in Fn.conditionAnd', () => {
// GIVEN
const stack = new cdk.Stack();
const expression = cdk.Fn.conditionAnd(
cdk.Fn.conditionEquals('a', '1'),
cdk.Fn.conditionEquals('b', '2'),
cdk.Fn.conditionEquals('c', '3'),
cdk.Fn.conditionEquals('d', '4'),
cdk.Fn.conditionEquals('e', '5'),
cdk.Fn.conditionEquals('f', '6'),
cdk.Fn.conditionEquals('g', '7'),
cdk.Fn.conditionEquals('h', '8'),
cdk.Fn.conditionEquals('i', '9'),
cdk.Fn.conditionEquals('j', '10'),
cdk.Fn.conditionEquals('k', '11'),
);

// WHEN
new cdk.CfnCondition(stack, 'Condition', { expression });

// THEN
expect(toCloudFormation(stack)).toEqual({
Conditions: {
Condition: {
'Fn::And': [
{
'Fn::And': [
{ 'Fn::Equals': ['a', '1'] },
{ 'Fn::Equals': ['b', '2'] },
{ 'Fn::Equals': ['c', '3'] },
{ 'Fn::Equals': ['d', '4'] },
{ 'Fn::Equals': ['e', '5'] },
{ 'Fn::Equals': ['f', '6'] },
{ 'Fn::Equals': ['g', '7'] },
{ 'Fn::Equals': ['h', '8'] },
{ 'Fn::Equals': ['i', '9'] },
{ 'Fn::Equals': ['j', '10'] },
],
},
{
'Fn::Equals': ['k', '11'],
},
],
},
},
});
});

test('condition length is more than 10 in Fn.conditionAnd', () => {
// GIVEN
const stack = new cdk.Stack();
const expression = cdk.Fn.conditionAnd(
cdk.Fn.conditionEquals('a', '1'),
cdk.Fn.conditionEquals('b', '2'),
cdk.Fn.conditionEquals('c', '3'),
cdk.Fn.conditionEquals('d', '4'),
cdk.Fn.conditionEquals('e', '5'),
cdk.Fn.conditionEquals('f', '6'),
cdk.Fn.conditionEquals('g', '7'),
cdk.Fn.conditionEquals('h', '8'),
cdk.Fn.conditionEquals('i', '9'),
cdk.Fn.conditionEquals('j', '10'),
cdk.Fn.conditionEquals('k', '11'),
cdk.Fn.conditionEquals('l', '12'),
);

// WHEN
new cdk.CfnCondition(stack, 'Condition', { expression });

// THEN
expect(toCloudFormation(stack)).toEqual({
Conditions: {
Condition: {
'Fn::And': [
{
'Fn::And': [
{ 'Fn::Equals': ['a', '1'] },
{ 'Fn::Equals': ['b', '2'] },
{ 'Fn::Equals': ['c', '3'] },
{ 'Fn::Equals': ['d', '4'] },
{ 'Fn::Equals': ['e', '5'] },
{ 'Fn::Equals': ['f', '6'] },
{ 'Fn::Equals': ['g', '7'] },
{ 'Fn::Equals': ['h', '8'] },
{ 'Fn::Equals': ['i', '9'] },
{ 'Fn::Equals': ['j', '10'] },
],
},
{
'Fn::And': [
{ 'Fn::Equals': ['k', '11'] },
{ 'Fn::Equals': ['l', '12'] },
],
},
],
},
},
});
});

test('condition length is 10n + 1 in Fn.conditionOr', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit 52f2d4a

Please sign in to comment.