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

Nested serialization regression between 1.8.2 and master #3544

Closed
3 tasks done
christianbundy opened this issue Dec 19, 2021 · 2 comments
Closed
3 tasks done

Nested serialization regression between 1.8.2 and master #3544

christianbundy opened this issue Dec 19, 2021 · 2 comments
Labels
bug V1 Bug related to Pydantic V1.X duplicate

Comments

@christianbundy
Copy link
Contributor

Checks

  • I added a descriptive title to this issue
  • I have searched (google, github) for similar issues and couldn't find anything
  • I have read and followed the docs and still think this is a bug

Bug

I found a small regression between 1.8.2 and master that broke some tests for me -- I bisected and found that it started with 458f257. Here's the test I used:

from typing import Optional
from pydantic import BaseModel, Field


class Model(BaseModel):
    value: Optional[str]
    nested: Optional[BaseModel]


def test_my_bug():
    assert (
        Model(value=None, nested=Model(value=None)).json(exclude_none=True)
        == '{"nested": {}}'
    )

In 1.8.2, options to json() (like exclude_none) were passed to all nested models, but in master that no longer occurs. Instead, we see:

{ "nested": { "value": null } }

In my case, I noticed this because I had two models:

class Model(BaseModel):
    value: Optional[str]

class OuterModel(Model):
    nested: Optional[Model]

    def json(self, **kwargs):
        kwargs.set_default("exclude_none", True)
        return super().json(**kwargs)


# Usage:
OuterModel(nested=Model()).json()

In 1.8.2 this was overridden json() was used on all nested models as well, but in master this is only applied to the outer model. This means that we get { "nested": { "value": null } } when previously we would've gotten { "nested": {} } like above.

@christianbundy christianbundy added the bug V1 Bug related to Pydantic V1.X label Dec 19, 2021
@samuelcolvin
Copy link
Member

I think this is the same as #3542. If not, let me know and I'll reopen

@christianbundy
Copy link
Contributor Author

I think when I posted the bug report they were slightly different, but it looks like @PrettyWood pushed another commit to take care of this. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X duplicate
Projects
None yet
Development

No branches or pull requests

2 participants