Skip to content

Commit 2685fae

Browse files
committed
use class from self.__class__ if document_class does not exist
This is the case when BackLink data has been fetched using fetch_links=True
1 parent 8049c4e commit 2685fae

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

beanie/odm/fields.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,9 @@ def __modify_schema__(cls, field_schema: Dict[str, Any]):
611611
)
612612

613613
def to_dict(self) -> dict[str, str]:
614-
document_class = DocsRegistry.evaluate_fr(self.document_class) # type: ignore
614+
document_class = DocsRegistry.evaluate_fr(
615+
getattr(self, "document_class", self.__class__)
616+
)
615617
return {"collection": document_class.get_collection_name()}
616618

617619

tests/odm/test_beanie_object_dumping.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import pytest
22
from pydantic import BaseModel, Field
33

4-
from beanie import Link, PydanticObjectId
4+
from beanie import BackLink, Link, PydanticObjectId
55
from beanie.odm.utils.pydantic import IS_PYDANTIC_V2
6-
from tests.odm.models import DocumentTestModelWithSoftDelete
6+
from tests.odm.models import (
7+
DocumentTestModelWithSoftDelete,
8+
DocumentWithBackLink,
9+
DocumentWithLink,
10+
)
711

812

913
class TestModel(BaseModel):
@@ -20,6 +24,17 @@ def data_maker():
2024
)
2125

2226

27+
def data_maker_backlink_pair():
28+
back_link_doc = DocumentWithBackLink(
29+
id="5f4e3f3b7c0c9d001f7d4c8e",
30+
back_link=BackLink(document_class=DocumentWithLink),
31+
)
32+
link_doc = DocumentWithLink(
33+
id="5f4e3f3b7c0c9d001f7d4c8f", link=back_link_doc
34+
)
35+
return link_doc, back_link_doc
36+
37+
2338
@pytest.mark.skipif(
2439
not IS_PYDANTIC_V2,
2540
reason="model dumping support is more complete with pydantic v2",
@@ -38,3 +53,18 @@ def test_id_types_serialized_when_dumping_to_json():
3853
dumped = data_maker().model_dump(mode="json")
3954
assert isinstance(dumped["my_id"], str)
4055
assert isinstance(dumped["fake_doc"]["id"], str)
56+
57+
58+
@pytest.mark.skipif(
59+
not IS_PYDANTIC_V2,
60+
reason="model dumping support is more complete with pydantic v2",
61+
)
62+
def test_backlink_serialization_to_json_when_fetched():
63+
link_doc, back_link_doc = data_maker_backlink_pair()
64+
65+
# presume we have fetched this BackLink by using fetch_links=True, nesting_depth=1
66+
back_link_doc.back_link = link_doc
67+
68+
dumped_model = back_link_doc.model_dump(mode="json")
69+
70+
assert dumped_model["back_link"] == {"collection": "DocumentWithLink"}

tests/odm/test_relations.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,24 @@ async def test_with_chaining_aggregation(self):
614614

615615
assert addresses_count[0] == {"count": 10}
616616

617+
@pytest.mark.skipif(
618+
not IS_PYDANTIC_V2,
619+
reason="model dumping support is more complete with pydantic v2",
620+
)
621+
async def test_dump_model_with_fetched_backlink(
622+
self, link_and_backlink_doc_pair
623+
):
624+
link_doc, back_link_doc = link_and_backlink_doc_pair
625+
626+
document_with_fetched_backlinks = await DocumentWithBackLink.get(
627+
back_link_doc.id, fetch_links=True, nesting_depth=1
628+
)
629+
630+
assert document_with_fetched_backlinks is not None
631+
model_json = document_with_fetched_backlinks.model_dump(mode="json")
632+
633+
assert model_json["back_link"] == {"collection": "DocumentWithLink"}
634+
617635
async def test_with_chaining_aggregation_and_text_search(self):
618636
# ARRANGE
619637
NUM_DOCS = 10

0 commit comments

Comments
 (0)