serverless-application-model icon indicating copy to clipboard operation
serverless-application-model copied to clipboard

Support Request Validator to the method on API Event

Open scarlier opened this issue 6 years ago • 97 comments

Description: Defining api model to required=true will not add the Request Validator to the method.

with following sam template and sam cli version 0.40

AWSTemplateFormatVersion : "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"

Globals:
  Function:
    AutoPublishAlias: live
    Runtime: python3.8
    Handler: lambda_handler.handler

Resources:

  restGateway:
    Type: "AWS::Serverless::Api"
    Properties:
      StageName: default
      Models:
        SomeModel:
          type: object
          properties:
            keyname:
              type: string
          required:
            - keyname

  byIamcToken:
    Type: "AWS::Serverless::Function"
    Properties:
      CodeUri: src.zip
      Events:
        HttpGet:
          Type: Api
          Properties:
            Path: '/request-path'
            Method: post
            RestApiId: !Ref restGateway
            RequestModel:
              Model: SomeModel
              Required: true

Observed result: Request Validator in method settings has value "NONE"

Expected result: Request Validator in method settings has value "Validate body..."

scarlier avatar Jan 21 '20 08:01 scarlier

Thanks for the feature request! We have passed this along to our product team for possible prioritization. Please +1 on the feature to help with prioritization.

praneetap avatar Jan 22 '20 19:01 praneetap

@praneetap this has been raised before. I opened issue #1232 and it was quickly closed in favor of #931

brysontyrrell avatar Feb 03 '20 03:02 brysontyrrell

+1

asyba avatar Feb 10 '20 21:02 asyba

A temporary solution could be to add a partial DefinitionBody in your ApiGateway resource :

   DefinitionBody:
        swagger: 2.0
        x-amazon-apigateway-request-validators:
          basic:
            validateRequestBody: true
            validateRequestParameters: true
        x-amazon-apigateway-request-validator: basic
        paths:
          /request-path:
            post:
              x-amazon-apigateway-integration:
                httpMethod: POST
                type: aws_proxy
                uri:
                  Fn::Sub: 'arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${byIamcToken.Arn}/invocations'
         parameters:
              - required: true
                in: body
                name: somemodel
                schema:
                  $ref: '#/definitions/somemodel'

This will add a validator named basic and will add it to every function in your apigateway, thus will enforce validation of the body and parameters (tweak it as you need)

g-pelletier avatar Feb 11 '20 16:02 g-pelletier

~@g-pelletier as I understand you can't do a partial swagger doc. You'll need to define the full API and the links to the Lambda functions as SAM will either generate the swagger doc or it will rely on the one provided if you've set DefinitionBody.~

~I believe in the case of the new HttpApi resource they have designed that to be able to merge source and auto-generated definitions. @praneetap can you verify?~

brysontyrrell avatar Feb 11 '20 17:02 brysontyrrell

You can generate partial swagger doc. I just did in our templates that needed request body verification. What I posted is exactly what I used combined with something similiar as what is posted in the original post.

g-pelletier avatar Feb 11 '20 18:02 g-pelletier

@g-pelletier disregard that last comment. I wasn't thinking. What you posted is what we've had to do for similar workarounds as well. Though I don't think you could really call that a partial swagger doc.

brysontyrrell avatar Feb 11 '20 18:02 brysontyrrell

Sorry, by partial, I meant that you don't need to write the full API code to have it working. SAM will append the other parts, such as Models and Authorizers.

g-pelletier avatar Feb 11 '20 18:02 g-pelletier

@praneetap @keetonian could you provide some info on how this relates to #931 and #1232? Those issues seem to suggest that there is work underway yet this issue seems to be still in triage? I'd avoid the swagger workaround if validation support for AWS::Serverless::Api-Models is in the pipeline.

agostbiro avatar Feb 13 '20 14:02 agostbiro

Hi!

I was a little bit annoyed with the lack of this feature, so I started hacking away a possible solution. I ended up with a version that works locally using cloud formation packaging/deploy, but I am not sure if it would be eligible for a Pull Request. I would appreciate if anyone would give me a feedback whether this would be useful to the general development of the specification.

Basically, I've added properties to the SAM specification

  • GenerateValidators inside AWS::Serverless::API
  • GeneratedValidator inside AWS::Serverless::Function implicit API event source.

The first one basically injects the following on the generated template:

          "x-amazon-apigateway-request-validators": {
            "BODY": {
              "validateRequestParameters": false, 
              "validateRequestBody": true
            }, 
            "FULL": {
              "validateRequestParameters": true, 
              "validateRequestBody": false
            }, 
            "PARAMS": {
              "validateRequestParameters": true, 
              "validateRequestBody": false
            }
          }

Then the second would inject this to the API Method:

                "x-amazon-apigateway-request-validator": "BODY",

A sample template would be:

  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      GenerateValidators: true  # New property
      StageName: Dev
      Models:
        Book:
          required:
          - name
          - author
          type: object
          properties:
            name:
              type: string
            author:
              type: string
              description: author

  CreateBookFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: dynamo/
      Handler: handlers.put_item_handler
      Events:
        HelloWorld:
          Type: Api 
          Properties:
            RestApiId: !Ref MyApi
            Path: /book
            Method: post
            RequestModel:
              Model: Book
              Required: true
            GeneratedValidator: BODY  # New property

paolorechia avatar Mar 31 '20 12:03 paolorechia

Still need "Request Validator" parameter in SAM.

AntonUspishnyi avatar Apr 22 '20 10:04 AntonUspishnyi

+1 for "Request Validator"

ifurs avatar Apr 22 '20 11:04 ifurs

Still need "Request Validator" parameter in SAM.

+1

teapo-t avatar Apr 22 '20 11:04 teapo-t

Still need "Request Validator" parameter in SAM.

+++

Doroschuk avatar Apr 22 '20 11:04 Doroschuk

Still need "Request Validator" parameter in SAM.

+1

Lifeformwp avatar Apr 22 '20 11:04 Lifeformwp

Still need "Request Validator" parameter in SAM.

+1

ShytN1k avatar Apr 22 '20 11:04 ShytN1k

++

AlexOliinyk avatar Apr 22 '20 11:04 AlexOliinyk

+1

pavlo-koval avatar Apr 22 '20 12:04 pavlo-koval

+1

wgfilho avatar Apr 27 '20 11:04 wgfilho

Hi! Is there any ETA?

AntonUspishnyi avatar Apr 27 '20 16:04 AntonUspishnyi