我有一个API,它对json有效负载有一些互斥的参数。我想在多个示例中显示这一点,但是yaml文件中的schema
似乎只能生成一个示例。
如果我的输入可以是:
{
"text": "some text"
}
或
{
"list": ["some text", "some more"]
}
但不是
{
"text": "some text",
"list": ["some text", "some more"]
}
在swagger 2.0中如何做到这一点?
像下面这样的模式定义具有误导性
definitions:
MutexSchema:
type: object
properties:
list:
type: array
items:
type: string
example: ["some text", "some more"]
text:
type: string
example: "Some text"
而且似乎您不能指定多个body
选项。显示相互排斥的有效负载及其相应响应的好方法是什么?
发布于 2021-03-06 22:27:39
OpenAPI 2.0不支持互斥属性,但您可以通过将minProperties: 1
和maxProperties: 1
添加到您的架构来模拟这一点。这本质上意味着只能传递text
或list
,但不能同时传递两者。
definitions:
MutexSchema:
type: object
properties:
list:
type: array
items:
type: string
example: ["some text", "some more"]
text:
type: string
example: "Some text"
minProperties: 1 # <--------
maxProperties: 1
显示相互排斥的有效负载及其相应响应的好方法是什么?
迁移到支持用于请求和响应的oneOf
和multiple examples
的OpenAPI 3。请注意,无法将请求和响应示例关联起来,但是可以在description
字段中提供附加信息。
paths:
/something:
post:
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/MutexSchema'
# Request body examples
examples:
text example:
summary: Example with text
value:
text: Some text
list example:
summary: Example with list
value:
list: [some text, some more]
responses:
'200':
description: OK
content:
application/json:
schema:
...
# Response examples
examples:
ex1:
summary: ...
value:
...
ex2:
summary: ...
value:
...
components:
schemas:
MutexSchema:
oneOf:
- $ref: '#/components/schemas/Text'
- $ref: '#/components/schemas/List'
Text:
type: object
required:
- text # <--- Property must be marked as required for oneOf to work
properties:
text:
type: string
example: Some text
additionalProperties: false
List:
type: object
required:
- list # <--- Property must be marked as required for oneOf to work
properties:
list:
type: array
items:
type: string
example: [some text, some more]
additionalProperties: false
https://stackoverflow.com/questions/66500859
复制相似问题