From 12732efec8fd0d654f6fc333df127f1047740050 Mon Sep 17 00:00:00 2001 From: Mingxuan Lin Date: Thu, 5 Sep 2024 17:50:26 +0000 Subject: [PATCH] Fix: bug #1008 also for pydantic V1 --- beanie/odm/fields.py | 4 +++- tests/odm/test_relations.py | 12 ++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/beanie/odm/fields.py b/beanie/odm/fields.py index eda1961a..fb564496 100644 --- a/beanie/odm/fields.py +++ b/beanie/odm/fields.py @@ -462,8 +462,10 @@ def to_dict(self): if not IS_PYDANTIC_V2: ENCODERS_BY_TYPE[Link] = lambda o: o.to_dict() +# workaround bug #3390 from pydantic v1 +_BackLinkBases = [] if IS_PYDANTIC_V2 else [BaseModel,] -class BackLink(Generic[T]): +class BackLink(Generic[T], *_BackLinkBases): """Back reference to a document""" def __init__(self, document_class: Type[T]): diff --git a/tests/odm/test_relations.py b/tests/odm/test_relations.py index 0657faad..94964a80 100644 --- a/tests/odm/test_relations.py +++ b/tests/odm/test_relations.py @@ -842,15 +842,15 @@ async def test_write_list(self, list_link_and_list_backlink_doc_pair): def test_json_schema_export(self): if IS_PYDANTIC_V2: json_schema = DocumentWithListBackLink.model_json_schema() - else: - import json - - json_schema = json.loads(DocumentWithListBackLink.schema_json()) + else: + json_schema = DocumentWithListBackLink.schema() + assert ( - json_schema["properties"]["back_link"]["items"]["type"] == "object" + json_schema["properties"]["back_link"]["items"] + and + json_schema["properties"]["back_link"]["type"] == "array" ) - class HouseForReversedOrderInit(Document): name: str door: Link["DoorForReversedOrderInit"]