Ray*_*oyd 7 amazon-web-services aws-api-gateway
我已经在我的 API Gateway swagger 文件 (OAS 3.0) 中添加了一个请求验证器。当我通过传入无效的请求正文来测试验证时,我收到的错误消息包含我不理解的错误。重现步骤如下。
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
Run Code Online (Sandbox Code Playgroud)
为新创建的员工资源设置集成方法,选择模拟集成。
使用以下请求正文测试员工 POST 方法:
{
"id": 1,
"phoneNumbers": [
{
"number": "1234567890"
}
],
"salary": 45000
}
Run Code Online (Sandbox Code Playgroud)
请求验证将通过此请求正文成功
{
"id": "1",
"phoneNumbers": [
{
"number": "1234567890"
}
],
"salary": 45000
}
Run Code Online (Sandbox Code Playgroud)
您现在将看到以下请求验证错误:
{
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]
}
Run Code Online (Sandbox Code Playgroud)
您可以看到此消息包含正确的错误,指出字符串 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
但是,我在 format 属性中使用正则表达式时也遇到了这些问题。这在已知问题中没有特别提到,所以仍在寻找有关的信息。
我认为重要的是要注意 OAS 文档中定义的模型应该是JSONSchema,而不一定是 OpenAPI 。format它们在运行时作为 JSONSchema Draft 4 进行验证,该草案在规范中不包含属性。
有时令人困惑的是import操作。当使用 OpenAPI 定义 API 并导入它时,API Gateway 最终会解析OAS 模型规范和 JSONSchema 草案 4 的交集。
如果您需要的 JSONSchema 属性未包含在 OpenAPI 的 Schema 规范中(例如type: [..., null]),则直接创建或更新 API Gateway ::Model 是一种解决方法。
| 归档时间: |
|
| 查看次数: |
850 次 |
| 最近记录: |