Skip to content

Commit 5b764cc

Browse files
authored
fix(aws-apigateway): api gateway usage plan (#19023)
API Gateway allows decimals and integers for rate limits. This PR fixes an error thrown when entering a decimal. To fix this, I created a new method `validateDouble()` in `utils.ts` based off `validateInteger()` and updated the caller as seen in the commit. This also fixes #18994. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 52128cf commit 5b764cc

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

packages/@aws-cdk/aws-apigateway/lib/usage-plan.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { CfnUsagePlan, CfnUsagePlanKey } from './apigateway.generated';
66
import { Method } from './method';
77
import { IRestApi } from './restapi';
88
import { Stage } from './stage';
9-
import { validateInteger } from './util';
9+
import { validateDouble, validateInteger } from './util';
1010

1111
/**
1212
* Container for defining throttling parameters to API stages or methods.
@@ -316,7 +316,7 @@ export class UsagePlan extends UsagePlanBase {
316316
const burstLimit = props.burstLimit;
317317
validateInteger(burstLimit, 'Throttle burst limit');
318318
const rateLimit = props.rateLimit;
319-
validateInteger(rateLimit, 'Throttle rate limit');
319+
validateDouble(rateLimit, 'Throttle rate limit');
320320

321321
ret = {
322322
burstLimit: burstLimit,

packages/@aws-cdk/aws-apigateway/lib/util.ts

+6
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ export function validateInteger(property: number | undefined, messagePrefix: str
7878
}
7979
}
8080

81+
export function validateDouble(property: number | undefined, messagePrefix: string) {
82+
if (property && isNaN(property) && isNaN(parseFloat(property.toString()))) {
83+
throw new Error(`${messagePrefix} should be an double`);
84+
}
85+
}
86+
8187
export class JsonSchemaMapper {
8288
/**
8389
* Transforms naming of some properties to prefix with a $, where needed

packages/@aws-cdk/aws-apigateway/test/usage-plan.test.ts

+53-2
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ describe('usage plan', () => {
2727
});
2828
});
2929

30-
test('usage plan with throttling limits', () => {
30+
test('usage plan with integer throttling limits', () => {
3131
// GIVEN
3232
const stack = new cdk.Stack();
3333
const api = new apigateway.RestApi(stack, 'my-api', { cloudWatchRole: false, deploy: true, deployOptions: { stageName: 'test' } });
3434
const method: apigateway.Method = api.root.addMethod('GET'); // Need at least one method on the api
3535
const usagePlanName = 'Basic';
36-
const usagePlanDescription = 'Basic Usage Plan with throttling limits';
36+
const usagePlanDescription = 'Basic Usage Plan with integer throttling limits';
3737

3838
// WHEN
3939
new apigateway.UsagePlan(stack, 'my-usage-plan', {
@@ -78,6 +78,57 @@ describe('usage plan', () => {
7878
});
7979
});
8080

81+
test('usage plan with integer and float throttling limits', () => {
82+
// GIVEN
83+
const stack = new cdk.Stack();
84+
const api = new apigateway.RestApi(stack, 'my-api', { cloudWatchRole: false, deploy: true, deployOptions: { stageName: 'test' } });
85+
const method: apigateway.Method = api.root.addMethod('GET'); // Need at least one method on the api
86+
const usagePlanName = 'Basic';
87+
const usagePlanDescription = 'Basic Usage Plan with integer and float throttling limits';
88+
89+
// WHEN
90+
new apigateway.UsagePlan(stack, 'my-usage-plan', {
91+
name: usagePlanName,
92+
description: usagePlanDescription,
93+
apiStages: [
94+
{
95+
stage: api.deploymentStage,
96+
throttle: [
97+
{
98+
method,
99+
throttle: {
100+
burstLimit: 20,
101+
rateLimit: 10.5,
102+
},
103+
},
104+
],
105+
},
106+
],
107+
});
108+
109+
// THEN
110+
Template.fromStack(stack).hasResourceProperties(RESOURCE_TYPE, {
111+
UsagePlanName: usagePlanName,
112+
Description: usagePlanDescription,
113+
ApiStages: [
114+
{
115+
ApiId: {
116+
Ref: 'myapi4C7BF186',
117+
},
118+
Stage: {
119+
Ref: 'myapiDeploymentStagetest4A4AB65E',
120+
},
121+
Throttle: {
122+
'//GET': {
123+
BurstLimit: 20,
124+
RateLimit: 10.5,
125+
},
126+
},
127+
},
128+
],
129+
});
130+
});
131+
81132
test('usage plan with blocked methods', () => {
82133
// GIVEN
83134
const stack = new cdk.Stack();

0 commit comments

Comments
 (0)