Skip to content
Discussion options

You must be logged in to vote

FastAPI doesn't know how to deal with such type of field. So, you need to manually define schema, validator and serializer for it.

Something like this:

from typing import Type, Annotated

from fastapi import FastAPI
from pydantic import BaseModel, field_serializer, field_validator
from pydantic.json_schema import WithJsonSchema


class MyType(BaseModel):
    a: str


class Container(BaseModel):
    T: Annotated[Type, WithJsonSchema({"type": "string"})]

    @field_validator("T", mode="plain")
    @classmethod
    def validate_type(cls, value: Type) -> Type:
        if isinstance(value, type):
            return value
        elif isinstance(value, str):
            try:
                re…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem question-migrate
3 participants
Converted from issue

This discussion was converted from issue #3275 on February 28, 2023 16:53.