flask_openapi.extension

This module contains the actual Flask extension.

class flask_openapi.extension.OpenAPI(app=None)

The Flask extension that handles all magic.

Parameters:app (flask.Flask) – The Flask app to apply this extension to.
add_definition(definition, name=None)

Add a new named definition.

Parameters:
  • definition – A dict describing a Swagger schemaObject. This may also be a str or pathlib.Path referring to a file to read.
  • name (str) – A name for the schema. If this is not specified, the title of the schema definition will be used.
add_response(name, response)

Define a possible response to be reused accross operations.

Parameters:
base_path

str – The relative URL prefix for all API calls.

See Swagger swaggerBasePath for details.

definitions

dict – The top level Swagger definitionsObject.

deprecated(fn)

Mark an operation as deprecated.

This will be exposed through the OpenAPI operation object. Additionally a warning will be emitted when the API is used. This can be configured using the OPENAPI_WARN_DEPRECATED configuration option. This must be one of warn or log.

See Swagger operationDeprecated.

host

str – The server host name.

This is only returned if show_host is True.

See Swagger swaggerHost for details.

info

dict – The top level Swagger infoObject.

init_app(app)

Initialize the OpenAPI instance for the given app.

This should be used for a deferred initialization, supporting the Flask factory pattern.

Parameters:app (flask.Flask) – The Flask app to apply this extension to.
paths

dict – The top level Swagger pathsObject.

response(status_code, response)

Describe a possible response for a Flask handler.

Parameters:
responses

dict – The top level Swagger responsesObject.

schema(schema)

A decorator to validate a request using a Swagger schemaObject.

Parameters:schema – Either a dict to use as a schema directly or a str referring to a named schema. (See add_definition.)
schemes

list – The supported schemes for all API calls.

See Swagger swaggerSchemes for details.

swagger

dict – The top level Swagger swaggerObject.

tag(*tags)

Tag an operation using one or more tags.

These tags are exposed through the OpenAPI operation object.

Parameters:*tags (str) – The tags to apply to the operation.
tags

dict – A list of tag descriptions.

See Swagger tagObject.

validatorgetter(fn)

Mark a function as a getter function to get a validator.

The function will be called with the JSON schema as a dict.

exception flask_openapi.extension.UnknownDefinitionError(name)

Raised when trying to get a definition which doesn’t exist.

Parameters:name (str) – The definition that could not be found.
exception flask_openapi.extension.UnnamedDefinitionError(definition)

Raised when trying to add a definition which has no title.

Parameters:definition (dict) – The unnamed JSON schema definition.