Skip to content

Commit

Permalink
Add deprecation warnings about implicit serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
cognifloyd committed Dec 7, 2019
1 parent e57e6a6 commit f029a01
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
10 changes: 10 additions & 0 deletions connexion/apis/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import pathlib
import sys
import warnings
from enum import Enum

import six
Expand Down Expand Up @@ -425,6 +426,15 @@ def _serialize_data(cls, data, mimetype):
elif isinstance(data, str):
body = data
else:
warnings.warn(
"Implicit (aiohttp) serialization with str() will change in the next major version. "
"This is triggered because a non-JSON response body is being stringified. "
"This will be replaced by something that is mimetype-specific and may "
"serialize some things as JSON or throw an error instead of silently "
"stringifying unknown response bodies. "
"Please make sure to specify media/mime types in your specs.",
warnings.FutureWarning # a Deprecation targeted at application users.
)
body = str(data)
else:
body = data
Expand Down
21 changes: 17 additions & 4 deletions connexion/apis/flask_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import warnings

import flask
import six
Expand Down Expand Up @@ -188,11 +189,23 @@ def _build_response(cls, mimetype, content_type=None, headers=None, status_code=
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), mimetype
if (isinstance(mimetype, str) and is_json_mimetype(mimetype)):
body = cls.jsonifier.dumps(data)
elif not (isinstance(data, bytes) or isinstance(data, str)):
warnings.warn(
"Implicit (flask) JSON serialization will change in the next major version. "
"This is triggered because a response body is being serialized as JSON "
"even though the mimetype is not a JSON type. "
"This will be replaced by something that is mimetype-specific and may "
"raise an error instead of silently converting everything to JSON. "
"Please make sure to specify media/mime types in your specs.",
warnings.FutureWarning # a Deprecation targeted at application users.
)
body = cls.jsonifier.dumps(data)
else:
body = data

return data, mimetype
return body, mimetype

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

0 comments on commit f029a01

Please sign in to comment.