Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified data silently truncated when using pass_original=True with multiple post_load methods #1755

Closed
ghostwheel42 opened this issue Feb 22, 2021 · 3 comments

Comments

@ghostwheel42
Copy link

ghostwheel42 commented Feb 22, 2021

Hi,
this happens when a pre_load(pass_many=True) hook appends items to the the input data collection, due to the usage of zip in _invoke_processors called by post_load(pass_many=False):

if pass_original:
data = [
processor(item, original, many=many, **kwargs)
for item, original in zip(data, original_data)
]

It is arguable if modifying the input data in this way should be allowed, but it is not forbidden in the docs. I think using itertools.zip_longest would be a good choice here. Raising an exception if len(data) != len(original_data) is another option.

Newly added items will not be validated too:

if many and not pass_many:
for idx, (item, orig) in enumerate(zip(data, original_data)):
self._run_validator(

I think itertools.zip_longest should be used here.

Cheers. Alex

@deckar01 deckar01 added the bug label Feb 22, 2021
@deckar01
Copy link
Member

deckar01 commented Feb 22, 2021

I was unable to reproduce this with pre_load, then post_load, but was able to when they were both post_load.

from marshmallow import Schema, fields, pre_load, post_load

class Test(Schema):
  foo = fields.Str()

  @post_load(pass_many=True)
  def setup(self, data, **kwargs):
    data.append({'foo': 'test'})
    print(data)
    return data

  @post_load(pass_many=False, pass_original=True)
  def test(self, data, original_data, **kwargs):
    print(data, original_data)
    return data

print(Test().load([{'foo': 'bar'}], many=True))
# [{'foo': 'bar'}, {'foo': 'test'}]
# {'foo': 'bar'} {'foo': 'bar'}
# [{'foo': 'bar'}]

@ghostwheel42
Copy link
Author

@deckar01 You are correct, I can't recreate this with pre_load anymore.
I found this a week ago and seem to have remembered incorrectly.

@sloria sloria changed the title Modified data silently truncated when using pass_original=True in Modified data silently truncated when using pass_original=True with multiple post_load methods Jan 20, 2025
@sloria
Copy link
Member

sloria commented Jan 20, 2025

fixed in #2797

@sloria sloria closed this as completed Jan 20, 2025
@sloria sloria removed the bug label Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants