使用请求验证模型的无服务器 Lambda 代理集成 - 如何获得详细的验证错误响应

Serverless Lambda Proxy Integration using request validation model - how to get detailed validation error response

我正在通过模型验证请求的负载 - 请参阅下面的 serverless.yml 摘录:

functions:
  authorizer:
    handler: src/authorization/authorizer.general
  activity:
    handler: src/resources/activity.submit
    events:
    - http:
        path: /tenant/{tenant}/activities
        method: POST
        cors: true
        authorizer: ${self:custom.authorizer.general}
        request:
          schema:
            application/json: ${file(models/activity.json)

一切都按预期工作,但与架构不匹配的负载的验证响应是:

{
    "message": "Invalid request body"
}

我已尝试添加响应模板,但收到一条警告,提示 'response' 将被删除(这是 lambda 代理集成,而不是 lambda 集成)。

如何让 API 网关在这种情况下给出详细的验证错误消息响应(即 Lambda 代理集成)?

任何帮助将不胜感激,因为我在网上找不到任何相关内容。

我通过添加网关响应来覆盖默认的 Bad Request Body 响应找到了解决方案。不幸的是,从 Serverless 1.50.0 开始,这并不直接受支持(参见 github issue

但能够通过无服务器资源添加网关响应,即 Cloudformation(见上文 link):

resources:
  Resources:
    ApiGatewayRestApi:
      Type: AWS::ApiGateway::RestApi
      Properties:
        Name: ${self:provider.stage}-${self:service}
    GatewayResponseResourceNotFound:
      Type: 'AWS::ApiGateway::GatewayResponse'
      Properties:
        RestApiId:
          Ref: 'ApiGatewayRestApi'
        ResponseType: BAD_REQUEST_BODY
        "StatusCode" : "422"
        ResponseTemplates:
          application/json: "{\"message\": \"$context.error.message\", \"error\": \"$context.error.validationErrorString\"}"

注意:我无法找到一种方法来指示哪个 属性 验证失败,但至少验证消息更详细一些