Skip to content

Commit

Permalink
marshmallow-code#1993 Fix serialization of List items if they are ser…
Browse files Browse the repository at this point in the history
…ialized by fields.Method.
  • Loading branch information
benedekh committed May 29, 2022
1 parent 645cba2 commit 18603d5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,4 @@ Contributors (chronological)
- Kevin Kirsche `@kkirsche <https://github.com/kkirsche>`_
- Isira Seneviratne `@Isira-Seneviratne <https://github.com/Isira-Seneviratne>`_
- Karthikeyan Singaravelan `@tirkarthi <https://github.com/tirkarthi>`_
- Benedek Horváth `@benedekh <https://github.com/benedekh>`_
3 changes: 2 additions & 1 deletion src/marshmallow/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,8 @@ def __init__(self, cls_or_instance: Field | type, **kwargs):
def _bind_to_schema(self, field_name, schema):
super()._bind_to_schema(field_name, schema)
self.inner = copy.deepcopy(self.inner)
self.inner._bind_to_schema(field_name, self)
self.inner.parent = self
self.inner._bind_to_schema(field_name, schema)
if isinstance(self.inner, Nested):
self.inner.only = self.only
self.inner.exclude = self.exclude
Expand Down
13 changes: 13 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,3 +665,16 @@ class Family(Schema):
"daughter": {"value": {"age": ["Missing data for required field."]}}
}
}


class TestMethodSerializationOfListItems:
def test_method_serialization_of_list_items(self):
class ExampleSchema(Schema):
dummy_list = fields.List(fields.Method(serialize="serialize_me"))

def serialize_me(self, obj):
return [str(value) for value in obj["dummy_list"]]

obj = {"dummy_list": ["hello", "world"]}
dumped = ExampleSchema().dump(obj)
assert dumped == {"dummy_list": [["hello", "world"], ["hello", "world"]]}

0 comments on commit 18603d5

Please sign in to comment.