connexion
connexion copied to clipboard
In some cases, the connexion sends an incorrect Content-Type in the response header
Description and actual behaviour
I was faced with a situation where it is necessary for the server to send data of different types for one route. An example of such an OpenAPI specification:
responses:
'200':
description: Return html content
content:
text/html:
schema:
type: string
default:
description: Unexpected error
content:
text/plain:
schema:
type: string
But in the response header, I always got Content-Type == application / json. At the same time, it was not possible to change the Content-Type even passing the necessary header as the third argument in the controller. I demonstrated this situation in the Pull Request #834.
Additional info:
The following lines are present in the source code of the Connexion. File: "connexion/operations/abstract.py"
def get_mimetype(self):
if all_json(self.produces):
try:
return self.produces[0]
except IndexError:
return DEFAULT_MIMETYPE
elif len(self.produces) == 1:
return self.produces[0]
else:
return DEFAULT_MIMETYPE
In our case: self.produces == ['text/html', 'text/plain'] and the last branch is triggered in the above quoted code.
python --version3.7.0pip show connexion | grep "^Version\:"2018.0.dev1
I faced a similar issue with the following specification:
responses:
200:
description: 'Return Notice'
content:
application/pdf:
schema:
type: string
format: binary
404:
description: Notice does not exist
content:
application/json:
schema:
type: string
when application/json is set as a content type for the 404, connexion try to serialize the response and asume content type is json even if a 200 is returned.
return request.content, 200
return request.content, 200, {'Content-Type': 'application/pdf'}
TypeError: Object of type bytes is not JSON serializable
If the 404 content is not set, It is possible to return the pdf but when a 404 is returned the content type is still pdf.
as a workaround, I was able to manually craft the response:
return ConnexionResponse(body={'error':'Not found'} ,status_code=404, content_type='application/json')
# or
return ConnexionResponse(body=request.content ,status_code=200, content_type='application/pdf')
did anyone ever manage to return some html? js and css work ok, just not text types
Also having the same issue, is this going to be fixed soon?
Fixed in https://github.com/spec-first/connexion/pull/1591