我正在尝试使用AWS SAM将请求验证器资源链接到SAM模板中的无服务器API。我已经创建了请求验证器,并在其RestApiId中引用了API,但是在AWS控制台中未将验证器设置为API默认验证器选项。
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Discription for the template
Globals:
Function:
Timeout: 30
Resources:
MyAPI:
Type: AWS::Serverless::Api
Properties:
Name: MyAPI
StageName: prod
Auth:
DefaultAuthorizer: MyAuthorizer
Authorizers:
MyAuthorizer:
FunctionPayloadType: REQUEST
FunctionArn: here goes the function Arn
Identity:
Context:
- identity.sourceIp
ReauthorizeEvery: 60
Models:
RequestModel:
$schema: 'http://json-schema.org/draft-04/mySchema#'
type: object
properties:
Format:
type: string
Name:
type: string
minLength: 3
Id:
type: string
required:
- Format
- Id
RequestValidator:
Type: AWS::ApiGateway::RequestValidator
Properties:
Name: RequestValidator
RestApiId: !Ref MyAPI
ValidateRequestBody: true
LambdaFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: NameOfTheFunction
CodeUri: ./src/folder/
Handler: Project::nameSpace.class::Handler
Runtime: dotnetcore2.1
Environment:
Variables:
variableA : value
variableB : value
variableC : value
variableD : value
Events:
ApiEndpoint:
Type: Api
Properties:
RestApiId: !Ref MyAPI
Path: /path
Method: post
RequestValidatorId: !Ref RequestValidator
Auth:
Authorizer: MyAuthorizer
RequestModel:
Model: RequestModel
Required: true
验证器已创建,如果我单击API上的“请求验证器”下拉菜单,则可以看到它。但是,请求验证器不会默认使用我定义的验证器。它只是没有任何内容作为选择的选项
答案 0 :(得分:1)
我在源代码中搜索了api转换函数,没有办法附加请求验证。 SAM将AWS :: Serverless :: Api转换为内联主体swagger / openapi定义,但对于x-amazon-apigateway-request-validator没有任何意义。我不明白为什么只有在api方法中附加架构部分时,才能在lambda事件中定义模型
"paths": {
"/post": {
"post": {
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/user"
}
}
},
"required": true
},
也许在SAM github中添加功能请求