Skip to content

Commit

Permalink
Merge pull request #33 from lovasoa/master
Browse files Browse the repository at this point in the history
Fix invalid serialization for lists of unions
  • Loading branch information
lovasoa committed May 29, 2020
2 parents 58bfc9f + 78c654c commit 4821724
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/marshmallow_union/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class MarshmallowUnionException(Exception):

class ExceptionGroup(MarshmallowUnionException):
"""Collection of possibly multiple exceptions."""

def __init__(self, msg: str, errors):
self.msg = msg
self.errors = errors
Expand Down Expand Up @@ -63,8 +64,8 @@ def _serialize(self, value: t.Any, attr: str, obj: str, **kwargs):
for candidate_field in fields:

try:
return candidate_field.serialize(
attr, obj, error_store=error_store, **kwargs
return candidate_field._serialize(
value, attr, obj, error_store=error_store, **kwargs
)
except ValueError as e:
error_store.store_error({attr: e})
Expand Down
9 changes: 9 additions & 0 deletions tests/test_union.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,22 @@ class IntStrSchema(marshmallow.Schema):
x = marshmallow_union.Union([marshmallow.fields.Int(), marshmallow.fields.String()])


class ListUnionSchema(marshmallow.Schema):
"""Schema with a list of unions."""

l = marshmallow.fields.List(
marshmallow_union.Union([marshmallow.fields.Int(), marshmallow.fields.String()])
)


@pytest.mark.parametrize(
"data, schema",
[
({"name": "Alice", "number_or_numbers": 25}, PersonSchema()),
({"name": "Alice", "number_or_numbers": [25, 50]}, PersonSchema()),
({"name": "Alice", "number_or_numbers": [25, 50]}, OtherSchema()),
({"x": 5}, IntStrSchema()),
({"l": ["h", 5, "n", 1]}, ListUnionSchema()),
({"x": "hello"}, IntStrSchema()),
({"items": {"a": 42, "b": [17]}}, MappingSchema()),
],
Expand Down

0 comments on commit 4821724

Please sign in to comment.