diff --git a/AUTHORS.rst b/AUTHORS.rst index 7f557b7f6..ea47e2e94 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -169,3 +169,4 @@ Contributors (chronological) - Kevin Kirsche `@kkirsche `_ - Isira Seneviratne `@Isira-Seneviratne `_ - Karthikeyan Singaravelan `@tirkarthi `_ +- Benedek Horváth `@benedekh `_ diff --git a/src/marshmallow/fields.py b/src/marshmallow/fields.py index 6b26126eb..faae699f3 100644 --- a/src/marshmallow/fields.py +++ b/src/marshmallow/fields.py @@ -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 diff --git a/tests/test_fields.py b/tests/test_fields.py index 79fd01727..b9fc92b25 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -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"]]}