我有两个模式对象:
'#/ components / schemas / customer' '#/ components / schemas / account'
是否有办法使用开放的API 3.0规范同时定义“#/ components / schemas / customer”和“#/ components / schemas / account”后响应正文
答案 0 :(得分:0)
这取决于用例。
1)如果响应是customer
或account
,请使用oneOf
:
responses:
'200':
description: A `customer` object or an `account` object
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/customer'
- $ref: '#/components/schemas/account'
# Example for Swagger UI - see the note below
example:
foo: bar
针对Swagger UI用户的说明:当前来自oneOf
模式的Swagger UI does not generate examples。解决方法是在example
旁边提供自定义oneOf
。
2)如果响应包含customer
和account
的一组组合属性,请使用allOf
:
responses:
'200':
description: Successful response
content:
application/json:
schema:
$ref: '#/components/schemas/MyResponse'
components:
MyResponse:
allOf:
- $ref: '#/components/schemas/customer'
- $ref: '#/components/schemas/account'
3)如果响应具有两个属性,其中一个是customer
对象,另一个是account
对象,请使用以下命令:
responses:
'200':
description: Successful response
content:
application/json:
schema:
$ref: '#/components/schemas/MyResponse'
components:
MyResponse:
type: object
properties:
customer:
$ref: '#/components/schemas/customer'
account:
$ref: '#/components/schemas/account'