-
Notifications
You must be signed in to change notification settings - Fork 277
feat: Added support for serialization_alias in beanie #1190
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
Changes from all commits
67f2d0e
0146557
ec8bd7c
5294249
a95b499
f61c007
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -403,7 +403,7 @@ def init_document_fields(self, cls) -> None: | |
| if cls._link_fields is None: | ||
| cls._link_fields = {} | ||
| for k, v in get_model_fields(cls).items(): | ||
| path = v.alias or k | ||
| path = getattr(v, "serialization_alias", None) or v.alias or k | ||
| setattr(cls, k, ExpressionField(path)) | ||
|
|
||
| link_info = self.detect_link(v, k) | ||
|
|
@@ -516,7 +516,9 @@ async def init_indexes(self, cls, allow_index_dropping: bool = False): | |
| IndexModel( | ||
| [ | ||
| ( | ||
| fvalue.alias or k, | ||
| getattr(fvalue, "serialization_alias", None) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likewise here, this would be a breaking change if the user does not have the Please add some more tests here around indexes and for these scenarios, e.g. model with previous indexes without the serialization alias, and another model (copy of the previous one) that has the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. got it, thanks for the detailed overview, will look into it.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @staticxterm yeah, you were right, I first created a model with a field having index, but no serialization_alias, and it created the correct index, then I modified the field to have a serialization_alias, and it also created a index according to the serialization_alias but doesn't drop the previous one, for it to gets drop, I tried allow_index_dropping=True and then it drops that index. |
||
| or fvalue.alias | ||
| or k, | ||
| indexed_attrs[0], | ||
| ) | ||
| ], | ||
|
|
@@ -639,7 +641,7 @@ def init_view_fields(self, cls) -> None: | |
| if cls._link_fields is None: | ||
| cls._link_fields = {} | ||
| for k, v in get_model_fields(cls).items(): | ||
| path = v.alias or k | ||
| path = getattr(v, "serialization_alias", None) or v.alias or k | ||
| setattr(cls, k, ExpressionField(path)) | ||
| link_info = self.detect_link(v, k) | ||
| depth_level = cls.get_settings().max_nesting_depths_per_field.get( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import pytest | ||
|
|
||
| from beanie.odm.utils.pydantic import IS_PYDANTIC_V2 | ||
| from tests.odm.models import DocumentTestModelWithSerializationAlias | ||
|
|
||
|
|
||
| def data_maker(): | ||
| return DocumentTestModelWithSerializationAlias(test_int=1, test_str="test") | ||
|
|
||
|
|
||
| @pytest.mark.skipif( | ||
| not IS_PYDANTIC_V2, | ||
| reason="model serialization_alias is not supported in pydantic V1", | ||
| ) | ||
| async def test_serialization_types_preserved_after_insertion(): | ||
| result = await DocumentTestModelWithSerializationAlias.insert_one( | ||
| data_maker() | ||
| ) | ||
| document = await DocumentTestModelWithSerializationAlias.get(result.id) | ||
| assert document is not None | ||
| assert document.test_int is not None | ||
| assert document.test_str is not None | ||
| dumped = document.model_dump(by_alias=True) | ||
| assert "test_int_serialize" in dumped | ||
| assert "test_str_serialize" in dumped | ||
| assert "test_int" not in dumped | ||
| assert "test_str" not in dumped |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somehow this comment got lost...
This would be a breaking change, and probably would not be correct if someone used the
validation_aliasinstead to initialize the model...