Skip to content

Commit

Permalink
feat(apigateway): configure RequestValidatorOptions instead of passin…
Browse files Browse the repository at this point in the history
…g RequestValidator object in addMedthod fixes #6193
  • Loading branch information
pankajy93 committed Mar 18, 2020
1 parent 0399ffc commit 4f31455
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
10 changes: 8 additions & 2 deletions packages/@aws-cdk/aws-apigateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,13 @@ resource.addMethod('GET', integration, {
'method.request.querystring.who': true
},
// We need to set the validator for ensuring they are passed
requestValidator: validator,
requestValidator: validator, // requestValidator is deprecated, Use `reqValidator`
// We can pass request validator's properties
reqValidator: {
requestValidatorName: 'test-validator',
validateRequestBody: true,
validateRequestParameters: false
}
methodResponses: [
{
// Successful response from the integration
Expand All @@ -321,7 +327,7 @@ resource.addMethod('GET', integration, {
},
// Validate the schema on the response
responseModels: {
'application/json': responseModel
'application/json': resp onseModel
}
},
{
Expand Down
25 changes: 23 additions & 2 deletions packages/@aws-cdk/aws-apigateway/lib/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ConnectionType, Integration } from './integration';
import { MockIntegration } from './integrations/mock';
import { MethodResponse } from './methodresponse';
import { IModel } from './model';
import { IRequestValidator } from './requestvalidator';
import { IRequestValidator, RequestValidatorOptions, RequestValidator } from './requestvalidator';
import { IResource } from './resource';
import { RestApi } from './restapi';
import { validateHttpMethod } from './util';
Expand Down Expand Up @@ -73,6 +73,7 @@ export interface MethodOptions {

/**
* The ID of the associated request validator.
* @deprecated Use `reqValidator`
*/
readonly requestValidator?: IRequestValidator;

Expand All @@ -83,6 +84,12 @@ export interface MethodOptions {
* @default - no authorization scopes
*/
readonly authorizationScopes?: string[]

/**
* Request validator options
* @default
*/
readonly reqValidator?: RequestValidatorOptions;
}

export interface MethodProps {
Expand Down Expand Up @@ -160,7 +167,7 @@ export class Method extends Resource {
integration: this.renderIntegration(props.integration),
methodResponses: this.renderMethodResponses(options.methodResponses),
requestModels: this.renderRequestModels(options.requestModels),
requestValidatorId: options.requestValidator ? options.requestValidator.requestValidatorId : undefined,
requestValidatorId: this.requestValidatorId(options),
authorizationScopes: options.authorizationScopes ?? defaultMethodOptions.authorizationScopes,
};

Expand Down Expand Up @@ -302,6 +309,20 @@ export class Method extends Resource {

return models;
}

private requestValidatorId(options: MethodOptions): string | undefined {
if (!options.requestValidator && !options.reqValidator) {
return undefined;
}

if (options.reqValidator) {
const validator = this.restApi.addRequestValidator('validator', options.reqValidator);
return validator.requestValidatorId;
}

//For backward compatibility
return options.requestValidator.requestValidatorId;
}
}

export enum AuthorizationType {
Expand Down
48 changes: 48 additions & 0 deletions packages/@aws-cdk/aws-apigateway/test/test.method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,54 @@ export = {
AuthorizationScopes: ABSENT
}));

test.done();
},

'method has a request validator with provided properties'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const api = new apigw.RestApi(stack, 'test-api', { deploy: false });

// WHEN
new apigw.Method(stack, 'method-man', {
httpMethod: 'GET',
resource: api.root,
options: {
reqValidator: {
requestValidatorName: 'test-validator',
validateRequestBody: true,
validateRequestParameters: false
}
}
});

// THEN
expect(stack).to(haveResource('AWS::ApiGateway::RequestValidator', {
RestApiId: { Ref: stack.getLogicalId(api.node.findChild('Resource') as cdk.CfnElement) },
ValidateRequestBody: true,
ValidateRequestParameters: false,
Name: 'test-validator'
}));

test.done();
},

'method do not have a request validator'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const api = new apigw.RestApi(stack, 'test-api', { deploy: false });

// WHEN
new apigw.Method(stack, 'method-man', {
httpMethod: 'GET',
resource: api.root
});

// THEN
expect(stack).to(haveResource('AWS::ApiGateway::Method', {
RequestValidatorId: ABSENT
}));

test.done();
}
};

0 comments on commit 4f31455

Please sign in to comment.