From 599800c070c6fa5f7dd29c88aa4863d5599e4e79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81uczak=2C=20Piotr?= Date: Fri, 4 Jan 2019 19:30:27 +0100 Subject: [PATCH] Fix deserialization by property with many=True Because data was overridden late in the _deserialize function it didn't preserve the parent schema for subsequent elements and passed along the previous list element instead --- marshmallow_polyfield/polyfield.py | 6 +++--- tests/test_deserialization.py | 12 ++++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/marshmallow_polyfield/polyfield.py b/marshmallow_polyfield/polyfield.py index ca85c75..f7e839e 100644 --- a/marshmallow_polyfield/polyfield.py +++ b/marshmallow_polyfield/polyfield.py @@ -10,7 +10,7 @@ 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] @@ -18,7 +18,7 @@ def _deserialize(self, value, attr, data): 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)): @@ -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) diff --git a/tests/test_deserialization.py b/tests/test_deserialization.py index 458cb23..f5f01a4 100644 --- a/tests/test_deserialization.py +++ b/tests/test_deserialization.py @@ -243,7 +243,7 @@ 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' ) @@ -251,9 +251,13 @@ def test_deserialize_polyfield(self, schema): {'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