starlite.openapi#

class starlite.openapi.controller.OpenAPIController#

Bases: Controller

Controller for OpenAPI endpoints.

path: str#

Base path for the OpenAPI documentation endpoints.

style: str = 'body { margin: 0; padding: 0 }'#

Base styling of the html body.

redoc_version: str = 'next'#

Redoc version to download from the CDN.

swagger_ui_version: str = '4.15.5'#

SwaggerUI version to download from the CDN.

stoplight_elements_version: str = '7.7.5'#

StopLight Elements version to download from the CDN.

favicon_url: str = ''#

URL to download a favicon from.

redoc_google_fonts: bool = True#

Download google fonts via CDN.

Should be set to False when 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.

swagger_css_url: str = 'https://cdn.jsdelivr.net/npm/swagger-ui-dist@4.15.5/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@4.15.5/swagger-ui-bundle.js'#

Download url for the Swagger UI JS bundle.

swagger_ui_standalone_preset_js_url: str = 'https://cdn.jsdelivr.net/npm/swagger-ui-dist@4.15.5/swagger-ui-standalone-preset.js'#

Download url for the Swagger Standalone Preset JS bundle.

stoplight_elements_css_url: str = 'https://unpkg.com/@stoplight/elements@7.7.5/styles.min.css'#

Download url for the Stoplight Elements CSS bundle.

stoplight_elements_js_url: str = 'https://unpkg.com/@stoplight/elements@7.7.5/web-components.min.js'#

Download url for the Stoplight Elements JS bundle.

static get_schema_from_request(request: Request) OpenAPI#

Return the OpenAPI pydantic model from the request instance.

Parameters:

request – A Starlite instance.

Returns:

An OpenAPI instance.

Raises:

ImproperlyConfiguredException – If the application openapi_config attribute is None.

after_request: Optional['AfterRequestHookHandler']#

A sync or async function executed before a Request is 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: Optional['AfterResponseHookHandler']#

A sync or async function called after the response has been awaited.

It receives the Request instance and should not return any values.

before_request: Optional['BeforeRequestHookHandler']#

A sync or async function called immediately before calling the route handler.

It receives the Request instance and any non-None return value is used for the response, bypassing the route handler.

dependencies: Optional['Dependencies']#

A string keyed dictionary of dependency Provider instances.

Type:

dependencies

etag: Optional['ETag']#

An etag header of type ETag to add to route handlers of this controller.

Can be overridden by route handlers.

exception_handlers: Optional['ExceptionHandlersMap']#

A dictionary that maps handler functions to status codes and/or exception types.

guards: Optional[List['Guard']]#

A list of Guard callables.

middleware: Optional[List['Middleware']]#

A list of Middleware.

opt: Optional[Dict[str, Any]]#

A string key dictionary of arbitrary values that can be accessed in Guards or wherever you have access to Request or ASGI Scope.

owner: Router#

The Router or Starlite app that owns the controller.

This value is set internally by Starlite and it should not be set when subclassing the controller.

parameters: Optional['ParametersMap']#

A mapping of Parameter definitions available to all application paths.

response_class: Optional['ResponseType']#

A custom subclass of [starlite.response.Response] to be used as the default response for all route handlers under the controller.

response_cookies: Optional['ResponseCookies']#

A list of [Cookie](starlite.datastructures.Cookie] instances.

response_headers: Optional['ResponseHeadersMap']#

A string keyed dictionary mapping ResponseHeader instances.

security: Optional[List['SecurityRequirement']]#

A list of dictionaries that to the schema of all route handlers under the controller.

tags: Optional[List[str]]#

A list of string tags that will be appended to the schema of all route handlers under the controller.

type_encoders: Optional['TypeEncodersMap']#

A mapping of types to callables that transform them into types supported for serialization.

should_serve_endpoint(request: Request) 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_config attribute is None.

property favicon: str#

Return favicon <link> tag, if applicable.

Returns:

A <link> tag if self.favicon_url is not empty, otherwise returns a placeholder meta tag.

property render_methods_map: Dict[Literal['redoc', 'swagger', 'elements'], Callable[[Request], str]]#

Map render method names to render methods.

Returns:

A mapping of string keys to render methods.

render_swagger_ui(request: Request) str#

Render an HTML page for Swagger-UI.

Notes

  • override this method to customize the template.

Parameters:

request – A Request instance.

Returns:

A rendered html string.

render_stoplight_elements(request: Request) str#

Render an HTML page for StopLight Elements.

Notes

  • override this method to customize the template.

Parameters:

request – A Request instance.

Returns:

A rendered html string.

render_redoc(request: Request) str#

Render an HTML page for Redoc.

Notes

  • override this method to customize the template.

Parameters:

request – A Request instance.

Returns:

A rendered html string.

render_404_page() str#

Render an HTML 404 page.

Returns:

A rendered html string.

class starlite.openapi.datastructures.ResponseSpec#

Bases: BaseModel

Container type of additional responses.

model: Type[BaseModel]#

A model that describes the content of the response.

generate_examples: bool#

Generate examples for the response content.

description: str#

A description of the response.

media_type: MediaType#

Response media type.