Repository: marshmallow-code/marshmallow

Issue:
post_dump is passing a list of objects as original object
Hi,

I think post_dump with pass_original=True should pass the original object related to the data serialized and not a list of objects which this object belongs to.

``` python
from marshmallow import fields, post_dump, Schema

class DeviceSchema(Schema):
    id = fields.String()

    @post_dump(pass_original=True)
    def __post_dump(self, data, obj):
        print(obj)  # <-- this is a list

devices = [dict(id=1), dict(id=2)]
DeviceSchema().dump(devices, many=True)
```

In the above example, the parameter `obj` is a list of devices rather than the device object itself.

What do you think?


Discussion:

Patch:
diff --git a/marshmallow/schema.py b/marshmallow/schema.py
index 2327081a..79bb8ee1 100644
--- a/marshmallow/schema.py
+++ b/marshmallow/schema.py
@@ -836,11 +836,11 @@ class BaseSchema(base.SchemaABC):
             if pass_many:
                 validator = functools.partial(validator, many=many)
             if many and not pass_many:
-                for idx, item in enumerate(data):
+                for idx, (item, orig) in enumerate(zip(data, original_data)):
                     try:
-                        self._unmarshal.run_validator(validator,
-                                                  item, original_data, self.fields, many=many,
-                                                  index=idx, pass_original=pass_original)
+                        self._unmarshal.run_validator(
+                            validator, item, orig, self.fields, many=many,
+                            index=idx, pass_original=pass_original)
                     except ValidationError as err:
                         errors.update(err.messages)
             else:


Test patch:
diff --git a/tests/test_decorators.py b/tests/test_decorators.py
index 3fb3f1bb..226e550f 100644
--- a/tests/test_decorators.py
+++ b/tests/test_decorators.py
@@ -365,6 +365,35 @@ class TestValidatesSchemaDecorator:
         assert '_schema' in errors['nested']
         assert 'foo' not in errors['nested']
 
+    @pytest.mark.parametrize("data", ([{"foo": 1, "bar": 2}],))
+    @pytest.mark.parametrize(
+        "pass_many,expected_data,expected_original_data",
+        (
+            [True, [{"foo": 1}], [{"foo": 1, "bar": 2}]],
+            [False, {"foo": 1}, {"foo": 1, "bar": 2}],
+        ),
+    )
+    def test_validator_nested_many_pass_original_and_pass_many(
+            self, pass_many, data, expected_data, expected_original_data):
+
+        class NestedSchema(Schema):
+            foo = fields.Int(required=True)
+
+            @validates_schema(pass_many=pass_many, pass_original=True)
+            def validate_schema(self, data, original_data, many=False):
+                assert data == expected_data
+                assert original_data == expected_original_data
+                assert many is pass_many
+                raise ValidationError("Method called")
+
+        class MySchema(Schema):
+            nested = fields.Nested(NestedSchema, required=True, many=True)
+
+        schema = MySchema()
+        errors = schema.validate({"nested": data})
+        error = errors["nested"] if pass_many else errors["nested"][0]
+        assert error["_schema"][0] == "Method called"
+
     def test_decorated_validators(self):
 
         class MySchema(Schema):