Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion beanie/odm/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,9 @@ def __modify_schema__(cls, field_schema: Dict[str, Any]):
)

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


Expand Down
34 changes: 32 additions & 2 deletions tests/odm/test_beanie_object_dumping.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import pytest
from pydantic import BaseModel, Field

from beanie import Link, PydanticObjectId
from beanie import BackLink, Link, PydanticObjectId
from beanie.odm.utils.pydantic import IS_PYDANTIC_V2
from tests.odm.models import DocumentTestModelWithSoftDelete
from tests.odm.models import (
DocumentTestModelWithSoftDelete,
DocumentWithBackLink,
DocumentWithLink,
)


class TestModel(BaseModel):
Expand All @@ -20,6 +24,17 @@ def data_maker():
)


def data_maker_backlink_pair():
back_link_doc = DocumentWithBackLink(
id="5f4e3f3b7c0c9d001f7d4c8e",
back_link=BackLink(document_class=DocumentWithLink),
)
link_doc = DocumentWithLink(
id="5f4e3f3b7c0c9d001f7d4c8f", link=back_link_doc
)
return link_doc, back_link_doc


@pytest.mark.skipif(
not IS_PYDANTIC_V2,
reason="model dumping support is more complete with pydantic v2",
Expand All @@ -38,3 +53,18 @@ def test_id_types_serialized_when_dumping_to_json():
dumped = data_maker().model_dump(mode="json")
assert isinstance(dumped["my_id"], str)
assert isinstance(dumped["fake_doc"]["id"], str)


@pytest.mark.skipif(
not IS_PYDANTIC_V2,
reason="model dumping support is more complete with pydantic v2",
)
def test_backlink_serialization_to_json_when_fetched():
link_doc, back_link_doc = data_maker_backlink_pair()

# presume we have fetched this BackLink by using fetch_links=True, nesting_depth=1
back_link_doc.back_link = link_doc

dumped_model = back_link_doc.model_dump(mode="json")

assert dumped_model["back_link"] == {"collection": "DocumentWithLink"}
18 changes: 18 additions & 0 deletions tests/odm/test_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,24 @@ async def test_with_chaining_aggregation(self):

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

@pytest.mark.skipif(
not IS_PYDANTIC_V2,
reason="model dumping support is more complete with pydantic v2",
)
async def test_dump_model_with_fetched_backlink(
self, link_and_backlink_doc_pair
):
link_doc, back_link_doc = link_and_backlink_doc_pair

document_with_fetched_backlinks = await DocumentWithBackLink.get(
back_link_doc.id, fetch_links=True, nesting_depth=1
)

assert document_with_fetched_backlinks is not None
model_json = document_with_fetched_backlinks.model_dump(mode="json")

assert model_json["back_link"] == {"collection": "DocumentWithLink"}

async def test_with_chaining_aggregation_and_text_search(self):
# ARRANGE
NUM_DOCS = 10
Expand Down