Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adjust _prepare_body interface to simplify improving _serialize_data
Rename _jsonify_data to _serialize_data to make its purpose easier to
understand (this was also known as _cast_body in aiohttp_api).

In exploring how to harmonize json serialization between aiothttp and
flask, we needed to be able to adjust the mimetype from within
_serialize_data. Harmonizing the actual serialization has to wait until
backwards incompatible changes can be made, but we can keep the new
interface, as these functions were introduced in this PR (#849).
  • Loading branch information
cognifloyd committed Dec 7, 2019
commit e57e6a61b4f57a23afb731040735f602043e70f5
9 changes: 5 additions & 4 deletions connexion/apis/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ def _prepare_body_and_status_code(cls, data, mimetype, status_code=None, extra_c
status_code = status_code.value

if data is not None:
body = cls._jsonify_data(data, mimetype)
body, mimetype = cls._serialize_data(data, mimetype)
else:
body = data

Expand All @@ -414,10 +414,11 @@ def _prepare_body_and_status_code(cls, data, mimetype, status_code=None, extra_c
**extra_context
})

return body, status_code
return body, status_code, mimetype

@classmethod
def _jsonify_data(cls, data, mimetype):
def _serialize_data(cls, data, mimetype):
# TODO: Harmonize with flask_api. Currently this is the backwards compatible with aiohttp_api._cast_body.
if not isinstance(data, bytes):
if isinstance(mimetype, str) and is_json_mimetype(mimetype):
body = cls.jsonifier.dumps(data)
Expand All @@ -427,7 +428,7 @@ def _jsonify_data(cls, data, mimetype):
body = str(data)
else:
body = data
return body
return body, mimetype

def json_loads(self, data):
return self.jsonifier.loads(data)
Expand Down
4 changes: 2 additions & 2 deletions connexion/apis/aiohttp_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def _build_response(cls, data, mimetype, content_type=None, headers=None, status
if cls._is_framework_response(data):
raise TypeError("Cannot return web.StreamResponse in tuple. Only raw data can be returned in tuple.")

data, status_code = cls._prepare_body_and_status_code(data=data, mimetype=mimetype, status_code=status_code, extra_context=extra_context)
data, status_code, serialized_mimetype = cls._prepare_body_and_status_code(data=data, mimetype=mimetype, status_code=status_code, extra_context=extra_context)

if isinstance(data, str):
text = data
Expand All @@ -355,7 +355,7 @@ def _build_response(cls, data, mimetype, content_type=None, headers=None, status
text = None
body = data

content_type = content_type or mimetype
content_type = content_type or mimetype or serialized_mimetype
return web.Response(body=body, text=text, headers=headers, status=status_code, content_type=content_type)

@classmethod
Expand Down
13 changes: 7 additions & 6 deletions connexion/apis/flask_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ def _build_response(cls, mimetype, content_type=None, headers=None, status_code=
if cls._is_framework_response(data):
return flask.current_app.make_response((data, status_code, headers))

data, status_code = cls._prepare_body_and_status_code(data=data, mimetype=mimetype, status_code=status_code, extra_context=extra_context)
data, status_code, serialized_mimetype = cls._prepare_body_and_status_code(data=data, mimetype=mimetype, status_code=status_code, extra_context=extra_context)

kwargs = {
'mimetype': mimetype,
'mimetype': mimetype or serialized_mimetype,
'content_type': content_type,
'headers': headers,
'response': data,
Expand All @@ -185,13 +185,14 @@ def _build_response(cls, mimetype, content_type=None, headers=None, status_code=
return flask.current_app.response_class(**kwargs) # type: flask.Response

@classmethod
def _jsonify_data(cls, data, mimetype):
# TODO: to discuss: Using jsonifier for all type of data, even when mimetype is not json is strange. Why ?
def _serialize_data(cls, data, mimetype):
# TODO: harmonize flask and aiohttp serialization when mimetype=None or mimetype is not JSON
# (cases where it might not make sense to jsonify the data)
if (isinstance(mimetype, str) and is_json_mimetype(mimetype)) \
or not (isinstance(data, bytes) or isinstance(data, str)):
return cls.jsonifier.dumps(data)
return cls.jsonifier.dumps(data), mimetype

return data
return data, mimetype

@classmethod
def get_request(cls, *args, **params):
Expand Down