Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug #1008 in the json_schema export of document with BackLink #1019

Closed
wants to merge 3 commits into from
Closed
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
12 changes: 11 additions & 1 deletion beanie/odm/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
12 changes: 12 additions & 0 deletions tests/odm/test_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,18 @@ 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):
if IS_PYDANTIC_V2:
json_schema = DocumentWithListBackLink.model_json_schema()
else:
return # schema does not work in pydantic v1 due to pydantic bug #3390
json_schema = DocumentWithListBackLink.schema()

assert (
json_schema["properties"]["back_link"]["items"]
and json_schema["properties"]["back_link"]["type"] == "array"
)


class HouseForReversedOrderInit(Document):
name: str
Expand Down
Loading