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

fix: fix deserialization of unions if the type of a field with the same name differs between union members #511

Merged
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
2 changes: 1 addition & 1 deletion dataclasses_json/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def _decode_generic(type_, value, infer_missing):
try:
res = _decode_dataclass(type_option, value, infer_missing)
break
except (KeyError, ValueError):
except (KeyError, ValueError, AttributeError):
continue
if res == value:
warnings.warn(
Expand Down
38 changes: 37 additions & 1 deletion tests/test_union.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ class Aux2:
class Aux3:
f2: str


@dataclass_json
@dataclass
class C4:
f1: Union[Aux1, Aux2]


@dataclass_json
@dataclass
class C12:
Expand Down Expand Up @@ -124,6 +126,31 @@ class C11:
"""
f1: Union[str, None] = None


@dataclass_json
@dataclass
class C13:
data: str


@dataclass_json
@dataclass
class C14:
content: str


@dataclass_json
@dataclass
class C15:
data: C13


@dataclass_json
@dataclass
class C16:
event: Union[C15, C13]


params = [
(C1(f1=12), {"f1": 12}, '{"f1": 12}'),
(C1(f1="str1"), {"f1": "str1"}, '{"f1": "str1"}'),
Expand Down Expand Up @@ -209,6 +236,7 @@ def test_deserialize_with_error(cls, data):
with pytest.raises(ValidationError):
assert s.load(data)


def test_deserialize_without_discriminator():
# determine based on type
json = '{"f1": {"f1": 1}}'
Expand Down Expand Up @@ -239,4 +267,12 @@ def test_deserialize_without_discriminator():
json = '{"f1": {"f3": "str2"}}'
s = C12.schema()
obj = s.loads(json)
assert type(obj.f1) == dict
assert type(obj.f1) == dict


def test_deserialize_with_mismatched_field_types():
json = '{"event": {"data": "Hello world!"} }'
s = C16.schema()
obj = s.loads(json)
assert obj.event is not None
assert obj.event.data == "Hello world!"
Loading