Is there a way to describe two different response types in OpenAPI 3.0?

流过昼夜 提交于 2019-12-11 03:09:06

问题


What I'd like to do is specify that sometimes the response to an API call might be a PDF document, and sometimes it will be JSON. I'd like to do this in OpenAPI 3.0 format. In the case of a PDF, the response would look like this:

      responses:
        '200':
          description: An invoice in PDF format.
          content:
            application/pdf:
              schema:
                type: string
                format: binary

And in the case of a JSON response, this would describe the response:

      responses:
        '200':
          description: A JSON object containing user name and avatar
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Invoice" 

The OAS3 documentation (https://swagger.io/docs/specification/describing-responses/) provides the following example for how to specify that one of a few different JSON schemas could be the response to a particular API call. This is almost what I want, except instead of describing different possible JSON schemas, I'd like to specify different possible content types, as described above. Is there a way to do this in OAS3 format?

      responses:
        '200':
          description: A JSON object containing pet information
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/Cat'
                  - $ref: '#/components/schemas/Dog'
                  - $ref: '#/components/schemas/Hamster'

回答1:


Just found that this works:

responses:
    '200':
      description: "An invoice."
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/Invoice"
        application/pdf:
          schema:
            type: "string"
            format: "binary"

See the "Response Media Types" section here: https://swagger.io/docs/specification/describing-responses/



来源:https://stackoverflow.com/questions/50397427/is-there-a-way-to-describe-two-different-response-types-in-openapi-3-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!