Skip to content

Commit 9b81662

Browse files
authored
fix(events): event bus name only generated if no props passed (#18153)
Since the `EventBus` resource requires a name, CDK will generate one for you. However, because of the way the logic was written, it would only generate one for you if you did NOT pass a props object. In effect, these two statements would have a different effect: ```ts new EventBus(this, 'Bus'); new EventBus(this, 'Bus', {}); ``` Fix that issue. Fixes #18070. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent e1d7efa commit 9b81662

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

packages/@aws-cdk/aws-events/lib/event-bus.ts

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,18 @@ export class EventBus extends EventBusBase {
239239
});
240240
}
241241

242-
private static eventBusProps(defaultEventBusName: string, props?: EventBusProps) {
243-
if (props) {
244-
const { eventBusName, eventSourceName } = props;
245-
const eventBusNameRegex = /^[\/\.\-_A-Za-z0-9]{1,256}$/;
242+
private static eventBusProps(defaultEventBusName: string, props: EventBusProps = {}) {
243+
const { eventBusName, eventSourceName } = props;
244+
const eventBusNameRegex = /^[\/\.\-_A-Za-z0-9]{1,256}$/;
245+
246+
if (eventBusName !== undefined && eventSourceName !== undefined) {
247+
throw new Error(
248+
'\'eventBusName\' and \'eventSourceName\' cannot both be provided',
249+
);
250+
}
246251

247-
if (eventBusName !== undefined && eventSourceName !== undefined) {
248-
throw new Error(
249-
'\'eventBusName\' and \'eventSourceName\' cannot both be provided',
250-
);
251-
} else if (eventBusName !== undefined && !Token.isUnresolved(eventBusName)) {
252+
if (eventBusName !== undefined) {
253+
if (!Token.isUnresolved(eventBusName)) {
252254
if (eventBusName === 'default') {
253255
throw new Error(
254256
'\'eventBusName\' must not be \'default\'',
@@ -262,24 +264,25 @@ export class EventBus extends EventBusBase {
262264
`'eventBusName' must satisfy: ${eventBusNameRegex}`,
263265
);
264266
}
265-
return { eventBusName };
266-
} else if (eventSourceName !== undefined) {
267-
// Ex: aws.partner/PartnerName/acct1/repo1
268-
const eventSourceNameRegex = /^aws\.partner(\/[\.\-_A-Za-z0-9]+){2,}$/;
269-
if (!eventSourceNameRegex.test(eventSourceName)) {
270-
throw new Error(
271-
`'eventSourceName' must satisfy: ${eventSourceNameRegex}`,
272-
);
273-
} else if (!eventBusNameRegex.test(eventSourceName)) {
274-
throw new Error(
275-
`'eventSourceName' must satisfy: ${eventBusNameRegex}`,
276-
);
277-
}
278-
return { eventBusName: eventSourceName, eventSourceName };
279-
} else {
280-
return { eventBusName: props.eventBusName };
281267
}
268+
return { eventBusName };
282269
}
270+
271+
if (eventSourceName !== undefined) {
272+
// Ex: aws.partner/PartnerName/acct1/repo1
273+
const eventSourceNameRegex = /^aws\.partner(\/[\.\-_A-Za-z0-9]+){2,}$/;
274+
if (!eventSourceNameRegex.test(eventSourceName)) {
275+
throw new Error(
276+
`'eventSourceName' must satisfy: ${eventSourceNameRegex}`,
277+
);
278+
} else if (!eventBusNameRegex.test(eventSourceName)) {
279+
throw new Error(
280+
`'eventSourceName' must satisfy: ${eventBusNameRegex}`,
281+
);
282+
}
283+
return { eventBusName: eventSourceName, eventSourceName };
284+
}
285+
283286
return { eventBusName: defaultEventBusName };
284287
}
285288

packages/@aws-cdk/aws-events/test/event-bus.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,19 @@ describe('event bus', () => {
1616
expect(stack).toHaveResource('AWS::Events::EventBus', {
1717
Name: 'Bus',
1818
});
19+
});
1920

21+
test('default event bus with empty props object', () => {
22+
// GIVEN
23+
const stack = new Stack();
24+
25+
// WHEN
26+
new EventBus(stack, 'Bus', {});
2027

28+
// THEN
29+
expect(stack).toHaveResource('AWS::Events::EventBus', {
30+
Name: 'Bus',
31+
});
2132
});
2233

2334
test('named event bus', () => {

0 commit comments

Comments
 (0)