无服务器设置API网关的请求验证器

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

我想通过无服务器设置API网关的请求验证器。我为请求验证程序尝试了两种不同的设置。但是,这两种方法都失败了。我已经总结了自己所做的事情,因此,如果有问题,请告诉我。

  1. 我用swagger(OAS3.0)编写API规范。因此,我尝试使用OAS扩展实现请求验证程序的设置。在使用sls deployswagger.yaml进行serverless.yml之后,x-amazon-apigateway-request-validators中描述的所有验证模式都未添加到“请求验证器”选项中。https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-swagger-extensions-request-validator.html

enter image description here

swagger.yaml在下面:

openapi: 3.0.0

info:
  description: xxx
  version: '0.1'
  title: xxx API
x-amazon-apigateway-request-validators:
  body-only:
    validateRequestBody: true,
    validateRequestParameters: false
  except-body:
    validateRequestBody: false,
    validateRequestParameters: true
  all:
    validateRequestBody: true,
    validateRequestParameters: true
tags:
  - name: auth
    description: xxx
paths:
  /login:
    post:
      tags:
        - auth
      summary: xxx
      description: ''
      x-amazon-apigateway-request-validator: all
      responses:
        '200':
          description: success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AuthResponse'
        '400':
          description: fail
          content:
            application/json
        '401':
          description: fail
          content:
            application/json
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AuthRequest'
        required: true
      x-amazon-apigateway-integration:
        responses:
          default:
            statusCode: "200"
        uri: "arn:aws:apigateway:ap-northeast-1:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-northeast-1:xxxxxxxxxxxx:function:xxx-api-dev-login/invocations"
        passthroughBehavior: "when_no_match"
        httpMethod: "POST"
        contentHandling: "CONVERT_TO_TEXT"
        type: "aws_proxy"

我的serverless.yml在下面:

resources:
  Resources:
    RestApi :
      Type : AWS::ApiGateway::RestApi
      Properties :
        Body : ${file(./swagger.yaml)}
    LoginApiToInvokeLambda:
      Type: AWS::Lambda::Permission
      DependsOn: LoginLambdaFunction
      Properties:
        FunctionName: xxx-ext-api-dev-login
        Action: lambda:InvokeFunction
        Principal: apigateway.amazonaws.com
  1. 我试图使用AWS::ApiGateway::RequestValidator实现请求验证器的设置。在使用上方sls deploy和下方swagger.yaml进行serverless.yml之后,allRequestValidatorAll中描述的severless.yml被添加到了请求验证器选项中。但是,请求验证程序的默认值仍为NONE。enter image description here
resources:
  Resources:
    RestApi :
      Type : AWS::ApiGateway::RestApi
      Properties :
        Body : ${file(./swagger.yaml)}
    LoginApiToInvokeLambda:
      Type: AWS::Lambda::Permission
      DependsOn: LoginLambdaFunction
      Properties:
        FunctionName: xxx-ext-api-dev-login
        Action: lambda:InvokeFunction
        Principal: apigateway.amazonaws.com
    RequestValidatorAll:
      Type: AWS::ApiGateway::RequestValidator
      Properties:
        Name: all
        RestApiId:
          Ref: RestApi
        ValidateRequestBody: true
        ValidateRequestParameters: true
swagger aws-api-gateway serverless-framework serverless aws-serverless
1个回答
-1
投票

我也有同样的问题。我可以验证响应主体,其中无服务器会创建模型

        request:
         schema:
          application/json: ${file(create_request.json)}

但是路径参数无效。如果您没有提供值,您还将获得200的返还]

        request:
         parameters:
          paths:
            id: true

你们能解决这个问题吗?#

如果我手动设置请求验证程序,它将起作用Request Validator

这里是手动调整后的正确行为。Api Gateway checks the path parameter

是否可以通过sls或cloudformation进行设置?

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