You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using an alias_generator and a validation alias, the alias_generator does not override the serialization alias (which is not set)1.
This is very unintuitive, since if a normal alias is set, it overrides default value for serialization alias as well2.
Setting alias_priority=1 on every field fixes this, but it also removes the validation alias3.
My suggestion would be making making alias_generator override both unset validation_alias and unset serialization_alias. Additionally there should be a default_alias_priority field added to the model_config, so that even when adding a serialization_alias, a common style can be guaranteed.
Why is this needed?
In our FastApi project, we want to return a JSON object, with CamelCase. In python we want to use snake case though. We get some data from another api, so I added a validation_alias to Model. This however broke our API, since the resulting JSON was not in CamelCase anymore.
Example Code
frompydanticimportBaseModel, ConfigDict, FieldclassFoo(BaseModel):
model_config=ConfigDict(
populate_by_name=True,
alias_generator=lambdaname: ''.join(word.capitalize() forwordinname.split('_'))
)
foo_bar0: strfoo_bar1: str=Field(validation_alias='foo_baz1')
foo_bar2: str=Field(validation_alias='foo_baz2', alias='foo_bra2')
foo_bar3: str=Field(validation_alias='foo_baz3', alias_priority=1)
foo=Foo(foo_bar0='foo0', # no validation_alias setfoo_baz1="foo1", # validation_alias works correctlyfoo_baz2="foo2", # validation_alias works correctlyfoo_bar3="foo3") # validation_alias doesn't work because it's overridenprint(foo.model_dump(by_alias=True))
# {# 'FooBar0': 'foo0', # Works correctly # 'foo_bar1': 'foo1', # Doesn't work (!! unexpected)# 'foo_bra2': 'foo2', # Works, but doesn't apply generator (as expected?)# 'FooBar3': 'foo3' # Works correctly# }
Initial Checks
Description
When using an
alias_generator
and avalidation alias
, thealias_generator
does not override the serialization alias (which is not set)1.This is very unintuitive, since if a normal alias is set, it overrides default value for serialization alias as well2.
Setting
alias_priority=1
on every field fixes this, but it also removes thevalidation alias
3.My suggestion would be making making
alias_generator
override both unsetvalidation_alias
and unsetserialization_alias
. Additionally there should be adefault_alias_priority
field added to themodel_config
, so that even when adding aserialization_alias
, a common style can be guaranteed.Why is this needed?
In our FastApi project, we want to return a JSON object, with CamelCase. In python we want to use snake case though. We get some data from another api, so I added a
validation_alias
to Model. This however broke our API, since the resulting JSON was not in CamelCase anymore.Example Code
Python, Pydantic & OS Version
Footnotes
See
foo_bar1
↩See
foo_bar2
. ↩See
foo_bar3
. ↩The text was updated successfully, but these errors were encountered: