Thanks to visit codestin.com
Credit goes to github.com

Skip to content

More consistent and intuitive alias behavior for validation and serialization #8379

@sydney-runkle

Description

@sydney-runkle

Right now, we have some inconsistent behavior in terms of using aliases in validation and serialization.

By default, if an alias or validation_alias is defined on a field, we use the alias for validation. This behavior can be changed by setting populate_by_name to True on the model_config.

Conversely, if an alias or serialization_alias is defined on a field, that alias not used by default for serialization. We must specify by_alias=True in the call to model_dump + other serialization functions.

I propose that in V3:

  • We use alias by default for both validation and serialization
  • We add a field (or fields, but ideally just one) to ConfigDict to support different behavior than the default

This is a breaking change, hence the V3 label.

Requests to change the inconsistent default behavior have been made for a few years, so I'm going to comb through issues and close those so we can centralize discussion here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions