Skip to content

Commit 4f31455

Browse files
committed
feat(apigateway): configure RequestValidatorOptions instead of passing RequestValidator object in addMedthod fixes #6193
1 parent 0399ffc commit 4f31455

File tree

3 files changed

+79
-4
lines changed

3 files changed

+79
-4
lines changed

packages/@aws-cdk/aws-apigateway/README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,13 @@ resource.addMethod('GET', integration, {
308308
'method.request.querystring.who': true
309309
},
310310
// We need to set the validator for ensuring they are passed
311-
requestValidator: validator,
311+
requestValidator: validator, // requestValidator is deprecated, Use `reqValidator`
312+
// We can pass request validator's properties
313+
reqValidator: {
314+
requestValidatorName: 'test-validator',
315+
validateRequestBody: true,
316+
validateRequestParameters: false
317+
}
312318
methodResponses: [
313319
{
314320
// Successful response from the integration
@@ -321,7 +327,7 @@ resource.addMethod('GET', integration, {
321327
},
322328
// Validate the schema on the response
323329
responseModels: {
324-
'application/json': responseModel
330+
'application/json': resp onseModel
325331
}
326332
},
327333
{

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

+23-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ConnectionType, Integration } from './integration';
55
import { MockIntegration } from './integrations/mock';
66
import { MethodResponse } from './methodresponse';
77
import { IModel } from './model';
8-
import { IRequestValidator } from './requestvalidator';
8+
import { IRequestValidator, RequestValidatorOptions, RequestValidator } from './requestvalidator';
99
import { IResource } from './resource';
1010
import { RestApi } from './restapi';
1111
import { validateHttpMethod } from './util';
@@ -73,6 +73,7 @@ export interface MethodOptions {
7373

7474
/**
7575
* The ID of the associated request validator.
76+
* @deprecated Use `reqValidator`
7677
*/
7778
readonly requestValidator?: IRequestValidator;
7879

@@ -83,6 +84,12 @@ export interface MethodOptions {
8384
* @default - no authorization scopes
8485
*/
8586
readonly authorizationScopes?: string[]
87+
88+
/**
89+
* Request validator options
90+
* @default
91+
*/
92+
readonly reqValidator?: RequestValidatorOptions;
8693
}
8794

8895
export interface MethodProps {
@@ -160,7 +167,7 @@ export class Method extends Resource {
160167
integration: this.renderIntegration(props.integration),
161168
methodResponses: this.renderMethodResponses(options.methodResponses),
162169
requestModels: this.renderRequestModels(options.requestModels),
163-
requestValidatorId: options.requestValidator ? options.requestValidator.requestValidatorId : undefined,
170+
requestValidatorId: this.requestValidatorId(options),
164171
authorizationScopes: options.authorizationScopes ?? defaultMethodOptions.authorizationScopes,
165172
};
166173

@@ -302,6 +309,20 @@ export class Method extends Resource {
302309

303310
return models;
304311
}
312+
313+
private requestValidatorId(options: MethodOptions): string | undefined {
314+
if (!options.requestValidator && !options.reqValidator) {
315+
return undefined;
316+
}
317+
318+
if (options.reqValidator) {
319+
const validator = this.restApi.addRequestValidator('validator', options.reqValidator);
320+
return validator.requestValidatorId;
321+
}
322+
323+
//For backward compatibility
324+
return options.requestValidator.requestValidatorId;
325+
}
305326
}
306327

307328
export enum AuthorizationType {

packages/@aws-cdk/aws-apigateway/test/test.method.ts

+48
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,54 @@ export = {
802802
AuthorizationScopes: ABSENT
803803
}));
804804

805+
test.done();
806+
},
807+
808+
'method has a request validator with provided properties'(test: Test) {
809+
// GIVEN
810+
const stack = new cdk.Stack();
811+
const api = new apigw.RestApi(stack, 'test-api', { deploy: false });
812+
813+
// WHEN
814+
new apigw.Method(stack, 'method-man', {
815+
httpMethod: 'GET',
816+
resource: api.root,
817+
options: {
818+
reqValidator: {
819+
requestValidatorName: 'test-validator',
820+
validateRequestBody: true,
821+
validateRequestParameters: false
822+
}
823+
}
824+
});
825+
826+
// THEN
827+
expect(stack).to(haveResource('AWS::ApiGateway::RequestValidator', {
828+
RestApiId: { Ref: stack.getLogicalId(api.node.findChild('Resource') as cdk.CfnElement) },
829+
ValidateRequestBody: true,
830+
ValidateRequestParameters: false,
831+
Name: 'test-validator'
832+
}));
833+
834+
test.done();
835+
},
836+
837+
'method do not have a request validator'(test: Test) {
838+
// GIVEN
839+
const stack = new cdk.Stack();
840+
const api = new apigw.RestApi(stack, 'test-api', { deploy: false });
841+
842+
// WHEN
843+
new apigw.Method(stack, 'method-man', {
844+
httpMethod: 'GET',
845+
resource: api.root
846+
});
847+
848+
// THEN
849+
expect(stack).to(haveResource('AWS::ApiGateway::Method', {
850+
RequestValidatorId: ABSENT
851+
}));
852+
805853
test.done();
806854
}
807855
};

0 commit comments

Comments
 (0)