Skip to main content

Sphinx extension for OpenAPI documentation

Project description

Sphinx Extension: OpenAPI

PyPI PyPI - License

Description

This Sphinx extension allows for downloading updated OpenAPI json + yaml specs for use with the sphinxcontrib.redoc extension.

Setup

Add the following to your conf.py (includes redoc extension setup):

import sys, os
from pathlib import Path

html_context = {}  # This is usually already defined for other themes/extensions
sys.path.append(os.path.abspath(os.path.join('_extensions', 'sphinx_openapi')))
extensions = ['sphinx_openapi', 'sphinxcontrib.redoc']

# -- Extension: sphinx_openapi (OpenAPI Local Download/Updater) -----------
# Used in combination with the sphinxcontrib.redoc extension
# Use OpenAPI ext to download/update -> redoc ext to generate

# Define the target json|yaml + path to save the downloaded OpenAPI spec
openapi_spec_url_noext = 'https://api.demo.goxbe.cloud/v1/openapi'
openapi_dir_path = '_specs'  # Downloads json|yaml files to here
openapi_file_type = 'json'  # 'json' or 'yaml' (we'll download them both, but generate from only 1)

# Link here from rst with explicit ".html" ext (!) but NOT from a doctree
openapi_generated_file_posix_path = Path(os.path.join(
   'content', '-', 'api', 'index')).as_posix()  # Parses to forward/slashes/

# Set the config values for the extension
html_context.update({
   'openapi_spec_url_noext': openapi_spec_url_noext,
   'openapi_dir_path': openapi_dir_path,
   'openapi_generated_file_posix_path': openapi_generated_file_posix_path,
   'openapi_file_type': openapi_file_type,
})

# -- Extension: sphinxcontrib.redoc --------------------------------------
# OpenAPI Docgen: Similar to sphinxcontrib-openapi, but +1 column for example responses
# (!) Prereq: OpenAPI Local Download (above)
# Doc | https://sphinxcontrib-redoc.readthedocs.io/en/stable
# Demo | https://sphinxcontrib-redoc.readthedocs.io/en/stable/api/github/

# Intentional forward/slashes/ for html; eg: "_static/specs/openapi.json"
xbe_spec = openapi_dir_path + '/openapi.json'
github_demo_spec = openapi_dir_path + '/github-demo.yml'

redoc = [
   {
       'name': 'Xsolla Backend API',
       'page': openapi_generated_file_posix_path,  # content/-/api/index
       # 'spec': '_static/specs/openapi.json',  # (!) Ours Currently won't build due to errs: `/components/schemas/ACLRecordMongo". Token "ACLRecordMongo" does not exist`
       'spec': github_demo_spec,  # DELETE ME AFTER DONE WITH TESTS!
       'embed': True,  # Local file only (!) but embed is less powerful
       'opts': {
           'lazy-rendering': True,  # Formerly called `lazy`; almost required for giant docs
           'required-props-first': True,  # Useful, (!) but slower
           'native-scrollbars': False,  # Improves perf on big specs when False
           'expand-responses': ["200", "201"],
           'suppress-warnings': False,
           'hide-hostname': False,
           'untrusted-spec': False,
       }
   },
]

print(f'[conf.py::sphinxcontrib.redoc] redoc[0].page: {redoc[0]["page"]}')
print(f'[conf.py::sphinxcontrib.redoc] redoc[0].spec: {redoc[0]["spec"]}')
print('')

Requirements

  • Python>=3.6
  • Sphinx>=1.8

This may work with older versions, but has not been tested.

Entry Point

See setup(app) definition at sphinx_openapi.py.

Tested in

  • Windows 11 via PowerShell 7
  • Ubuntu 22.04 via ReadTheDocs (RTD) CI

Notes

  • __init__.py is required for both external pathing and to treat the directory as a pkg

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sphinx_openapi-1.0.6.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sphinx_openapi-1.0.6-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file sphinx_openapi-1.0.6.tar.gz.

File metadata

  • Download URL: sphinx_openapi-1.0.6.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.16

File hashes

Hashes for sphinx_openapi-1.0.6.tar.gz
Algorithm Hash digest
SHA256 b9cf4918f2867227a217891b321c70416dd48fdf9fc722c008c7c7ef87bffc8a
MD5 74f4e66bc61ca401b859c35af521d581
BLAKE2b-256 36e2edf1fb96a83656352eb3ad17dd2e23faa82349df9e1e61d1c895fe93505d

See more details on using hashes here.

File details

Details for the file sphinx_openapi-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: sphinx_openapi-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.16

File hashes

Hashes for sphinx_openapi-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 c013e6f3c324931db7174234f40aa62e3706bffe939734dd86f4c026d5b21866
MD5 ef94dd9d922f89dbee0fcc574b6a9b32
BLAKE2b-256 caa9be9977027182198ff4a6858395dd8fc36ab06c7fa3a05c75beccbf5f0f96

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page