Support Request Validator to the method on API Event
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..."
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 this has been raised before. I opened issue #1232 and it was quickly closed in favor of #931
+1
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 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?~
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 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.
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.
@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.
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
GenerateValidatorsinside AWS::Serverless::APIGeneratedValidatorinside 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
Still need "Request Validator" parameter in SAM.
+1 for "Request Validator"
Still need "Request Validator" parameter in SAM.
+1
Still need "Request Validator" parameter in SAM.
+++
Still need "Request Validator" parameter in SAM.
+1
Still need "Request Validator" parameter in SAM.
+1
++
+1
+1
Hi! Is there any ETA?