Skip to content

OpenAPIResponse class

ravyn.openapi.datastructures.OpenAPIResponse

Bases: BaseModel

The OpenAPIResponse is used for OpenAPI documentation purposes and allows to describe in detail what alternative responses the API can return as well as the type of the return itself.

Example

from typing import Union

from ravyn import post
from ravyn.openapi.datastructures import OpenAPIResponse
from pydantic import BaseModel


class ItemOut(BaseModel):
    sku: str
    description: str


@post(path='/create', summary="Creates an item", responses={200: OpenAPIResponse(model=ItemOut, description="Successfully created an item")})
async def create() -> Union[None, ItemOut]:
    ...

model instance-attribute

model

A pydantic.BaseModel type of object of a list of pydantic.BaseModel types of objects.

This is parsed and displayed in the OpenAPI documentation.

Example

from ravyn.openapi.datastructures import OpenAPIResponse
from pydantic import BaseModel


class Error(BaseModel):
    detail: str

# Single
OpenAPIResponse(model=Error)

# list
OpenAPIResponse(model=[Error])

description class-attribute instance-attribute

description = 'Additional response'

Description of the response.

This description is displayed in the OpenAPI documentation.

media_type class-attribute instance-attribute

media_type = JSON

The media-type of the response.

status_text class-attribute instance-attribute

status_text = None

Description of the status_code. The description of the status code itself.

This description is displayed in the OpenAPI documentation.

validate_model

validate_model(model)
Source code in ravyn/openapi/datastructures.py
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
@field_validator("model", mode="before")
def validate_model(
    cls,
    model: Union[
        Type[BaseModel],
        list[Type[BaseModel]],
        Type[Struct],
        list[Type[Struct]],
        Type[Any],
        list[Type[Any]],
    ],
) -> Union[
    Type[BaseModel],
    list[Type[BaseModel]],
    Type[Struct],
    list[Type[Struct]],
    Type[Any],
    list[Type[Any]],
]:
    if isinstance(model, list) and len(model) > 1:
        raise ValueError(
            "The representation of a list of models in OpenAPI can only be a total of one. Example: OpenAPIResponse(model=[MyModel])."
        )
    return model
    - "!^model_config"
    - "!^validate_model"