connexion icon indicating copy to clipboard operation
connexion copied to clipboard

Impossible to return mime type other than `application/json` if multiple mime types are defined

Open aigielsk opened this issue 3 years ago • 1 comments

We have an API spec in which we have a download operation that is supposed to return application/octet-stream (with status code 200). In case something is wrong with the user input another status codes are defined (400 and 404) that are supposed to return application/json.

This seems impossible to achieve with connexion. It always assumes application/json mime type and attempts to parse the JSON before sending the response.

Some details: The method get_mimetype: https://github.com/spec-first/connexion/blob/ca70b16ff5e5f9c1a321c629db771968d94e8f0d/connexion/operations/abstract.py#L327 first checks if all returned mime types are the same and then (if this is not the case) - it only allows a non-JSON mime type if there is just one type defined:

https://github.com/spec-first/connexion/blob/ca70b16ff5e5f9c1a321c629db771968d94e8f0d/connexion/operations/abstract.py#L339-L340

Otherwise it returns the DEFAULT_MIMETYPE, which is application/json.

It should be possible to define the returned mime type per returned status code.

aigielsk avatar Mar 24 '22 10:03 aigielsk

HI @aigielsk, handling multiple mimetypes on the same endpoint is indeed a current limitation of connexion (see #653). You can work around this by returning a response object with the correct mimetype set.

RobbeSneyders avatar Apr 05 '22 21:04 RobbeSneyders

Fixed in https://github.com/spec-first/connexion/pull/1588

RobbeSneyders avatar Feb 15 '23 17:02 RobbeSneyders