Git Product home page Git Product logo

Comments (4)

dantetemplar avatar dantetemplar commented on June 15, 2024

Actually, I've struggled with json schema for beanie Link....

That's my solution for replacing _id with id in FastAPI, and dividing Input and Output shemas for Generic Link

__all__ = ["CustomDocument", "CustomLink"]

from typing import Type, Any, TypeVar, get_args

from beanie import Document, PydanticObjectId, Link
from beanie.odm.registry import DocsRegistry
from pydantic import Field, ConfigDict, GetCoreSchemaHandler
from pydantic_core import CoreSchema, core_schema


class CustomDocument(Document):
    model_config = ConfigDict(json_schema_extra={})

    id: PydanticObjectId | None = Field(default=None, description="MongoDB document ObjectID", serialization_alias="id")

    class Settings:
        keep_nulls = False
        max_nesting_depth = 1


D = TypeVar("D", bound=Document)


class CustomLink(Link[D]):
    @classmethod
    def __get_pydantic_core_schema__(cls, source_type: Type[Any], handler: GetCoreSchemaHandler) -> CoreSchema:
        document_class = DocsRegistry.evaluate_fr(get_args(source_type)[0])
        document_class: Type[Document]

        serialization_schema = core_schema.plain_serializer_function_ser_schema(
            lambda instance: cls.serialize(instance),
            return_schema=core_schema.union_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()),
                        }
                    ),
                    document_class.__pydantic_core_schema__,
                ],
            ),
        )

        schema = core_schema.json_or_python_schema(
            python_schema=core_schema.with_info_plain_validator_function(cls.build_validation(handler, source_type)),
            json_schema=core_schema.with_default_schema(core_schema.str_schema(), default="5eb7cf5a86d9755df3a6c593"),
            serialization=serialization_schema,
        )

        return schema

from beanie.

github-actions avatar github-actions commented on June 15, 2024

This issue is stale because it has been open 30 days with no activity.

from beanie.

github-actions avatar github-actions commented on June 15, 2024

This issue was closed because it has been stalled for 14 days with no activity.

from beanie.

dantetemplar avatar dantetemplar commented on June 15, 2024

someday I will create PR...

from beanie.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.