Skip to content

Commit

Permalink
Fix: unified methods for getting extra parameters of the field during…
Browse files Browse the repository at this point in the history
… backlinks processing (#703)

* fix: optional backlinks

* version: 1.22.5
  • Loading branch information
roman-right committed Sep 14, 2023
1 parent 19e5ade commit d6f4223
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 11 deletions.
2 changes: 1 addition & 1 deletion beanie/__init__.py
Expand Up @@ -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",
Expand Down
20 changes: 12 additions & 8 deletions beanie/odm/utils/init.py
Expand Up @@ -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,
)
Expand All @@ -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,
)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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()
Expand Down
11 changes: 11 additions & 0 deletions docs/changelog.md
Expand Up @@ -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 <https://github.com/roman-right/beanie/pull/703>

- 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
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions tests/odm/conftest.py
Expand Up @@ -90,6 +90,8 @@
DocumentWithBsonBinaryField,
DocumentWithRootModelAsAField,
DocWithCallWrapper,
DocumentWithOptionalBackLink,
DocumentWithOptionalListBackLink,
)
from tests.odm.views import ViewForTest, ViewForTestWithLink

Expand Down Expand Up @@ -265,6 +267,8 @@ async def init(db):
DocumentWithBsonBinaryField,
DocumentWithRootModelAsAField,
DocWithCallWrapper,
DocumentWithOptionalBackLink,
DocumentWithOptionalListBackLink,
]
await init_beanie(
database=db,
Expand Down
24 changes: 24 additions & 0 deletions tests/odm/models.py
Expand Up @@ -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"
Expand All @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion tests/test_beanie.py
Expand Up @@ -2,4 +2,4 @@


def test_version():
assert __version__ == "1.22.4"
assert __version__ == "1.22.5"

0 comments on commit d6f4223

Please sign in to comment.