gpt4 book ai didi

amazon-web-services - API Gateway 请求验证器是否支持格式属性?

转载 作者:行者123 更新时间:2023-12-04 08:18:55 25 4
gpt4 key购买 nike

我已经在我的 API Gateway swagger 文件 (OAS 3.0) 中添加了一个请求验证器。当我通过传入无效的请求正文来测试验证时,我收到的错误消息包含我不理解的错误。重现步骤如下。

  • 使用以下招摇创建一个新的 api 网关:
  • openapi: 3.0.0
    info:
    version: "1"
    title: Request Validation Example
    description: |
    ## Request Validation
    Minimal swagger to reproduce request validation errors.

    x-amazon-apigateway-request-validators:
    all:
    validateRequestBody: true
    validateRequestParameters: true
    x-amazon-apigateway-gateway-responses:
    BAD_REQUEST_BODY:
    statusCode: 400
    responseTemplates:
    application/json: |
    {
    message: $context.error.messageString
    errors: $context.error.validationErrorString
    }
    paths:
    /employee:
    post:
    x-amazon-apigateway-request-validator: all
    summary: Create a new Employee
    operationId: CreateEmployee
    requestBody:
    content:
    application/json:
    schema:
    $ref: "#/components/schemas/Employee"
    required: true
    responses:
    "201":
    description: Created
    $ref: "#/components/responses/200"
    components:
    responses:
    "200":
    description: Success
    schemas:
    Employee:
    type: object
    properties:
    id:
    type: integer
    format: int32
    phoneNumbers:
    type: array
    items:
    $ref: "#/components/schemas/PhoneNumber"
    salary:
    type: number
    format: double
    required:
    - phoneNumbers
    - salary
    PhoneNumber:
    type: object
    properties:
    number:
    type: string
    required:
    - number
  • 为新创建的员工资源设置集成方式,选择模拟集成。
  • 使用以下请求正文测试员工 POST 方法:
  • {
    "id": 1,
    "phoneNumbers": [
    {
    "number": "1234567890"
    }
    ],
    "salary": 45000
    }

    请求验证将通过此请求正文成功
  • 使用以下请求正文测试员工 POST 方法:
  • {
    "id": "1",
    "phoneNumbers": [
    {
    "number": "1234567890"
    }
    ],
    "salary": 45000
    }

    您现在将看到以下请求验证错误:
    {
    message: "Invalid request body"
    errors: [instance type (string) does not match any allowed primitive type (allowed: [\"integer\"]), format attribute \"double\" not supported, format attribute \"int32\" not supported]
    }

    您可以看到此消息包含正确的错误,指出字符串 id 与类型整数不匹配。您还会看到有关不支持格式属性 double 和 int32 的错误,这些是我不明白的错误。据我所知,OAS 2.0 和 3.0 支持 double 和 int32 格式属性。 API Gateway 请求验证器是否支持 double 和 int32 格式属性?请求验证器是否在我的 swagger 定义中配置不正确?

    编辑 :
    看来 int32 和 double 格式属性是已知问题: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-known-issues.html#api-gateway-known-issues-rest-apis

    但是,我在 format 属性中使用正则表达式时也遇到了这些问题。这在已知问题中没有特别提到,所以仍在寻找相关信息。

    最佳答案

    我认为重要的是要注意 OAS 文档中定义的模型应该是 JSONSchema ,不一定是OpenAPI。它们在运行时被验证为 JSONSchema Draft 4,其中不包括 format规范中的属性。

    有时令人困惑的是 import手术。当使用 OpenAPI 定义您的 API 并导入它时,API Gateway 最终会解析 路口模型的 OAS 规范和 JSONSchema 草案 4。

    如果您需要 JSONSchema 的一个属性,而该属性未包含在 OpenAPI 的 Schema 规范中(例如 type: [..., null] ),那么直接创建或更新 API Gateway::Model 是一种解决方法。

    关于amazon-web-services - API Gateway 请求验证器是否支持格式属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59581914/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com