我从 http 请求中得到以下形式的响应:它是一个未命名数组和对象的数组。对于这种情况,我无法找出正确的 Swagger(Open API)规范。
[
[
{
"prop1": "hello",
"prop2": "hello again"
},
{
"prop1": "bye",
"prop2": "bye again"
}
],
{
"key": 123
}
]
Run Code Online (Sandbox Code Playgroud)
答案取决于您使用的 OpenAPI 版本。
OpenAPI 3.0支持oneOf,因此可以为数组项定义多个模式:
openapi: 3.0.0
...
paths:
/something:
get:
responses:
'200':
description: success
content:
application/json:
schema:
type: array
items:
oneOf: # <---------
- type: array
items:
type: object
properties:
prop1:
type: string
prop2:
type: string
- type: object
properties:
key:
type: integer
required:
- key
Run Code Online (Sandbox Code Playgroud)
OpenAPI 2.0不支持oneOf或混合类型。您最多可以使用无类型模式,这意味着数组项可以是任何东西——对象、数组或原语——但你不能指定确切的类型。
swagger: '2.0'
...
paths:
/:
get:
produces:
- application/json
responses:
'200':
description: success
schema:
type: array
items: {} # <---------
# Example to display in Swagger UI:
example:
- - prop1: hello
prop2: hello again
- prop1: bye
prop2: bye again
- key: 123
Run Code Online (Sandbox Code Playgroud)
amp*_*amp -3
我找到方法了,谢谢:
"responses": {
"200": {
"description": "success",
"schema": {
"type": "array",
"items": [{
"type": "array",
"items": {
"type": "object",
"properties": {
"prop1": {
"type": "string",
},
"prop2": {
"type": "string",
}
}
}
},
{
"type": "object",
"properties": {
"key": {
"type": "number"
}
}
}]
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9625 次 |
| 最近记录: |