Skip to content

OpenAPIResponse class

esmerald.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 esmerald import post
from esmerald.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 esmerald.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.