Skip to content

Commit

Permalink
Merge pull request #21 from pejter/master
Browse files Browse the repository at this point in the history
Fix deserialization by property with many=True
  • Loading branch information
Bachmann1234 authored Jan 6, 2019
2 parents 803a743 + 599800c commit 25bcd6d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 3 additions & 3 deletions marshmallow_polyfield/polyfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ def __init__(self, many=False, **metadata):
super(PolyFieldBase, self).__init__(**metadata)
self.many = many

def _deserialize(self, value, attr, data):
def _deserialize(self, value, attr, parent):
if not self.many:
value = [value]

results = []
for v in value:
deserializer = None
try:
deserializer = self.deserialization_schema_selector(v, data)
deserializer = self.deserialization_schema_selector(v, parent)
if isinstance(deserializer, type):
deserializer = deserializer()
if not isinstance(deserializer, (Field, Schema)):
Expand All @@ -40,7 +40,7 @@ def _deserialize(self, value, attr, data):

# Will raise ValidationError if any problems
if isinstance(deserializer, Field):
data = deserializer.deserialize(v, attr, data)
data = deserializer.deserialize(v, attr, parent)
else:
deserializer.context.update(getattr(self, 'context', {}))
data = deserializer.load(v)
Expand Down
12 changes: 8 additions & 4 deletions tests/test_deserialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,21 @@ def make_object(self, data):
def test_deserialize_polyfield(self, schema):
original = self.ContrivedShapeClass(
Rectangle('blue', 1, 100),
[Rectangle('pink', 4, 93)],
[Rectangle('pink', 4, 93), Rectangle('red', 3, 90)],
'rectangle'
)

data = schema().load(
{'main': {'color': 'blue',
'length': 1,
'width': 100},
'others': [{'color': 'pink',
'length': 4,
'width': 93}],
'others': [
{'color': 'pink',
'length': 4,
'width': 93},
{'color': 'red',
'length': 3,
'width': 90}],
'type': 'rectangle'}
)
assert data == original

0 comments on commit 25bcd6d

Please sign in to comment.