Skip to content

Commit c31f9b4

Browse files
authored
fix(apigateway): enabled property of ApiKeyProps is ignored (#18407)
Using nullish coalescing so that it defaults to `true` only if `enabled` is `null` or `undefined`. Fixes #18402. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 838ec83 commit c31f9b4

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

Diff for: packages/@aws-cdk/aws-apigateway/lib/api-key.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export class ApiKey extends ApiKeyBase {
165165
const resource = new CfnApiKey(this, 'Resource', {
166166
customerId: props.customerId,
167167
description: props.description,
168-
enabled: props.enabled || true,
168+
enabled: props.enabled ?? true,
169169
generateDistinctId: props.generateDistinctId,
170170
name: this.physicalName,
171171
stageKeys: this.renderStageKeys(props.resources),

Diff for: packages/@aws-cdk/aws-apigateway/test/api-key.test.ts

+54-7
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,33 @@ describe('api key', () => {
1717
// should have an api key with no props defined.
1818
});
1919

20+
21+
test('enabled flag is respected', () => {
22+
// GIVEN
23+
const stack = new cdk.Stack();
24+
25+
// WHEN
26+
new apigateway.ApiKey(stack, 'my-api-key', {
27+
enabled: false,
28+
value: 'arandomstringwithmorethantwentycharacters',
29+
});
30+
31+
// THEN
32+
expect(stack).toHaveResource('AWS::ApiGateway::ApiKey', {
33+
Enabled: false,
34+
Value: 'arandomstringwithmorethantwentycharacters',
35+
});
36+
});
37+
38+
2039
test('specify props for apiKey', () => {
2140
// GIVEN
2241
const stack = new cdk.Stack();
23-
const api = new apigateway.RestApi(stack, 'test-api', { cloudWatchRole: false, deploy: true, deployOptions: { stageName: 'test' } });
42+
const api = new apigateway.RestApi(stack, 'test-api', {
43+
cloudWatchRole: false,
44+
deploy: true,
45+
deployOptions: { stageName: 'test' },
46+
});
2447
api.root.addMethod('GET'); // api must have atleast one method.
2548

2649
// WHEN
@@ -61,7 +84,11 @@ describe('api key', () => {
6184
test('use an imported api key', () => {
6285
// GIVEN
6386
const stack = new cdk.Stack();
64-
const api = new apigateway.RestApi(stack, 'test-api', { cloudWatchRole: false, deploy: true, deployOptions: { stageName: 'test' } });
87+
const api = new apigateway.RestApi(stack, 'test-api', {
88+
cloudWatchRole: false,
89+
deploy: true,
90+
deployOptions: { stageName: 'test' },
91+
});
6592
api.root.addMethod('GET'); // api must have atleast one method.
6693

6794
// WHEN
@@ -83,7 +110,11 @@ describe('api key', () => {
83110
// GIVEN
84111
const stack = new cdk.Stack();
85112
const user = new iam.User(stack, 'User');
86-
const api = new apigateway.RestApi(stack, 'test-api', { cloudWatchRole: false, deploy: true, deployOptions: { stageName: 'test' } });
113+
const api = new apigateway.RestApi(stack, 'test-api', {
114+
cloudWatchRole: false,
115+
deploy: true,
116+
deployOptions: { stageName: 'test' },
117+
});
87118
api.root.addMethod('GET'); // api must have atleast one method.
88119

89120
// WHEN
@@ -130,7 +161,11 @@ describe('api key', () => {
130161
// GIVEN
131162
const stack = new cdk.Stack();
132163
const user = new iam.User(stack, 'User');
133-
const api = new apigateway.RestApi(stack, 'test-api', { cloudWatchRole: false, deploy: true, deployOptions: { stageName: 'test' } });
164+
const api = new apigateway.RestApi(stack, 'test-api', {
165+
cloudWatchRole: false,
166+
deploy: true,
167+
deployOptions: { stageName: 'test' },
168+
});
134169
api.root.addMethod('GET'); // api must have atleast one method.
135170

136171
// WHEN
@@ -182,7 +217,11 @@ describe('api key', () => {
182217
// GIVEN
183218
const stack = new cdk.Stack();
184219
const user = new iam.User(stack, 'User');
185-
const api = new apigateway.RestApi(stack, 'test-api', { cloudWatchRole: false, deploy: true, deployOptions: { stageName: 'test' } });
220+
const api = new apigateway.RestApi(stack, 'test-api', {
221+
cloudWatchRole: false,
222+
deploy: true,
223+
deployOptions: { stageName: 'test' },
224+
});
186225
api.root.addMethod('GET'); // api must have atleast one method.
187226

188227
// WHEN
@@ -253,7 +292,11 @@ describe('api key', () => {
253292
test('only api key is created when rate limiting properties are not provided', () => {
254293
// GIVEN
255294
const stack = new cdk.Stack();
256-
const api = new apigateway.RestApi(stack, 'test-api', { cloudWatchRole: false, deploy: true, deployOptions: { stageName: 'test' } });
295+
const api = new apigateway.RestApi(stack, 'test-api', {
296+
cloudWatchRole: false,
297+
deploy: true,
298+
deployOptions: { stageName: 'test' },
299+
});
257300
api.root.addMethod('GET'); // api must have atleast one method.
258301

259302
// WHEN
@@ -281,7 +324,11 @@ describe('api key', () => {
281324
test('api key and usage plan are created and linked when rate limiting properties are provided', () => {
282325
// GIVEN
283326
const stack = new cdk.Stack();
284-
const api = new apigateway.RestApi(stack, 'test-api', { cloudWatchRole: false, deploy: true, deployOptions: { stageName: 'test' } });
327+
const api = new apigateway.RestApi(stack, 'test-api', {
328+
cloudWatchRole: false,
329+
deploy: true,
330+
deployOptions: { stageName: 'test' },
331+
});
285332
api.root.addMethod('GET'); // api must have atleast one method.
286333

287334
// WHEN

0 commit comments

Comments
 (0)