Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1879: Serializes Sets in Response #1881

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions chalice/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def handle_extra_types(obj):
# to support that as well.
if isinstance(obj, decimal.Decimal):
return float(obj)
# DynamoDB responses might contain sets
if isinstance(obj, set):
return list(obj)
# This is added for backwards compatibility.
# It will keep only the last value for every key as it used to.
if isinstance(obj, MultiDict):
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1816,6 +1816,17 @@ def test_http_response_to_dict(body, headers, status_code):
assert isinstance(serialized['body'], six.string_types)


@given(headers=STR_MAP,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we need these property based tests for this change. We're passing in varying headers and status code values but our test is just verifying the values of sets. You can probably just create various Response instances and verify the to_dict() serializes the types properly. Something like test_can_encode_binary_body_as_base64().

Probably worth adding an extra test for nested sets as well.

status_code=st.integers(min_value=200, max_value=599))
def test_http_response_with_set_to_dict(headers, status_code):
r = Response(body={'example': set([1, 2, 3])}, headers=headers, status_code=status_code)
serialized = r.to_dict()
assert 'headers' in serialized
assert 'statusCode' in serialized
assert 'body' in serialized
assert isinstance(serialized['body'], six.string_types)


@given(body=st.binary(), content_type=st.sampled_from(BINARY_TYPES))
def test_handles_binary_responses(body, content_type):
r = Response(body=body, headers={'Content-Type': content_type})
Expand Down