Skip to content

Commit 6804c7c

Browse files
authored
fix(events): restore Match.anyOf support for raw strings (#36908)
### Issue # (if applicable) Closes #36902. ### Reason for this change PR #36602 introduced a regression where `Match.anyOf()` no longer accepts raw strings as arguments. The change assumed all arguments are arrays (results from other Match methods), causing the method to call `.map()` on strings, which fails. Users who previously used `Match.anyOf("string1", "string2")` now get a TypeError. ### Description of changes Modified the `anyOf()` method in `event-pattern.ts` to handle both raw values (strings, numbers) and arrays (results from other Match methods): - Added `Array.isArray()` check to detect input type - Arrays from Match methods are processed with `match.map(Token.asString)` - Raw values are wrapped as single-element arrays with `[Token.asString(match)]` - Added unit tests for raw string inputs and mixed inputs This restores backward compatibility while maintaining the fix from PR #36602 for array inputs. ### Describe any new or updated permissions being added N/A - No IAM permission changes. This is a CDK API fix that does not affect CloudFormation template generation or resource permissions. ### Description of how you validated changes - **Unit tests**: Added 2 new test cases in `matchers.test.ts`: - `anyOf with raw strings` - Tests `Match.anyOf('string1', 'string2')` and single string inputs - `anyOf with mixed inputs` - Tests combinations of raw strings and Match method results - **Existing tests**: All 39 existing tests continue to pass - **Build verification**: Module compiles successfully with no linting errors ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) [comment]: <> (cdk-contribution-power preview) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 9ccb548 commit 6804c7c

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

packages/aws-cdk-lib/aws-events/lib/event-pattern.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,14 @@ export class Match implements IResolvable {
226226
throw new UnscopedValidationError('A list of matchers must contain at least one element.');
227227
}
228228

229-
return matchers.map(match => match.map(Token.asString)).flatMap(a => a);
229+
return matchers.flatMap(match => {
230+
// If it's already an array (from other Match methods), process each element
231+
if (Array.isArray(match)) {
232+
return match.map((m: any) => Token.asString(m));
233+
}
234+
// If it's a raw value (string, number, etc.), wrap it as a single element
235+
return [Token.asString(match)];
236+
});
230237
}
231238

232239
private static anythingButConjunction(filterKey: string, values: string[]): string[] {

packages/aws-cdk-lib/aws-events/test/matchers.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,40 @@ describe(Match, () => {
113113
expect(() => stack.resolve(Match.anyOf())).toThrow(/A list of matchers must contain at least one element/);
114114
});
115115

116+
test('anyOf with raw strings', () => {
117+
// Test raw strings only (regression test for issue #36902)
118+
expect(stack.resolve(Match.anyOf('string1', 'string2'))).toEqual([
119+
'string1',
120+
'string2',
121+
]);
122+
123+
// Test single raw string
124+
expect(stack.resolve(Match.anyOf('single-string'))).toEqual([
125+
'single-string',
126+
]);
127+
});
128+
129+
test('anyOf with mixed inputs', () => {
130+
// Test mixed raw strings and Match method results (regression test for issue #36902)
131+
expect(stack.resolve(Match.anyOf('raw-string', Match.prefix('pre')))).toEqual([
132+
'raw-string',
133+
{ prefix: 'pre' },
134+
]);
135+
136+
// Test mixed with multiple Match methods
137+
expect(stack.resolve(Match.anyOf('string1', Match.prefix('pre'), Match.suffix('suf')))).toEqual([
138+
'string1',
139+
{ prefix: 'pre' },
140+
{ suffix: 'suf' },
141+
]);
142+
143+
// Test mixed with raw string at the end
144+
expect(stack.resolve(Match.anyOf(Match.prefix('pre'), 'raw-string'))).toEqual([
145+
{ prefix: 'pre' },
146+
'raw-string',
147+
]);
148+
});
149+
116150
test('prefix', () => {
117151
expect(stack.resolve(Match.prefix('foo'))).toEqual([
118152
{ prefix: 'foo' },

0 commit comments

Comments
 (0)