如何在AWS SAM模板中为AWS :: Serverless :: Api添加请求验证器?

问题描述 投票:0回答:1

我正在尝试使用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上的“请求验证器”下拉菜单,则会看到它。但是,请求验证器不会默认使用我定义的验证器。它只是没有None作为选择的选项

amazon-web-services aws-api-gateway json-schema-validator aws-sam
1个回答
0
投票

我在源代码中搜索了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中添加功能请求

© www.soinside.com 2019 - 2024. All rights reserved.