Skip to content
This repository was archived by the owner on Jan 28, 2022. It is now read-only.

Commit 79439d9

Browse files
authored
Merge pull request #87 from sv-tools/fix-propagate-dump-mode
Fix __propagate_dump_mode__ for list nested
2 parents 12d9c1d + 61c2223 commit 79439d9

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

marshmallow_objects/models.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,12 @@ def __propagate_dump_mode__(self, value):
159159
if isinstance(field, fields.Nested):
160160
nested = getattr(self, name, None)
161161
if nested is not None and nested != marshmallow.missing:
162-
nested.__propagate_dump_mode__(value)
162+
if isinstance(nested, list):
163+
for sub_nested in nested:
164+
if sub_nested is not None and sub_nested != marshmallow.missing:
165+
sub_nested.__propagate_dump_mode__(value)
166+
else:
167+
nested.__propagate_dump_mode__(value)
163168

164169
@contextlib.contextmanager
165170
def __dump_mode_on__(self):

tests/test_models.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,15 +539,26 @@ class MissingCompany(marshmallow.Model):
539539
name = marshmallow.fields.String()
540540
owner = marshmallow.NestedModel(MissingPerson)
541541
hr = marshmallow.NestedModel(MissingPerson, allow_none=True)
542+
workers = marshmallow.NestedModel(MissingPerson, many=True, allow_none=True)
542543

543544

544545
class TestMissingFields(unittest.TestCase):
545-
def test_missing_filed(self):
546+
def test_field(self):
546547
self.assertEqual({"name": "John Doe"}, MissingPerson(name="John Doe").dump())
547548

548-
def test_nested_missing_filed(self):
549+
def test_nested_field(self):
549550
self.assertEqual({"owner": {"name": "John Doe"}}, MissingCompany(owner={"name": "John Doe"}).dump())
550551

552+
def test_nested_none(self):
553+
obj = MissingCompany(owner={"name": "John Doe"})
554+
self.assertIsNone(obj.hr)
555+
self.assertEqual({"owner": {"name": "John Doe"}}, obj.dump())
556+
557+
def test_nested_list(self):
558+
obj = MissingCompany(owner={"name": "John Doe"}, workers=[{"name": "Bob"}])
559+
self.assertEqual(1, len(obj.workers))
560+
self.assertEqual({"owner": {"name": "John Doe"}, "workers": [{"name": "Bob"}]}, obj.dump())
561+
551562

552563
class SelfNested(marshmallow.Model):
553564
name = marshmallow.fields.String()

0 commit comments

Comments
 (0)