Handler

class tornado_openapi3.handler.OpenAPIRequestHandler(application: tornado.web.Application, request: tornado.httputil.HTTPServerRequest, **kwargs: Any)

Base class for HTTP request handlers.

A request handler extending tornado.web.RequestHandler providing OpenAPI spec validation on incoming requests and translating errors into appropriate HTTP responses.

property spec_dict: dict

The OpenAPI 3 specification

Override this in your request handlers to load or define your OpenAPI 3 spec.

Return type

dict

property spec: openapi_core.spec.paths.SpecPath

The OpenAPI 3 specification.

Override this in your request handlers to customize how your OpenAPI 3 spec is loaded and validated.

Return type

openapi_core.schema.specs.model.Spec

property custom_formatters: Mapping[str, tornado_openapi3.types.Formatter]

A dictionary mapping value formats to formatter objects.

If your schemas make use of format modifiers, you may specify them in this dictionary paired with a Formatter object that provides methods to validate values and unmarshal them into Python objects.

Return type

Mapping[str, Formatter]

property custom_media_type_deserializers: Mapping[str, Callable[[Union[bytes, str]], Any]]

A dictionary mapping media types to deserializing functions.

If your endpoints make use of content types beyond application/json, you must add them to this dictionary with a deserializing method that converts the raw body (as bytes or str) to Python objects.

Return type

Mapping[str, Deserializer]

async prepare() None

Called at the beginning of a request before get/post/etc.

Performs OpenAPI validation of the incoming request. Problems encountered while validating the request are translated to HTTP error codes:

OpenAPI Errors

Error Code

Description

PathNotFound

404

Could not find the path for this request in the OpenAPI specification.

OperationNotFound

405

Could not find the operation specified for this request in the OpenAPI specification.

CastError, DeserializeError, MissingRequiredParameter, MissingRequestBody, ValidateError

400

The message body could not be decoded or did not validate against the specified schema.

InvalidSecurity

401

Required authorization was missing from the request.

MediaTypeNotFound

415

The content type of the request did not match any of the types in the OpenAPI specification.

Any other OpenAPIError

500

An unexpected error occurred.

To provide content in these error requests, you may override on_openapi_error().

on_openapi_error(status_code: int, error: openapi_core.exceptions.OpenAPIError) None

Sets an HTTP status code and finishes the request.

By default, no content is returned. To provide more informative responses, you may override this method.