From d6f42239eafa06150b6ac1440349a121d33848d7 Mon Sep 17 00:00:00 2001 From: Roman Right Date: Wed, 13 Sep 2023 23:01:46 -0600 Subject: [PATCH] Fix: unified methods for getting extra parameters of the field during backlinks processing (#703) * fix: optional backlinks * version: 1.22.5 --- beanie/__init__.py | 2 +- beanie/odm/utils/init.py | 20 ++++++++++++-------- docs/changelog.md | 11 +++++++++++ pyproject.toml | 2 +- tests/odm/conftest.py | 4 ++++ tests/odm/models.py | 24 ++++++++++++++++++++++++ tests/test_beanie.py | 2 +- 7 files changed, 54 insertions(+), 11 deletions(-) diff --git a/beanie/__init__.py b/beanie/__init__.py index 8f854af4..c41e6b54 100644 --- a/beanie/__init__.py +++ b/beanie/__init__.py @@ -31,7 +31,7 @@ from beanie.odm.views import View from beanie.odm.union_doc import UnionDoc -__version__ = "1.22.4" +__version__ = "1.22.5" __all__ = [ # ODM "Document", diff --git a/beanie/odm/utils/init.py b/beanie/odm/utils/init.py index 025c214d..658e26ce 100644 --- a/beanie/odm/utils/init.py +++ b/beanie/odm/utils/init.py @@ -273,9 +273,9 @@ def detect_link( if cls is BackLink: return LinkInfo( field_name=field_name, - lookup_field_name=field.json_schema_extra[ # type: ignore - "original_field" - ], + lookup_field_name=get_extra_field_info( + field, "original_field" + ), document_class=DocsRegistry.evaluate_fr(optional_args[0]), # type: ignore link_type=LinkTypes.OPTIONAL_BACK_DIRECT, ) @@ -296,9 +296,9 @@ def detect_link( if cls is BackLink: return LinkInfo( field_name=field_name, - lookup_field_name=field.json_schema_extra[ # type: ignore - "original_field" - ], + lookup_field_name=get_extra_field_info( + field, "original_field" + ), document_class=DocsRegistry.evaluate_fr(get_args(optional_args[0])[0]), # type: ignore link_type=LinkTypes.OPTIONAL_BACK_LIST, ) @@ -430,7 +430,9 @@ async def init_document_collection(self, cls): if ( document_settings.timeseries is not None and document_settings.name - not in await self.database.list_collection_names(authorizedCollections=True, nameOnly=True) + not in await self.database.list_collection_names( + authorizedCollections=True, nameOnly=True + ) ): collection = await self.database.create_collection( **document_settings.timeseries.build_query( @@ -642,7 +644,9 @@ async def init_view(self, cls: Type[View]): self.init_view_fields(cls) self.init_cache(cls) - collection_names = await self.database.list_collection_names(authorizedCollections=True, nameOnly=True) + collection_names = await self.database.list_collection_names( + authorizedCollections=True, nameOnly=True + ) if self.recreate_views or cls._settings.name not in collection_names: if cls._settings.name in collection_names: await cls.get_motor_collection().drop() diff --git a/docs/changelog.md b/docs/changelog.md index b355f9f8..49f17bf0 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -2,6 +2,17 @@ Beanie project +## [1.22.5] - 2023-09-13 + +### Fix: Unify Methods for Retrieving Field's Extra Parameters During Backlink Processing +- Author - [Roman Right](https://github.com/roman-right) +- PR + +- Issues: + - [[BUG] Optional[Backlink]](https://github.com/roman-right/beanie/issues/702) + +[1.22.5]: https://pypi.org/project/beanie/1.22.5 + ## [1.22.4] - 2023-09-13 ### Fix Numpy Array Incompatability diff --git a/pyproject.toml b/pyproject.toml index ba1621f9..0e4dc776 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "flit_core.buildapi" [project] name = "beanie" -version = "1.22.4" +version = "1.22.5" description = "Asynchronous Python ODM for MongoDB" readme = "README.md" requires-python = ">=3.7,<4.0" diff --git a/tests/odm/conftest.py b/tests/odm/conftest.py index 7e354e07..535a7d02 100644 --- a/tests/odm/conftest.py +++ b/tests/odm/conftest.py @@ -90,6 +90,8 @@ DocumentWithBsonBinaryField, DocumentWithRootModelAsAField, DocWithCallWrapper, + DocumentWithOptionalBackLink, + DocumentWithOptionalListBackLink, ) from tests.odm.views import ViewForTest, ViewForTestWithLink @@ -265,6 +267,8 @@ async def init(db): DocumentWithBsonBinaryField, DocumentWithRootModelAsAField, DocWithCallWrapper, + DocumentWithOptionalBackLink, + DocumentWithOptionalListBackLink, ] await init_beanie( database=db, diff --git a/tests/odm/models.py b/tests/odm/models.py index eac42199..788a09a4 100644 --- a/tests/odm/models.py +++ b/tests/odm/models.py @@ -885,6 +885,18 @@ class DocumentWithBackLink(Document): i: int = 1 +class DocumentWithOptionalBackLink(Document): + if IS_PYDANTIC_V2: + back_link: Optional[BackLink[DocumentWithLink]] = Field( + json_schema_extra={"original_field": "link"}, + ) + else: + back_link: Optional[BackLink[DocumentWithLink]] = Field( + original_field="link" + ) + i: int = 1 + + class DocumentWithListLink(Document): link: List[Link["DocumentWithListBackLink"]] s: str = "TEST" @@ -902,6 +914,18 @@ class DocumentWithListBackLink(Document): i: int = 1 +class DocumentWithOptionalListBackLink(Document): + if IS_PYDANTIC_V2: + back_link: Optional[List[BackLink[DocumentWithListLink]]] = Field( + json_schema_extra={"original_field": "link"}, + ) + else: + back_link: Optional[List[BackLink[DocumentWithListLink]]] = Field( + original_field="link" + ) + i: int = 1 + + class DocumentToBeLinked(Document): s: str = "TEST" diff --git a/tests/test_beanie.py b/tests/test_beanie.py index ff2c4778..6d8f006b 100644 --- a/tests/test_beanie.py +++ b/tests/test_beanie.py @@ -2,4 +2,4 @@ def test_version(): - assert __version__ == "1.22.4" + assert __version__ == "1.22.5"