beanie icon indicating copy to clipboard operation
beanie copied to clipboard

[BUG] BackLink causes error when using model_json_schema()

Open gdlmx opened this issue 6 months ago • 6 comments

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 avatar Aug 26 '24 12:08 gdlmx