From db127718963e5aa1e54d86f5ff422cca1de94da8 Mon Sep 17 00:00:00 2001 From: Mingxuan Lin Date: Thu, 5 Sep 2024 13:46:41 +0000 Subject: [PATCH 1/3] Fix: bug #1008 in BackLink.model_json_schema() https://github.com/BeanieODM/beanie/issues/1008 add test for BackLink json_schema --- beanie/odm/fields.py | 12 +++++++++++- tests/odm/test_relations.py | 3 +++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/beanie/odm/fields.py b/beanie/odm/fields.py index c2a95e77..e2f0ca6a 100644 --- a/beanie/odm/fields.py +++ b/beanie/odm/fields.py @@ -487,7 +487,17 @@ def validate(v: Union[DBRef, T], field): def __get_pydantic_core_schema__( cls, source_type: Any, handler: GetCoreSchemaHandler ) -> CoreSchema: # type: ignore - return plain_validator(cls.build_validation(handler, source_type)) + # build_validation can return any object + # so json_schema should be a general dict + return core_schema.json_or_python_schema( + python_schema = plain_validator( + cls.build_validation(handler, source_type), + ), + json_schema = core_schema.dict_schema( + keys_schema=core_schema.str_schema(), + values_schema=core_schema.any_schema() + ), + ) else: diff --git a/tests/odm/test_relations.py b/tests/odm/test_relations.py index 5f1ce7d9..e4191166 100644 --- a/tests/odm/test_relations.py +++ b/tests/odm/test_relations.py @@ -839,6 +839,9 @@ async def test_write_list(self, list_link_and_list_backlink_doc_pair): for lnk in new_back_link_doc.back_link: assert lnk.s == "new value" + def test_json_schema_export(self): + json_schema = DocumentWithListBackLink.model_json_schema() + assert json_schema['properties']['back_link']['items']['type'] == 'object' class HouseForReversedOrderInit(Document): name: str From 220d804eafc98800bd07d331a1592073e1897833 Mon Sep 17 00:00:00 2001 From: Mingxuan Lin Date: Thu, 5 Sep 2024 14:52:52 +0000 Subject: [PATCH 2/3] Fix: bug #1008 and test for json schema --- beanie/odm/fields.py | 10 +++++----- tests/odm/test_relations.py | 12 ++++++++++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/beanie/odm/fields.py b/beanie/odm/fields.py index e2f0ca6a..eda1961a 100644 --- a/beanie/odm/fields.py +++ b/beanie/odm/fields.py @@ -488,14 +488,14 @@ def __get_pydantic_core_schema__( cls, source_type: Any, handler: GetCoreSchemaHandler ) -> CoreSchema: # type: ignore # build_validation can return any object - # so json_schema should be a general dict + # so json_schema should be a general dict return core_schema.json_or_python_schema( - python_schema = plain_validator( + python_schema=plain_validator( cls.build_validation(handler, source_type), ), - json_schema = core_schema.dict_schema( - keys_schema=core_schema.str_schema(), - values_schema=core_schema.any_schema() + json_schema=core_schema.dict_schema( + keys_schema=core_schema.str_schema(), + values_schema=core_schema.any_schema(), ), ) diff --git a/tests/odm/test_relations.py b/tests/odm/test_relations.py index e4191166..0657faad 100644 --- a/tests/odm/test_relations.py +++ b/tests/odm/test_relations.py @@ -840,8 +840,16 @@ async def test_write_list(self, list_link_and_list_backlink_doc_pair): assert lnk.s == "new value" def test_json_schema_export(self): - json_schema = DocumentWithListBackLink.model_json_schema() - assert json_schema['properties']['back_link']['items']['type'] == 'object' + if IS_PYDANTIC_V2: + json_schema = DocumentWithListBackLink.model_json_schema() + else: + import json + + json_schema = json.loads(DocumentWithListBackLink.schema_json()) + assert ( + json_schema["properties"]["back_link"]["items"]["type"] == "object" + ) + class HouseForReversedOrderInit(Document): name: str From 4a40b38e4f607af79bc5f125325c69831f2e4b68 Mon Sep 17 00:00:00 2001 From: Mingxuan Lin Date: Thu, 5 Sep 2024 17:50:26 +0000 Subject: [PATCH 3/3] disable test for pydantic V1 due to https://github.com/pydantic/pydantic/issues/3390 --- tests/odm/test_relations.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/odm/test_relations.py b/tests/odm/test_relations.py index 0657faad..3537fae4 100644 --- a/tests/odm/test_relations.py +++ b/tests/odm/test_relations.py @@ -843,11 +843,12 @@ def test_json_schema_export(self): if IS_PYDANTIC_V2: json_schema = DocumentWithListBackLink.model_json_schema() else: - import json + return # schema does not work in pydantic v1 due to pydantic bug #3390 + json_schema = DocumentWithListBackLink.schema() - json_schema = json.loads(DocumentWithListBackLink.schema_json()) assert ( - json_schema["properties"]["back_link"]["items"]["type"] == "object" + json_schema["properties"]["back_link"]["items"] + and json_schema["properties"]["back_link"]["type"] == "array" )