【发布时间】:2020-04-22 05:09:29
【问题描述】:
我已将请求验证器添加到我的 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
但是,我在格式属性中使用正则表达式时也遇到了这些问题。这在已知问题中没有具体提及,因此仍在寻找相关信息。
【问题讨论】:
-
这是一个非常有趣的发现。我不确定您为什么在请求验证中看到有关 OAS 本身的错误。
标签: amazon-web-services aws-api-gateway