serverless: Query string does not honor required/not required

This is a Bug Report

Description

  • What went wrong?

I created a query string parameter set to true, which marks it as required. However, nothing happens when it’s not passed.

  • What did you expect should have happened?

I expect to see an error when the parameter is missing.

  • What was the config you used?

See below for serverless.yml.

  • What stacktrace or error message from your provider did you see?

None. Expected to see an error when query string parameter is missing.


I want my request to have a certain query string parameter. I saw the following here:

To pass optional and required parameters to your functions, so you can use them in API Gateway tests and SDK generation, marking them as true will make them required, false will make them optional.

However, when marking a query string as true does not seem to make it required. When using the following code (clean start from serverless create --template aws-nodejs-typescript), all requests pass to the hello path, even if the query string is not set.

serverless.yml:

service:
  name: example-test

plugins:
  - serverless-webpack
  - serverless-offline

provider:
  name: aws
  runtime: nodejs6.10

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          method: get
          path: hello
          request:
            parameters:
              querystrings:
                url: true

Similar or dependent issues:

Additional Data

  • Serverless Framework Version you’re using: 1.27.3
  • Operating System: Ubuntu 18.04 LTS
  • Stack Trace: N/A
  • Provider Error messages: N/A

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 25
  • Comments: 18 (1 by maintainers)

Commits related to this issue

Most upvoted comments

If you set params as required via serverless syntax you then can just also activate the validator for parameters like so

Resources:
  ParameterRequestValidator:
    Type: AWS::ApiGateway::RequestValidator
    Properties:
      Name: ParameterRequestValidator
      RestApiId:
        Ref: ApiGatewayRestApi
      ValidateRequestBody: false
      ValidateRequestParameters: true

  ApiGatewayMethodNameOfYourApiLookItUpInYourTemplate:
    Properties:
      RequestValidatorId:
        Ref: ParameterRequestValidator

Here is the working template:

In serverless.yml add this

   plugins:
       - serverless-reqvalidator-plugin
   functions:
       events:
          -http:
            path: hello
            method: get
            reqValidatorName: ParameterRequestValidator
    resources:

     Resources:
         ParameterRequestValidator:
            Type: AWS::ApiGateway::RequestValidator
            Properties:
                   Name: ParameterRequestValidator
                   RestApiId:
                         Ref: ApiGatewayRestApi
                   ValidateRequestBody: false
                   ValidateRequestParameters: true

Is there any update on this? I am facing the same issue.

I literally gave a full explaination right above your comment

Oookay I got it! It should be: ApiGatewayMethodFooGet, where foo is my handler name and get is the HTTP method I’m handling.

@akleiber thank you 🙏