Skip to content

Schemas

Response schemas heavily rely on the concept of pydantic GenericModel.

In this way response_model can be wrapped by the ResponseSchema in the fastapi operation.

ResponseSchema will wrap your response ONLY if you configured a response_model in your route operation.

In order to create a response schema you need to make the class a Generic type.

from typing import TypeVar, Generic
from fastapi_responseschema import AbstractResponseSchema

T = TypeVar("T")


class ResponseSchema(AbstractResponseSchema[T], Generic[T]):
    ...

Constructors

When creating a response schema, constructors must be defined in subclass to ensure that the final response model gets correctly created and additional metadata can be passed to the final response.

AbstractResponseSchema.from_exception

This constructor wraps the final response from an exception handler. You can view the full parameter list here.

AbstractResponseSchema.from_api_route

This constructor wraps the final response when initializing an APIRoute. You can view the full parameter list here.

from typing import TypeVar, Generic
from fastapi_responseschema import AbstractResponseSchema

T = TypeVar("T")


class ResponseSchema(AbstractResponseSchema[T], Generic[T]):
    data: T
    error: bool
    message: str

    @classmethod
    def from_exception(cls, reason: T, status_code: int, message: str = "Error", **others):  # from an exception handler 
        return cls(
            data=reason,
            error=status_code >= 400, 
            message=message
        )

    @classmethod
    def from_api_route(
        cls, content: T, status_code: int, description: Optional[str] = None, **others
    ):  # from an api route
        return cls(
            data=content,
            error=status_code >= 400, 
            description=description
        )

Multiple response schemas can be built and composed in SchemaAPIRoute subclasses.