openapi#
- class litestar.openapi.OpenAPIConfig#
Bases:
objectConfiguration for OpenAPI.
To enable OpenAPI schema generation and serving, pass an instance of this class to the
Litestarconstructor using theopenapi_configkwargs.- __init__(title: str, version: str, create_examples: bool = False, random_seed: int = 10, contact: Contact | None = None, description: str | None = None, external_docs: ExternalDocumentation | None = None, license: License | None = None, security: list[SecurityRequirement] | None = None, components: Components | list[Components] = <factory>, servers: list[Server] = <factory>, summary: str | None = None, tags: list[Tag] | None = None, terms_of_service: str | None = None, use_handler_docstrings: bool = False, webhooks: dict[str, PathItem | Reference] | None = None, operation_id_creator: OperationIDCreator = <function default_operation_id_creator>, path: str | None = None, render_plugins: Sequence[OpenAPIRenderPlugin] = (), openapi_router: Router | None = None, openapi_controller: type[OpenAPIController] | None = None, root_schema_site: Literal['redoc', 'swagger', 'elements', 'rapidoc'] | None = None, enabled_endpoints: set[str] | None = None) None#
- enabled_endpoints: set[str] | None = None#
A set of the enabled documentation sites and schema download endpoints.
Deprecated since version v2.8.0.
- external_docs: ExternalDocumentation | None = None#
Links to external documentation.
Should be an instance of
ExternalDocumentation.
- openapi_controller: type[OpenAPIController] | None = None#
Controller for generating OpenAPI routes.
Must be subclass of
OpenAPIController.Deprecated since version v2.8.0.
- openapi_router: Router | None = None#
An optional router for serving OpenAPI documentation and schema files.
If provided,
pathis ignored.This parameter is also ignored if the deprecated
openapi_routerkwarg is provided.openapi_routeris not required, but it can be passed to customize the configuration of the router used to serve the documentation endpoints. For example, you can add middleware or guards to the router.Handlers to serve the OpenAPI schema and documentation sites are added to this router according to
render_plugins, so routes shouldn’t be added that conflict with these.
- operation_id_creator(http_method: Method, path_components: list[str | PathParameterDefinition]) str#
Create a unique ‘operationId’ for an OpenAPI PathItem entry.
- path: str | None = None#
Base path for the OpenAPI documentation endpoints.
If no path is provided the default is
/schema.Ignored if
openapi_routeris provided.
- random_seed: int = 10#
The random seed used when creating the examples to ensure deterministic generation of examples.
- render_plugins: Sequence[OpenAPIRenderPlugin] = ()#
Plugins for rendering OpenAPI documentation UIs.
- root_schema_site: Literal['redoc', 'swagger', 'elements', 'rapidoc'] | None = None#
The static schema generator to use for the “root” path of
/schema/.Deprecated since version v2.8.0.
- security: list[SecurityRequirement] | None = None#
API Security requirements information.
- Should be an instance of
- to_openapi_schema() OpenAPI#
Return an
OpenAPIinstance from the values stored inself.- Returns:
An instance of
OpenAPI.
- use_handler_docstrings: bool = False#
Draw operation description from route handler docstring if not otherwise provided.
- webhooks: dict[str, PathItem | Reference] | None = None#
A mapping of key to either
PathItemor.Referenceobjects.
- components: Components | list[Components]#
API Components information.
Should be an instance of
Componentsor a list thereof.
- class litestar.openapi.OpenAPIController#
Bases:
ControllerController for OpenAPI endpoints.
- after_request: AfterRequestHookHandler | None#
A sync or async function executed before a
Requestis passed to any route handler.If this function returns a value, the request will not reach the route handler, and instead this value will be used.
- after_response: AfterResponseHookHandler | None#
A sync or async function called after the response has been awaited.
It receives the
Requestinstance and should not return any values.
- before_request: BeforeRequestHookHandler | None#
A sync or async function called immediately before calling the route handler.
It receives the
Requestinstance and any non-Nonereturn value is used for the response, bypassing the route handler.
- cache_control: CacheControlHeader | None#
A
CacheControlHeaderheader to add to route handlers of this controller.Can be overridden by route handlers.
- dependencies: Dependencies | None#
A string keyed dictionary of dependency
Providerinstances.
- dto: type[AbstractDTO] | None | EmptyType#
AbstractDTOto use for (de)serializing and validation of request data.
- etag: ETag | None#
An
etagheader of typeETagto add to route handlers of this controller.Can be overridden by route handlers.
- exception_handlers: ExceptionHandlersMap | None#
A map of handler functions to status codes and/or exception types.
- include_in_schema: bool | EmptyType#
A boolean flag dictating whether the route handler should be documented in the OpenAPI schema
- middleware: Sequence[Middleware] | None#
A sequence of
Middleware.
- opt: Mapping[str, Any] | None#
A string key mapping of arbitrary values that can be accessed in
Guardsor wherever you have access toRequestorASGI Scope.
- owner: Router#
The
RouterorLitestarapp that owns the controller.This value is set internally by Litestar and it should not be set when subclassing the controller.
- parameters: ParametersMap | None#
A mapping of
Parameterdefinitions available to all application paths.
- request_class: type[Request] | None#
A custom subclass of
Requestto be used as the default request for all route handlers under the controller.
- request_max_body_size: int | None | EmptyType#
Maximum allowed size of the request body in bytes. If this size is exceeded, a ‘413 - Request Entity Too Large’ error response is returned.
- response_class: type[Response] | None#
A custom subclass of
Responseto be used as the default response for all route handlers under the controller.
- response_cookies: ResponseCookies | None#
A list of
Cookieinstances.
- response_headers: ResponseHeaders | None#
A string keyed dictionary mapping
ResponseHeaderinstances.
- return_dto: type[AbstractDTO] | None | EmptyType#
AbstractDTOto use for serializing outbound response data.
- security: Sequence[SecurityRequirement] | None#
A sequence of dictionaries that to the schema of all route handlers under the controller.
- signature_namespace: dict[str, Any]#
A mapping of names to types for use in forward reference resolution during signature modelling.
- signature_types: Sequence[Any]#
A sequence of types for use in forward reference resolution during signature modelling.
These types will be added to the signature namespace using their
__name__attribute.
- tags: Sequence[str] | None#
A sequence of string tags that will be appended to the schema of all route handlers under the controller.
- type_decoders: TypeDecodersSequence | None#
A sequence of tuples, each composed of a predicate testing for type identity and a msgspec hook for deserialization.
- type_encoders: TypeEncodersMap | None#
A mapping of types to callables that transform them into types supported for serialization.
- websocket_class: type[WebSocket] | None#
A custom subclass of
WebSocketto be used as the default websocket for all route handlers under the controller.
- property favicon: str#
Return favicon
<link>tag, if applicable.- Returns:
A
<link>tag ifself.favicon_urlis not empty, otherwise returns a placeholder meta tag.
- static get_schema_from_request(request: Request[Any, Any, Any]) OpenAPI#
Return the OpenAPI pydantic model from the request instance.
- rapidoc_js_url: str = 'https://unpkg.com/rapidoc@9.3.4/dist/rapidoc-min.js'#
Download url for the RapiDoc JS bundle.
- redoc_google_fonts: bool = True#
Download google fonts via CDN.
Should be set to
Falsewhen not using a CDN.
- redoc_js_url: str = 'https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js'#
Download url for the Redoc JS bundle.
- property render_methods_map: dict[Literal['redoc', 'swagger', 'elements', 'rapidoc'], Callable[[Request], bytes]]#
Map render method names to render methods.
- Returns:
A mapping of string keys to render methods.
- render_redoc(request: Request[Any, Any, Any]) bytes#
Render an HTML page for Redoc.
Notes
override this method to customize the template.
- render_stoplight_elements(request: Request[Any, Any, Any]) bytes#
Render an HTML page for StopLight Elements.
Notes
override this method to customize the template.
- render_swagger_ui(request: Request[Any, Any, Any]) bytes#
Render an HTML page for Swagger-UI.
Notes
override this method to customize the template.
- render_swagger_ui_oauth2_redirect(request: Request[Any, Any, Any]) bytes#
Render an HTML oauth2-redirect.html page for Swagger-UI.
Notes
override this method to customize the template.
- should_serve_endpoint(request: Request[Any, Any, Any]) bool#
Verify that the requested path is within the enabled endpoints in the openapi_config.
- Parameters:
request¶ – To be tested if endpoint enabled.
- Returns:
A boolean.
- Raises:
ImproperlyConfiguredException – If the application
openapi_configattribute isNone.
- stoplight_elements_css_url: str = 'https://unpkg.com/@stoplight/elements@7.7.18/styles.min.css'#
Download url for the Stoplight Elements CSS bundle.
- stoplight_elements_js_url: str = 'https://unpkg.com/@stoplight/elements@7.7.18/web-components.min.js'#
Download url for the Stoplight Elements JS bundle.
- swagger_css_url: str = 'https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.18.2/swagger-ui.css'#
Download url for the Swagger UI CSS bundle.
- swagger_ui_bundle_js_url: str = 'https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.18.2/swagger-ui-bundle.js'#
Download url for the Swagger UI JS bundle.
- swagger_ui_init_oauth: dict[Any, Any] | bytes = {}#
JSON to initialize Swagger UI OAuth2 by calling the initOAuth method.
Refer to the following URL for details: Swagger-UI.
- class litestar.openapi.ResponseSpec#
Bases:
objectContainer type of additional responses.