Skip to content

Commit

Permalink
Merge pull request #1117 from stealthycoin/fix-encoding-type-error
Browse files Browse the repository at this point in the history
Fix encoding type error
  • Loading branch information
stealthycoin authored May 6, 2019
2 parents ad31b35 + ac0ac24 commit 1933249
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Next Release (TBD)
* Fix handling of more complex Accept headers for binary
content types
(`#1078 <https://github.com/aws/chalice/issues/1078>`__)
* Raise TypeError when trying to serialize an unserializable
type
(`#1100 <https://github.com/aws/chalice/issues/1100>`__)


1.8.0
Expand Down
7 changes: 4 additions & 3 deletions chalice/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ def unquote_str(value, encoding='utf-8'):
_ANY_STRING = (basestring, bytes) # noqa pylint: disable=E0602


def handle_decimals(obj):
def handle_extra_types(obj):
# Lambda will automatically serialize decimals so we need
# to support that as well.
if isinstance(obj, decimal.Decimal):
return float(obj)
return obj
raise TypeError('Object of type %s is not JSON serializable'
% obj.__class__.__name__)


def error_response(message, error_code, http_status_code, headers=None):
Expand Down Expand Up @@ -364,7 +365,7 @@ def to_dict(self, binary_types=None):
body = self.body
if not isinstance(body, _ANY_STRING):
body = json.dumps(body, separators=(',', ':'),
default=handle_decimals)
default=handle_extra_types)
response = {
'headers': self.headers,
'statusCode': self.status_code,
Expand Down
14 changes: 12 additions & 2 deletions tests/unit/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from chalice import app
from chalice import NotFoundError
from chalice.app import APIGateway, Request, Response, handle_decimals
from chalice.app import APIGateway, Request, Response, handle_extra_types
from chalice import __version__ as chalice_version
from chalice.deploy.validate import ExperimentalFeatureError
from chalice.deploy.validate import validate_feature_flags
Expand Down Expand Up @@ -152,6 +152,16 @@ def test_invalid_binary_response_body_throws_value_error(sample_app):
response.to_dict(sample_app.api.binary_types)


def test_invalid_JSON_response_body_throws_type_error(sample_app):
response = app.Response(
status_code=200,
body={'foo': object()},
headers={'Content-Type': 'application/json'}
)
with pytest.raises(TypeError):
response.to_dict()


def test_can_encode_binary_body_as_base64(sample_app):
response = app.Response(
status_code=200,
Expand Down Expand Up @@ -1399,7 +1409,7 @@ def test_http_request_to_dict_is_json_serializable(http_request_kwargs):
request_dict = request.to_dict()
# We should always be able to dump the request dict
# to JSON.
assert json.dumps(request_dict, default=handle_decimals)
assert json.dumps(request_dict, default=handle_extra_types)


@given(body=st.text(), headers=STR_MAP,
Expand Down

0 comments on commit 1933249

Please sign in to comment.