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

Alias Generator not working with Validation Alias #7781

Closed
1 task done
eawd123wadd4354as243df opened this issue Oct 9, 2023 · 2 comments · Fixed by #7802
Closed
1 task done

Alias Generator not working with Validation Alias #7781

eawd123wadd4354as243df opened this issue Oct 9, 2023 · 2 comments · Fixed by #7802
Assignees
Labels
feature request help wanted Pull Request welcome

Comments

@eawd123wadd4354as243df
Copy link

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

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

from pydantic import BaseModel, ConfigDict, Field
class Foo(BaseModel):
    model_config = ConfigDict(
        populate_by_name=True,
        alias_generator=lambda name: ''.join(word.capitalize() for word in name.split('_'))
    )

    foo_bar0: str
    foo_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 set
          foo_baz1="foo1",  # validation_alias works correctly
          foo_baz2="foo2",  # validation_alias works correctly
          foo_bar3="foo3")  # validation_alias doesn't work because it's overriden

print(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
#   }

Python, Pydantic & OS Version

pydantic version: 2.1.1
        pydantic-core version: 2.5.0
          pydantic-core build: profile=release pgo=false mimalloc=true
                 install path: C:\Users\jsi\PycharmProjects\TESTS\venv\Lib\site-packages\pydantic
               python version: 3.11.2 (tags/v3.11.2:878ead1, Feb  7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)]
                     platform: Windows-10-10.0.19045-SP0
     optional deps. installed: ['typing-extensions']

Footnotes

  1. See foo_bar1

  2. See foo_bar2.

  3. See foo_bar3.

@eawd123wadd4354as243df eawd123wadd4354as243df added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Oct 9, 2023
@sydney-runkle sydney-runkle added feature request and removed bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Oct 9, 2023
@davidhewitt
Copy link
Contributor

Seems reasonable to me that serialization alias should be set from the generator for foo_bar1.

@sydney-runkle
Copy link
Member

Hey @eawd123wadd4354as243df,

Any interest in opening a PR to address this feature request? 😄 Thanks for filing the request!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request help wanted Pull Request welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants