Skip to content

v2.29.0 introduced a bug with serialization that uses a wrapped serializer #1651

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

Closed
ornariece opened this issue Feb 27, 2025 · 1 comment · Fixed by #1660
Closed

v2.29.0 introduced a bug with serialization that uses a wrapped serializer #1651

ornariece opened this issue Feb 27, 2025 · 1 comment · Fixed by #1660

Comments

@ornariece
Copy link
Contributor

import pydantic


class MyModel(pydantic.RootModel):
    root: str

    @pydantic.model_serializer(mode="wrap")
    def my_wrapper_serializer(self, serializer: pydantic.SerializerFunctionWrapHandler):
        return "my_prefix:" + serializer(self)


class MyParentModel(pydantic.BaseModel):
    nested: MyModel

    @pydantic.field_serializer('nested', mode="wrap")
    def wrapped_field_serializer(
        self, field_value, serializer: pydantic.SerializerFunctionWrapHandler
    ):
        return serializer(field_value)


if __name__ == "__main__":
    my_model = MyParentModel.model_validate({'nested': "validated_value"})

    print(my_model.model_dump())
    #>expected: {'nested': 'my_prefix:validated_value'}
    #>actual:   {'nested': 'my_prefix:my_prefix:validated_value'}

i've narrowed down the issue to the "bump to pydantic-core 2.29.0" commit in pydantic.

taking a quick look at what changed in pydantic-core in that version, it seems the bug was introduced by this commit about memory optimization? it looks like the to_python method is naively delegating to the schema serializer without accounting for the wrapping mode. i know little about rust, and haven't really investigated so i could be far off here.

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

Successfully merging a pull request may close this issue.

2 participants