Skip to content

fix: Serialize sets into JSON #781

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

Merged
merged 2 commits into from
Aug 12, 2020
Merged
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
8 changes: 5 additions & 3 deletions sentry_sdk/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
if PY2:
# Importing ABCs from collections is deprecated, and will stop working in 3.8
# https://github.com/python/cpython/blob/master/Lib/collections/__init__.py#L49
from collections import Mapping, Sequence
from collections import Mapping, Sequence, Set

serializable_str_types = string_types

else:
# New in 3.3
# https://docs.python.org/3/library/collections.abc.html
from collections.abc import Mapping, Sequence
from collections.abc import Mapping, Sequence, Set

# Bytes are technically not strings in Python 3, but we can serialize them
serializable_str_types = (str, bytes)
Expand Down Expand Up @@ -291,7 +291,9 @@ def _serialize_node_impl(

return rv_dict

elif not isinstance(obj, serializable_str_types) and isinstance(obj, Sequence):
elif not isinstance(obj, serializable_str_types) and isinstance(
obj, (Set, Sequence)
):
rv_list = []

for i, v in enumerate(obj):
Expand Down
18 changes: 18 additions & 0 deletions tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ def inner(message, **kwargs):
return inner


@pytest.fixture
def extra_normalizer(relay_normalize):
if relay_normalize({"test": "test"}) is None:
pytest.skip("no relay available")

def inner(message, **kwargs):
event = serialize({"extra": {"foo": message}}, **kwargs)
normalized = relay_normalize(event)
return normalized["extra"]["foo"]

return inner


def test_bytes_serialization_decode(message_normalizer):
binary = b"abc123\x80\xf0\x9f\x8d\x95"
result = message_normalizer(binary, should_repr_strings=False)
Expand All @@ -66,3 +79,8 @@ def test_bytes_serialization_repr(message_normalizer):
binary = b"abc123\x80\xf0\x9f\x8d\x95"
result = message_normalizer(binary, should_repr_strings=True)
assert result == r"b'abc123\x80\xf0\x9f\x8d\x95'"


def test_serialize_sets(extra_normalizer):
result = extra_normalizer({1, 2, 3})
assert result == [1, 2, 3]