Hint
Check out sphinxcontrib-redoc if you are interested in separate three-panel OpenAPI spec rendering.
sphinxcontrib-openapi is a Sphinx extension to generate APIs docs from OpenAPI (fka Swagger) spec. It depends on sphinxcontrib-httpdomain that provides an HTTP domain for describing RESTful HTTP APIs, so we don't need to reinvent the wheel.
pip install sphinxcontrib-openapiConsider you have the following OpenAPI spec saved at specs/openapi.yml:
.. literalinclude:: specs/openapi.yml :language: yaml
You can render it by using the openapi directive:
.. openapi:: specs/openapi.ymland it will be rendered into something like:
.. openapi:: specs/openapi.yml
You can also use URLs where to fetch the OpenAPI spec from:
.. openapi:: https://petstore.swagger.io/v2/swagger.jsonThe openapi directive supports the following options:
encoding- Encoding to be used to read an OpenAPI spec. If not passed, Sphinx's source encoding will be used.
pathsA comma separated list of paths to filter the included OpenAPI spec by. For example:
.. openapi:: specs/openapi.yml :paths: /persons /evidence :encoding: utf-8
Would only render the endpoints at
/personsand/evidence, ignoring all others.examples- If passed, both request and response examples will be rendered. Please note, if examples are not provided in a spec, they will be generated by internal logic based on a corresponding schema.
group- If passed, paths will be grouped by tags. If a path has no tag assigned, it
will be grouped in a
defaultgroup. format- The format of text in the spec, either
rstormarkdown. If not supplied, ReStructured Text is assumed. includeA line separated list of regular expressions to filter the included openapi spec by. For example:
.. openapi:: specs/openapi.yml :include: /evid.* :encoding: utf-8
Would render the endpoints at
/evidenceand/evidence/{pk}excludeA line separated list of regular expressions to filter the included openapi spec by (excluding matches). For example:
.. openapi:: specs/openapi.yml :exclude: /evidence/{pk} :encoding: utf-8
Would render
/personsand/evidenceendpoints, but not/evidence/{pk}endpointsmethodsA line separated list of http methods to filter included openapi spec. For example:
.. openapi:: specs/openapi.yml :methods: get post put :encoding: utf-8
Would render paths with get, post or put method
exclude, include and paths can also be used together (exclude
taking precedence over include and paths)
http-methods-orderA whitespace delimited list of HTTP methods to render first. For example:
.. openapi:: specs/openapi.yml :http-methods-order: head get
Would render the
headmethod, followed by thegetmethod, followed by the rest of the methods in their declared ordered.