开放API 2.0
OAS2 不支持每个状态代码的多个响应模式。您只能有一个模式,例如,一个自由形式的对象(type: object
没有properties
)。
开放API 3.x
在 OAS3 中,您可以使用oneOf
为同一操作定义多个可能的请求主体或响应主体:
openapi: 3.0.0
...
paths:
/path:
get:
responses:
'200':
description: Success
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/ResponseOne'
- $ref: '#/components/schemas/ResponseTwo'
但是,无法将特定响应模式映射到特定参数值。您需要在description
响应、操作和/或参数中口头记录这些细节。
这是一个可能相关的增强请求:
Allow operationObject overloading with get-^ post-^ etc
Swagger UI 用户注意:旧版本的 Swagger UI(3.39.0 版之前)不会自动生成示例oneOf
和anyOf
模式。作为一种解决方法,您可以指定响应example
或examples
手动指定。请注意,使用多个examples
需要 Swagger UI 3.23.0+ 或 Swagger Editor 3.6.31+。
responses:
'200':
description: Success
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/ResponseOne'
- $ref: '#/components/schemas/ResponseTwo'
example: # <--- Workaround for Swagger UI < 3.39.0
foo: bar