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

[BUG] Beanie does not properly supports pydantic.AliasGenerator #1033

Open
jcierocki opened this issue Sep 29, 2024 · 2 comments
Open

[BUG] Beanie does not properly supports pydantic.AliasGenerator #1033

jcierocki opened this issue Sep 29, 2024 · 2 comments

Comments

@jcierocki
Copy link

Describe the bug
I receive data with all field names following camel case and I need this data immediately inserted into the database, without any additional processing. But I prefer to keep all the field names in the database in snake case. Pydantic V2 allows to specify different aliases for validation and serialisation using pydantic.AliasGenerator but Beanie does support this at all, leading to unexpected behaviour. In make case this issue is primarily inserting Document.id as "id" not "_id".

Providing AliasGenerator as an argument to ConfigDict affects the behaviour of Document.model_fields. That is the instances of FieldInfo class no longer have attribute alias but validation_alias (if provided) and seralization_alias (if provided). After digging a little bit into Beanie code I believe I found at leat the partial source of this issue here: link. beanie.odm.utils.Encoder._iter_model_items() recognises neither FieldInfo.validation_alias nor FieldInfor. seralization_alias .

To Reproduce

from beanie import Document
from beanie.odm.documents import document_alias_generator
from pydantic import ConfigDict, AliasGenerator
from pydantic.alias_generators import to_camel

class SomeBeanieModel(Document):
    model_config = ConfigDict(alias_generator=AliasGenerator(validation_alias=lambda s: document_alias_generator(to_camel(s)), seralization_alias=document_alias_generator)

    some_field: int

SomeBeanieModel(some_field=1).insert()

Expected behavior
I believe Beanie should support serialization_alias while loading to/from database. That is, if serialization_alias is provided, it is responsible for mapping MongoDB field names and Python ODM model attributes. If not provided, the default alias generator, that is document_alias_generator (using Python field names with an exception for id mapped as _id) should be used.For constructing Document instances in Python, both the real field names and the aliases defined by validation_alias should be supported, with either error on collision or the conversion controlled by pydantic.fields.FieldInfo.alias_priority.

Copy link
Contributor

This issue is stale because it has been open 30 days with no activity.

@github-actions github-actions bot added the Stale label Oct 30, 2024
@staticxterm
Copy link

Hi @jcierocki, thank you for the report.

I haven't verified this, but I am adding a feature request label since I believe the maintainers were not aware of this functionality as it was only recently added in Pydantic v2.6.0, released near the end of January this year.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants