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

[BUG] BackLink causes error when using model_json_schema() #1008

Open
gdlmx opened this issue Aug 26, 2024 · 6 comments
Open

[BUG] BackLink causes error when using model_json_schema() #1008

gdlmx opened this issue Aug 26, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@gdlmx
Copy link

gdlmx commented Aug 26, 2024

Describe the bug
Generating Json Schema from a document with a BackLink field will fail with the error: PydanticInvalidForJsonSchema.

To Reproduce

from typing import List
from pydantic import Field
from beanie import Document, Link, BackLink

class Task(Document):
    detail: str
    owner:  Link["User"]

class User(Document):
    name: str
    tasks: List[BackLink["Task"]] = Field(json_schema_extra={"original_field": "owner"})

User.model_json_schema()

The last line will raise error:

PydanticInvalidForJsonSchema: Cannot generate a JsonSchema for core_schema.PlainValidatorFunctionSchema ({'type': 'with-info', 'function': <function BackLink.build_validation.<locals>.validate at 0x7f89140bdd00>})

Expected behavior
The last line (model_json_schema) should return

{
  "properties": {
    "_id": { "...":"..." },
    "name": {  "title": "Name",  "type": "string"  },
    "tasks": { "...":"..." }
  },
  "required": [ "name", "tasks" ],
  "title": "User",
  "type": "object"
}

Additional context
Python version: 3.11
Beanie version: 1.26.0
Pydantic version: 2.8.2

@gdlmx
Copy link
Author

gdlmx commented Aug 26, 2024

Possible cause of this error:

beanie/beanie/odm/fields.py

Lines 473 to 479 in 51e73eb

@classmethod
def __get_pydantic_core_schema__(
cls, source_type: Any, handler: GetCoreSchemaHandler
) -> CoreSchema: # type: ignore
return core_schema.with_info_plain_validator_function(
cls.build_validation(handler, source_type)
)

See this discussion of a similar error with Pydantic v2.4

A correct implementation already exists on the Link class.

beanie/beanie/odm/fields.py

Lines 408 to 430 in bdb6a86

@classmethod
def __get_pydantic_core_schema__(
cls, source_type: Any, handler: GetCoreSchemaHandler
) -> CoreSchema: # type: ignore
return core_schema.json_or_python_schema(
python_schema=plain_validator(
cls.build_validation(handler, source_type)
),
json_schema=core_schema.typed_dict_schema(
{
"id": core_schema.typed_dict_field(
core_schema.str_schema()
),
"collection": core_schema.typed_dict_field(
core_schema.str_schema()
),
}
),
serialization=core_schema.plain_serializer_function_ser_schema( # type: ignore
lambda instance: cls.serialize(instance),
when_used="json", # type: ignore
),
)

@adeelsohailahmed adeelsohailahmed added the bug Something isn't working label Aug 27, 2024
@adeelsohailahmed
Copy link
Contributor

Thanks for reporting the issue, and taking the time to investigate the cause. Would you like to send a PR to fix this issue, @gdlmx?

@xalien10
Copy link

@adeelsohailahmed @gdlmx Any updated about this fix yet?

@gdlmx
Copy link
Author

gdlmx commented Aug 28, 2024

Thanks for reporting the issue, and taking the time to investigate the cause. Would you like to send a PR to fix this issue, @gdlmx?

Yes, I can prepare a PR to fix it. Could you provide me some idea about the proper json schema for BackLink?

Given this line

return parse_obj(document_class, v)

There is no guarantee of any field in a BackLink object, is there?

@adeelsohailahmed
Copy link
Contributor

There is no guarantee of any field in a BackLink object, is there?

I'm not sure if I fully understand your question here. Could you elaborate more?

@xalien10
Copy link

xalien10 commented Sep 2, 2024

@gdlmx is there any update about the PR? :-)

gdlmx pushed a commit to gdlmx/beanie that referenced this issue Sep 5, 2024
add test for BackLink json_schema
gdlmx added a commit to gdlmx/beanie that referenced this issue Sep 5, 2024
gdlmx added a commit to gdlmx/beanie that referenced this issue Sep 5, 2024
gdlmx added a commit to gdlmx/beanie that referenced this issue Sep 5, 2024
gdlmx added a commit to gdlmx/beanie that referenced this issue Sep 5, 2024
gdlmx added a commit to gdlmx/beanie that referenced this issue Sep 5, 2024
include a base class for BackLink due to a bug in
pydantic v1
pydantic/pydantic#3390
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants