首页 > 解决方案 > API Gateway 请求验证器是否支持格式属性?

问题描述

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

  1. 使用以下招摇创建一个新的 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
  1. 为新创建的员工资源设置集成方式,选择模拟集成。

  2. 使用以下请求正文测试员工 POST 方法:

{
    "id": 1,
    "phoneNumbers": [
        {
            "number": "1234567890"
        }
    ],
    "salary": 45000
}

请求验证将通过此请求正文成功

  1. 使用以下请求正文测试员工 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 和双格式属性是已知问题:https ://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-known-issues.html#api-gateway-known-issues -rest-apis

但是,我在格式属性中使用正则表达式也遇到了这些问题。这在已知问题中没有具体提及,因此仍在寻找相关信息。

标签: amazon-web-servicesaws-api-gateway

解决方案


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

有时可能令人困惑的是import操作。当使用 OpenAPI 定义您的 API 并将其导入时,API Gateway 最终会解析模型的 OAS 规范和 JSONSchema 草案 4的交集。

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


推荐阅读