aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

Cannot call request validator options multiple times.

Open revanthsamavedam opened this issue 5 years ago • 7 comments
trafficstars

Calling the constructor for RequestValidatorOptions multiple times gives an error

There is already a Construct with name 'validator' in RestApi

Reproduction Steps

`

    resource1.add_method(
        http_method='POST',
        integration=xxxIntegration,
        request_parameters={
            'method.request.header.passcode': True,
            'method.request.header.message': True
        },
        request_validator_options=_apigw.RequestValidatorOptions(validate_request_parameters=True, request_validator_name='POST-r1-validator')
    )

    resource2.add_method(
        http_method='POST',
        integration=yyyIntegration,
        request_parameters={
            'method.request.header.passcode': True,
            'method.request.querystring.id': True
        },
        request_validator_options=_apigw.RequestValidatorOptions(validate_request_parameters=True,request_validator_name='POST-r2-validator') 
    )

`

Error Log

Environment

  • **CLI Version :1.35.1
  • **Framework Version:1.35.1
  • **OS : Mac Mojave
  • **Language : Python

Other


This is :bug: Bug Report

revanthsamavedam avatar Apr 26 '20 23:04 revanthsamavedam

Thanks for filing this @durja. This is, in fact, a bug on the CDK.

You can workaround this by using the requestValidator property.

nija-at avatar May 13 '20 12:05 nija-at

The proposed solution is to remove the requestValidatorOptions property completely, And add in

      const req = new apiGateway.RequestValidator(this, uniqueName, {
        restApi: this._api,
        requestValidatorName: uniqueName,
        validateRequestBody: false,
        validateRequestParameters: true
      })

I can't actually create the new class object without it failing at the above line.

TypeError: Cannot read property 'children' of undefined
    at synthNestedAssemblies (proj\cdk\node_modules\@aws-cdk\core\lib\private\synthesis.ts:50:33)
    at synthNestedAssemblies (proj\cdk\node_modules\@aws-cdk\core\lib\private\synthesis.ts:54:7)
    at synthNestedAssemblies (proj\cdk\node_modules\@aws-cdk\core\lib\private\synthesis.ts:54:7)
    at synthNestedAssemblies (proj\cdk\node_modules\@aws-cdk\core\lib\private\synthesis.ts:54:7)
    at Object.synthesize (proj\cdk\node_modules\@aws-cdk\core\lib\private\synthesis.ts:14:3)
    at App.synth (proj\cdk\node_modules\@aws-cdk\core\lib\stage.ts:188:23)
    at process.App.process.once (proj\cdk\node_modules\@aws-cdk\core\lib\app.ts:123:45)
    at Object.onceWrapper (events.js:277:13)
    at process.emit (events.js:189:13)
    at process.EventEmitter.emit (domain.js:441:20)

monkeytronics avatar Feb 27 '21 21:02 monkeytronics

@monkeytronics changing the implementation throws another error

Caused by: java.lang.NullPointerException: restApi is required

fuzzy28 avatar Nov 01 '21 03:11 fuzzy28

Hi folks, will this be fixed eventually?

EHadoux avatar Jul 06 '22 14:07 EHadoux

I had to remove validation also :(

aokhotnikov avatar Jul 06 '22 15:07 aokhotnikov

Was able to get around this by creating a validator object.

      const queryStringValidator = new apigateway.RequestValidator(
      scope,
      "validator",
      {
        restApi: api,
        requestValidatorName: "validator",
        validateRequestBody: false,
        validateRequestParameters: true,
      }
    );

    resource.addMethod(
      "GET",
      new apigateway.LambdaIntegration(this.lambdaFunc),
      {
        requestParameters: {
          "method.request.querystring.attribute": true,
        },
        requestValidator: queryStringValidator,
      }
    );

oanguin avatar Jul 12 '22 12:07 oanguin

Not the most elegant solution, but here is what I'm using:

const getMethodOptions = (model?: Model): MethodOptions => {
      const methodOptions: MethodOptions = {
        authorizer: tokenAuthorizer,
        requestParameters: {
          "method.request.header.authorization": true,
        },
        requestModels: {},
        // https://github.com/aws/aws-cdk/issues/7613
        requestValidator: new RequestValidator(
          this,
          "AppApiRequestValidator" + this.requestValidatorSuffix,
          {
            restApi: appApi,
            validateRequestParameters: true,
            validateRequestBody: Boolean(model),
          }
        ),
      };
      this.requestValidatorSuffix++;
      if (model) {
        return {
          ...methodOptions,
          requestModels: { "application/json": model },
        };
      }
      return methodOptions;
    };

bestickley avatar Aug 09 '22 19:08 bestickley

Hi there 👋

It has been about over a year and a half since this has been reported, what is the status on this?

Thanks!

therealvio avatar Nov 16 '22 11:11 therealvio

This issue has received a significant amount of attention so we are automatically upgrading its priority. A member of the community will see the re-prioritization and provide an update on the issue.

github-actions[bot] avatar Nov 20 '22 00:11 github-actions[bot]

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

github-actions[bot] avatar Apr 27 '23 12:04 github-actions[bot]