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
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 .
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.
The text was updated successfully, but these errors were encountered:
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 insertingDocument.id
as "id" not "_id".Providing
AliasGenerator
as an argument toConfigDict
affects the behaviour ofDocument.model_fields
. That is the instances ofFieldInfo
class no longer have attributealias
butvalidation_alias
(if provided) andseralization_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 neitherFieldInfo.validation_alias
norFieldInfor. seralization_alias
.To Reproduce
Expected behavior
I believe Beanie should support
serialization_alias
while loading to/from database. That is, ifserialization_alias
is provided, it is responsible for mapping MongoDB field names and Python ODM model attributes. If not provided, the default alias generator, that isdocument_alias_generator
(using Python field names with an exception forid
mapped as_id
) should be used.For constructingDocument
instances in Python, both the real field names and the aliases defined byvalidation_alias
should be supported, with either error on collision or the conversion controlled bypydantic.fields.FieldInfo.alias_priority
.The text was updated successfully, but these errors were encountered: