How to specify alternative response formats with swagger / OpenAPI?

I have swagger.yaml something like this:

 swagger: "2.0" paths: /something: get: parameters: - name: format in: query type: string pattern: '^(csv|json|xml)$' responses: 200: schema: type: ? 

And I want to return different formats (csv, json, xml) depending on the value of the format request parameter (for example, localhost / api / something? Format = csv).

How can I specify different response formats in the specification?

+6
source share
1 answer

I found a workaround by providing different endpoints:

 swagger: "2.0" paths: /something/json: get: produces: - application/json responses: 200: schema: type: object properties: ... /something/csv: get: produces: - text/csv responses: 200: schema: type: string 

Note the different produces: inside each get , and not one at the top level.

Actual response header for the csv endpoint:

 Content-Length:64 Content-Type:text/csv; charset=utf-8 Date:Fri, 26 Aug 2016 

I also tried adding headers to yaml (right after the code above), but it does not change the actual response header:

  headers: Content-type: type: string description: text/csv; charset=utf-8 Content-Disposition: type: string description: attachment; filename=data.csv 

At any endpoint, I get a console message (I create this using connexion ):

Resource interpreted as Document but transferred with MIME type application/json , or

Resource interpreted as Document but transferred with MIME type text/csv

In addition, csv is interpreted as a download file that does not appear in the browser.

... so I suspect I have not understood yet.

+3
source

All Articles