OpenAPI 2.0
OAS2 does not support multiple response schemas per status code. You can only have a single schema, for example, a free-form object (type: object
without properties
).
OpenAPI 3.0
In OAS3 you can use oneOf
to define multiple possible request bodies or response bodies for the same operation:
openapi: 3.0.0
...
paths:
/path:
get:
responses:
'200':
description: Success
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/ResponseOne'
- $ref: '#/components/schemas/ResponseTwo'
However, it's not possible to map specific response schemas to specific parameter values. You'll need to document these specifics verbally in the description
of the response, operation and/or parameter.
Here's a possibly related enhancement request:
Allow operationObject overloading with get-^ post-^ etc
Note for Swagger UI users: As of this writing (December 2018) Swagger UI does not automatically generate examples for oneOf
and anyOf
schemas. You can follow this issue for updates.
As a workaround, you can specify a response example
or examples
manually. Note that using multiple examples
require Swagger UI 3.23.0+ or Swagger Editor 3.6.31+.
responses:
'200':
description: Success
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/ResponseOne'
- $ref: '#/components/schemas/ResponseTwo'
example: # <--------
foo: bar
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…