我如何使用 Mule 4.4 运行时验证 OpenAPI 3.0 中的 JSON 有效载荷
how do I validate a JSON payload in OpenAPI 3.0 with Mule 4.4 Runtime
我需要使用 openapi: 3.0.0 开发一个 Mule API ( 4.4 Runtime )
端点是一个 POST,具有以下请求负载:
{
"Employee": {
"Address": {
"City": "a",
"Country": "aaa"
}
}
}
这是 OpenAPI 3.0 规范的相关部分:
openapi: "3.0.0"
paths:
/search:
post:
tags:
- SearchUser
summary: Search for Users
operationId: getUser
requestBody:
description: User Request Object
content:
application/json:
schema:
$ref: '#/components/schemas/RequestComp'
required: true
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/ResponseComp'
'400':
description: Bad request
content: {}
'404':
description: User not found
content: {}
'405':
description: Validation exception
content: {}
components:
schemas:
RequestComp:
type: object
properties:
Employee:
$ref: '#/components/schemas/EmployeeComp'
EmployeeComp:
type: object
properties:
Address:
$ref: '#/components/schemas/AddressComp'
AddressComp:
type: object
properties:
City:
type: string
required: true
nullable: false
minLength: 1
Country:
type: string
required: true
nullable: false
minLength: 1
ResponseComp:
type: object
properties:
City:
type: string
Country:
type: string
所以我可以验证 'City' 和 'Country' 等单个元素不为 null 但如何防止以下请求? (目前它没有被标记为无效:)
{
"Address": {
"City": "a",
"Country": "aaa"
}
}
您可以将 Employee
包装器定义为必需的 属性,也可以通过添加 additionalProperties: false
来禁止未知属性。请注意,required
是 ,但是 object-level 属性 - 它是必需属性的列表。
components:
schemas:
RequestComp:
type: object
required: [Employee] # <-----
properties:
Employee:
$ref: '#/components/schemas/EmployeeComp'
additionalProperties: false # <-----
EmployeeComp:
type: object
properties:
Address:
$ref: '#/components/schemas/AddressComp'
additionalProperties: false # <-----
AddressComp:
type: object
required: [City, Country] # <-----
properties:
City:
type: string
# required: true # <-- remove this
nullable: false
minLength: 1
Country:
type: string
# required: true # <-- remove this
nullable: false
minLength: 1
additionalProperties: false # <-----
我需要使用 openapi: 3.0.0 开发一个 Mule API ( 4.4 Runtime ) 端点是一个 POST,具有以下请求负载:
{
"Employee": {
"Address": {
"City": "a",
"Country": "aaa"
}
}
}
这是 OpenAPI 3.0 规范的相关部分:
openapi: "3.0.0"
paths:
/search:
post:
tags:
- SearchUser
summary: Search for Users
operationId: getUser
requestBody:
description: User Request Object
content:
application/json:
schema:
$ref: '#/components/schemas/RequestComp'
required: true
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/ResponseComp'
'400':
description: Bad request
content: {}
'404':
description: User not found
content: {}
'405':
description: Validation exception
content: {}
components:
schemas:
RequestComp:
type: object
properties:
Employee:
$ref: '#/components/schemas/EmployeeComp'
EmployeeComp:
type: object
properties:
Address:
$ref: '#/components/schemas/AddressComp'
AddressComp:
type: object
properties:
City:
type: string
required: true
nullable: false
minLength: 1
Country:
type: string
required: true
nullable: false
minLength: 1
ResponseComp:
type: object
properties:
City:
type: string
Country:
type: string
所以我可以验证 'City' 和 'Country' 等单个元素不为 null 但如何防止以下请求? (目前它没有被标记为无效:)
{
"Address": {
"City": "a",
"Country": "aaa"
}
}
您可以将 Employee
包装器定义为必需的 属性,也可以通过添加 additionalProperties: false
来禁止未知属性。请注意,required
是
components:
schemas:
RequestComp:
type: object
required: [Employee] # <-----
properties:
Employee:
$ref: '#/components/schemas/EmployeeComp'
additionalProperties: false # <-----
EmployeeComp:
type: object
properties:
Address:
$ref: '#/components/schemas/AddressComp'
additionalProperties: false # <-----
AddressComp:
type: object
required: [City, Country] # <-----
properties:
City:
type: string
# required: true # <-- remove this
nullable: false
minLength: 1
Country:
type: string
# required: true # <-- remove this
nullable: false
minLength: 1
additionalProperties: false # <-----