Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot call request validator options multiple times. #7613

Closed
durja opened this issue Apr 26, 2020 · 10 comments · Fixed by #25324
Closed

Cannot call request validator options multiple times. #7613

durja opened this issue Apr 26, 2020 · 10 comments · Fixed by #25324
Assignees
Labels
@aws-cdk/aws-apigateway Related to Amazon API Gateway bug This issue is a bug. effort/small Small work item – less than a day of effort p1

Comments

@durja
Copy link

durja commented Apr 26, 2020

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 Report

@durja durja added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Apr 26, 2020
@SomayaB SomayaB added the @aws-cdk/aws-apigateway Related to Amazon API Gateway label Apr 29, 2020
@nija-at
Copy link
Contributor

nija-at commented May 13, 2020

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 nija-at added p1 and removed needs-triage This issue or PR still needs to be triaged. labels May 13, 2020
@nija-at nija-at added effort/small Small work item – less than a day of effort p2 and removed p1 labels Aug 10, 2020
@monkeytronics
Copy link

monkeytronics commented Feb 27, 2021

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)

@fuzzy28
Copy link

fuzzy28 commented Nov 1, 2021

@monkeytronics changing the implementation throws another error

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

@EHadoux
Copy link

EHadoux commented Jul 6, 2022

Hi folks, will this be fixed eventually?

@aokhotnikov
Copy link

I had to remove validation also :(

@oanguin
Copy link

oanguin commented Jul 12, 2022

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,
      }
    );

@bestickley
Copy link

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;
    };

@therealvio
Copy link

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 added a commit to therealvio-org/game-off-2022 that referenced this issue Nov 17, 2022
NOTE: The requestValidatorOptions had to be broken out into individual
objects. Refer to: aws/aws-cdk#7613
therealvio added a commit to therealvio-org/game-off-2022 that referenced this issue Nov 19, 2022
NOTE: The requestValidatorOptions had to be broken out into individual
objects. Refer to: aws/aws-cdk#7613
@github-actions
Copy link

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 github-actions bot added p1 and removed p2 labels Nov 20, 2022
therealvio pushed a commit to therealvio-org/game-off-2022 that referenced this issue Nov 21, 2022
* feat: Adds formal API path for playerHand
The `playerHand` path has been added to the legalBrawl API.
This will be the destination where POST requests are made by the game
client to submit, and in return receive an opposing player's selected
cards.

A model has been added to ensure that the body's JSON data is valid
before it reaches the Lambda (though this doesn't stop unsanitary
inputs, it's a matter of validation at a schema-level).

* feat: Adds handling for POST reqs to playerHand
A first-cut of handling logic for POST requests to `playerHand` has
been added. Currently, this only *submits* `playerHandInfo` payloads
into the `playerHands` database.
This change provides the capability for an HTTP POST request of a
specific format to be inserted into the `playerHands` dynamoDB database
using the `PutItem` API.

Known Bugs:
- There is currently an unhandled scenario where if an entry already
exists, it should return an error
- When marshalling the Golang struct to a DynamoDB struct using
`MarshalMap`, there doesn't seem to be any indication that the payload
coming in is bogus. For example, a field could be called "hand" rather
than "card". This causes the data for the unmatching field to be "Null".

* refactor: Reorganizes aws package

The `aws` package use by the `legalbrawlapi` module is getting
needlessly deep, and is causing circular dependencies. Because the API
gateway request body is being used by DynamoDB in `PutItem` requests
(among others).

This "flattening" of the structure should better support the
requirements that these two components have.

* refactor: Merges struct types

Throughout the API Gateway and DynamoDB code, there were similarities
that were suitable to merge into common struct types.

The benefit of this is that bespoke "conversion" logic isn't necessary
if HandInfo is a 1:1 to its DynamoDB counterpart. So in theory the
body coming in from the request can immediately be used in the `PutItem`
API call.

This results in (hopefully) maintable code, while also still being
terse.

* refactor: Changes type scopes for aws package to private

* chore: Tweaks HandleRequest function

* doc: Adds docstring to addHand method

* doc: Removes "bug" regarding duplicate hands.

Players may be using their existing hand for matchmaking. Since DynamoDB
doesn't duplicate entries anyway. This isn't a bug as far as we would
be concerned. This may change as requirements evolve.

* feat: Adds POST and GET to playerHands API

* feat: Adds timeout to playerHands GET request

* feat: Adds timeout to playerHands POST request

* feat: Adds PUT method to API

* feat: Adds validation to POST & PUT methods

NOTE: The requestValidatorOptions had to be broken out into individual
objects. Refer to: aws/aws-cdk#7613

* feat: Adds validation to GET method

* feat: Adds API-side validation of GET method

* feat: Adds API-side validation of POST and PUT request

* feat: Adds logic to PUT request

* feat: Adds preexisting player checks for POST request

* chore: Cleans up error codes, and messages for apigateway

* refactor: Removes an unnecessary dDBHandler client creation

* feat: Adds timeout to playerHands PUT request
@corymhall corymhall self-assigned this Apr 26, 2023
@mergify mergify bot closed this as completed in #25324 Apr 27, 2023
mergify bot pushed a commit that referenced this issue Apr 27, 2023
…nder feature flag) (#25324)

When using `addMethod` with `requestValidatorOptions` it is not possible to do this multiple times on the same RestApi. The same node id is used for all `RequestValidators` added using this method.

This PR adds a new feature flag which when enabled will generate a unique id for the RequestValidators added in this way.

fixes #7613

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

⚠️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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment