Skip to content

Commit

Permalink
Fix: bug BeanieODM#1008 also for pydantic V1
Browse files Browse the repository at this point in the history
  • Loading branch information
gdlmx committed Sep 5, 2024
1 parent 220d804 commit 12732ef
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
4 changes: 3 additions & 1 deletion beanie/odm/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Expand Down
12 changes: 6 additions & 6 deletions tests/odm/test_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit 12732ef

Please sign in to comment.