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/decorators.py b/marshmallow/decorators.py
index 8b6df0df..cd850fc0 100644
--- a/marshmallow/decorators.py
+++ b/marshmallow/decorators.py
@@ -107,6 +107,9 @@ def post_dump(fn=None, pass_many=False, pass_original=False):
     By default, receives a single object at a time, transparently handling the ``many``
     argument passed to the Schema. If ``pass_many=True``, the raw data
     (which may be a collection) and the value for ``many`` is passed.
+
+    If ``pass_original=True``, the original data (before serializing) will be passed as
+    an additional argument to the method.
     """
     return tag_processor(POST_DUMP, fn, pass_many, pass_original=pass_original)
 
@@ -129,6 +132,9 @@ def post_load(fn=None, pass_many=False, pass_original=False):
     By default, receives a single datum at a time, transparently handling the ``many``
     argument passed to the Schema. If ``pass_many=True``, the raw data
     (which may be a collection) and the value for ``many`` is passed.
+
+    If ``pass_original=True``, the original data (before deserializing) will be passed as
+    an additional argument to the method.
     """
     return tag_processor(POST_LOAD, fn, pass_many, pass_original=pass_original)
 
diff --git a/marshmallow/schema.py b/marshmallow/schema.py
index 79bb8ee1..904c5322 100644
--- a/marshmallow/schema.py
+++ b/marshmallow/schema.py
@@ -869,8 +869,8 @@ class BaseSchema(base.SchemaABC):
                     data = utils.if_none(processor(data, many), data)
             elif many:
                 if pass_original:
-                    data = [utils.if_none(processor(item, original_data), item)
-                            for item in data]
+                    data = [utils.if_none(processor(item, original), item)
+                            for item, original in zip(data, original_data)]
                 else:
                     data = [utils.if_none(processor(item), item) for item in data]
             else:


Test patch:
diff --git a/tests/test_decorators.py b/tests/test_decorators.py
index 226e550f..41a2d44a 100644
--- a/tests/test_decorators.py
+++ b/tests/test_decorators.py
@@ -758,3 +758,80 @@ def test_decorator_error_handling_with_dump(decorator):
         schema.dump(object())
     assert exc.value.messages == {'foo': 'error'}
     schema.load({})
+
+
+class Nested(object):
+    def __init__(self, foo):
+        self.foo = foo
+
+
+class Example(object):
+    def __init__(self, nested):
+        self.nested = nested
+
+
+example = Example(nested=[Nested(x) for x in range(1)])
+
+
+@pytest.mark.parametrize(
+    "data,expected_data,expected_original_data",
+    (
+        [example, {"foo": 0}, example.nested[0]],
+    ),
+)
+def test_decorator_post_dump_with_nested_pass_original_and_pass_many(
+        data, expected_data, expected_original_data):
+
+    class NestedSchema(Schema):
+        foo = fields.Int(required=True)
+
+        @post_dump(pass_many=False, pass_original=True)
+        def check_pass_original_when_pass_many_false(self, data, original_data):
+            assert data == expected_data
+            assert original_data == expected_original_data
+            return data
+
+        @post_dump(pass_many=True, pass_original=True)
+        def check_pass_original_when_pass_many_true(self, data, many, original_data):
+            assert many is True
+            assert data == [expected_data]
+            assert original_data == [expected_original_data]
+            return data
+
+    class ExampleSchema(Schema):
+        nested = fields.Nested(NestedSchema, required=True, many=True)
+
+    schema = ExampleSchema()
+    assert schema.dump(data) == {"nested": [{"foo": 0}]}
+
+
+@pytest.mark.parametrize(
+    "data,expected_data,expected_original_data",
+    (
+        [{"nested": [{"foo": 0}]}, {"foo": 0}, {"foo": 0}],
+    ),
+)
+def test_decorator_post_load_with_nested_pass_original_and_pass_many(
+        data, expected_data, expected_original_data):
+
+    class NestedSchema(Schema):
+        foo = fields.Int(required=True)
+
+        @post_load(pass_many=False, pass_original=True)
+        def check_pass_original_when_pass_many_false(self, data, original_data):
+            assert data == expected_data
+            assert original_data == expected_original_data
+            return data
+
+        @post_load(pass_many=True, pass_original=True)
+        def check_pass_original_when_pass_many_true(self, data, many, original_data):
+            assert many is True
+            assert data == [expected_data]
+            assert original_data == [expected_original_data]
+            return data
+
+    class ExampleSchema(Schema):
+        nested = fields.Nested(NestedSchema, required=True, many=True)
+
+    schema = ExampleSchema()
+    assert schema.load(data) == data