Core API

apispec

Contains main apispec classes: APISpec and BasePlugin

class apispec.APISpec(title, version, openapi_version, plugins=(), **options)[source]

Stores metadata that describes a RESTful API using the OpenAPI specification.

Parameters
path(path=None, operations=None, summary=None, description=None, parameters=None, **kwargs)[source]

Add a new path object to the spec.

https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#path-item-object

Parameters
  • path (str|None) – URL path component

  • operations (dict|None) – describes the http methods and options for path

  • summary (str) – short summary relevant to all operations in this path

  • description (str) – long description relevant to all operations in this path

  • parameters (list|None) – list of parameters relevant to all operations in this path

  • kwargs (dict) – parameters used by any path helpers see register_path_helper()

tag(tag)[source]

Store information about a tag.

Parameters

tag (dict) – the dictionary storing information about the tag.

to_yaml()[source]

Render the spec to YAML. Requires PyYAML to be installed.

class apispec.BasePlugin[source]

Base class for APISpec plugin classes.

init_spec(spec)[source]

Initialize plugin with APISpec object

Parameters

spec (APISpec) – APISpec object this plugin instance is attached to

operation_helper(path=None, operations=None, **kwargs)[source]

May mutate operations.

Parameters
parameter_helper(parameter, **kwargs)[source]

May return parameter component description as a dict.

Parameters
  • parameter (dict) – Parameter fields

  • kwargs (dict) – All additional keywords arguments sent to APISpec.parameter()

path_helper(path=None, operations=None, parameters=None, **kwargs)[source]

May return a path as string and mutate operations dict and parameters list.

Parameters

Return value should be a string or None. If a string is returned, it is set as the path.

The last path helper returning a string sets the path value. Therefore, the order of plugin registration matters. However, generally, registering several plugins that return a path does not make sense.

response_helper(response, **kwargs)[source]

May return response component description as a dict.

Parameters
  • response (dict) – Response fields

  • kwargs (dict) – All additional keywords arguments sent to APISpec.response()

schema_helper(name, definition, **kwargs)[source]

May return definition as a dict.

Parameters
  • name (str) – Identifier by which schema may be referenced

  • definition (dict) – Schema definition

  • kwargs (dict) – All additional keywords arguments sent to APISpec.schema()

apispec.core

Core apispec classes and functions.

class apispec.core.Components(plugins, openapi_version)[source]

Stores OpenAPI components

Components are top-level fields in OAS v2. They became sub-fields of “components” top-level field in OAS v3.

parameter(component_id, location, component=None, **kwargs)[source]

Add a parameter which can be referenced.

Parameters
  • param_id (str) – identifier by which parameter may be referenced.

  • location (str) – location of the parameter.

  • component (dict) – parameter fields.

  • kwargs (dict) – plugin-specific arguments

response(component_id, component=None, **kwargs)[source]

Add a response which can be referenced.

Parameters
  • component_id (str) – ref_id to use as reference

  • component (dict) – response fields

  • kwargs (dict) – plugin-specific arguments

schema(name, component=None, **kwargs)[source]

Add a new schema to the spec.

Parameters
  • name (str) – identifier by which schema may be referenced.

  • component (dict) – schema definition.

Note

If you are using apispec.ext.marshmallow, you can pass fields’ metadata as additional keyword arguments.

For example, to add enum and description to your field:

status = fields.String(
    required=True,
    enum=['open', 'closed'],
    description='Status (open or closed)',
)

https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#schemaObject

security_scheme(component_id, component)[source]

Add a security scheme which can be referenced.

Parameters
  • component_id (str) – component_id to use as reference

  • kwargs (dict) – security scheme fields

apispec.exceptions

Exception classes.

exception apispec.exceptions.APISpecError[source]

Base class for all apispec-related errors.

exception apispec.exceptions.DuplicateComponentNameError[source]

Raised when registering two components with the same name

exception apispec.exceptions.DuplicateParameterError[source]

Raised when registering a parameter already existing in a given scope

exception apispec.exceptions.InvalidParameterError[source]

Raised when parameter doesn’t contains required keys

exception apispec.exceptions.OpenAPIError[source]

Raised when a OpenAPI spec validation fails.

exception apispec.exceptions.PluginMethodNotImplementedError[source]

Raised when calling an unimplemented helper method in a plugin

apispec.utils

Various utilities for parsing OpenAPI operations from docstrings and validating against the OpenAPI spec.

class apispec.utils.OpenAPIVersion(openapi_version)[source]

OpenAPI version

Parameters

openapi_version (str|OpenAPIVersion) – OpenAPI version

Parses an OpenAPI version expressed as string. Provides shortcut to digits (major, minor, patch).

Example:

ver = OpenAPIVersion('3.0.2')
assert ver.major == 3
assert ver.minor == 0
assert ver.patch == 1
assert ver.vstring == '3.0.2'
assert str(ver) == '3.0.2'
apispec.utils.build_reference(component_type, openapi_major_version, component_name)[source]

Return path to reference

Parameters
  • component_type (str) – Component type (schema, parameter, response, security_scheme)

  • openapi_major_version (int) – OpenAPI major version (2 or 3)

  • component_name (str) – Name of component to reference

apispec.utils.dedent(content)[source]

Remove leading indent from a block of text. Used when generating descriptions from docstrings. Note that python’s textwrap.dedent doesn’t quite cut it, as it fails to dedent multiline docstrings that include unindented text on the initial line.

apispec.utils.deepupdate(original, update)[source]

Recursively update a dict.

Subdict’s won’t be overwritten but also updated.

apispec.utils.trim_docstring(docstring)[source]

Uniformly trims leading/trailing whitespace from docstrings.

Based on http://www.python.org/peps/pep-0257.html#handling-docstring-indentation

apispec.utils.validate_spec(spec)[source]

Validate the output of an APISpec object against the OpenAPI specification.

Note: Requires installing apispec with the [validation] extras.

pip install 'apispec[validation]'
Raise

apispec.exceptions.OpenAPIError if validation fails.