-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159 from lsst-sqre/tickets/DM-37525
DM-37525: Add openapi generator extension
- Loading branch information
Showing
13 changed files
with
640 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
############################################################## | ||
Embedding a Redoc subsite for OpenAPI (HTTP API) documentation | ||
############################################################## | ||
|
||
FastAPI_, the Python framework for building web applications, automatically generates OpenAPI specifications. | ||
Documenteer provides built-in support for embedding a Redoc_ subsite for this OpenAPI documentation in a Rubin user guide. | ||
This solution combines the Documenteer Sphinx extension :doc:`documenteer.ext.openapi </sphinx-extensions/openapi>` to install the OpenAPI specification into the docs from your FastAPI application and the third-party extension sphinxcontrib-redoc_ to embed the Redoc subsite into your Sphinx documentation site. | ||
|
||
.. _guides-openapi-generator-function: | ||
|
||
1. Create a function to generate the OpenAPI docs | ||
================================================= | ||
|
||
The first step is to create or identify a function that generates an OpenAPI specification for your application as a JSON-serialized string. | ||
This function must be importable from the Sphinx build process. | ||
For SQuaRE/Safir-standardized FastAPI applications, it may make sense to add this function to the :file:`main.py` module where the FastAPI application is defined: | ||
|
||
.. code-block:: python | ||
:caption: src/main.py | ||
import json | ||
from fastapi import FastAPI | ||
app = FastAPI() | ||
def create_openapi() -> str: | ||
"""Create the OpenAPI spec for static documentation.""" | ||
spec = get_openapi( | ||
title=app.title, | ||
version=app.version, | ||
description=app.description, | ||
routes=app.routes, | ||
) | ||
return json.dumps(spec) | ||
.. note:: | ||
|
||
This step is not necessary if the OpenAPI specification is already available in the source repository or is generated by a separate build process. | ||
See the note in :ref:`step 3 <guides-openapi-config>` on configuring an existing OpenAPI specification. | ||
|
||
.. _guides-openapi-stub: | ||
|
||
2. Add a stub page for the Redoc subsite | ||
======================================== | ||
|
||
In your Sphinx documentation project, add a stub page for the Redoc subsite. | ||
This step adds the Redoc page to the Sphinx toctree and therefore to the Sphinx site navigation. | ||
|
||
First, create the stub file: | ||
|
||
.. code-block:: rst | ||
:caption: docs/api.rst | ||
######## | ||
REST API | ||
######## | ||
This is a stub page for the API. | ||
Then add this page to the site's toctree: | ||
|
||
.. code-block:: rst | ||
:caption: docs/index.rst | ||
.. toctree:: | ||
api | ||
.. _guides-openapi-config: | ||
|
||
3. Configuration in documenteer.toml | ||
==================================== | ||
|
||
In the Sphinx project's :file:`documenteer.toml` configuration file, add a :ref:`[project.openapi] <guide-project-openapi>` table: | ||
|
||
.. code-block:: toml | ||
:caption: docs/documenteer.toml | ||
[project.openapi] | ||
openapi_path = "_static/openapi.json" | ||
doc_path = "api" | ||
[project.openapi.generator] | ||
function = "example.main:create_openapi" | ||
The :ref:`openapi_path <guide-project-openapi-openapi-path>` key specifies the path to the OpenAPI specification file, relative to the Sphinx project root. | ||
The :ref:`doc_path <guide-project-openapi-doc-path>` key specifies the path to the Redoc subsite, relative to the Sphinx project root, and should match the path of the stub page created in :ref:`step 2 <guides-openapi-stub>`. | ||
|
||
The :ref:`function <guide-project-openapi-generator-function>` key specifies the import path to the function in your app that generates the OpenAPI specification. | ||
This field is formatted as ``<module>:<function>``. | ||
For example, if the function called ``create_openapi`` is in the :file:`main.py` module of the :file:`example` package, the value would be ``"example.main:create_openapi"``. | ||
|
||
If the function takes positional or keyword arguments, you can specify them in the :ref:`positional_args <guide-project-openapi-generator-positional-args>` and :ref:`keyword_args <guide-project-openapi-generator-keyword-args>` keys, respectively. | ||
|
||
.. code-block:: toml | ||
:caption: docs/documenteer.toml | ||
[project.openapi.generator] | ||
function = "example.main:create_openapi" | ||
positional_args = ["arg1", "arg2"] | ||
keyword_args = {kwarg1 = "value1", kwarg2 = "value2"} | ||
.. note:: | ||
|
||
If the OpenAPI specification is already available, you don't need to specify the :ref:`[project.openapi.generator] <guide-project-openapi-generator>` table. | ||
Ensure that the ``openapi_path`` key is set to the OpenAPI specification file's path, relative to the Sphinx project root. | ||
If necessary, you can create a symlink to ensure the OpenAPI specification is available within the documentation source directory. | ||
|
||
Additional resources | ||
==================== | ||
|
||
- Find reference documentation for the :ref:`[project.openapi] table <guide-project-openapi>` in the :doc:`documenteer.toml configuration file <toml-reference>`. | ||
- Learn more about the :doc:`documenteer.ext.openapi </sphinx-extensions/openapi>` extension. | ||
- Learn more about the `sphinxcontrib-redoc`_ extension. | ||
- Learn more about the FastAPI_ framework. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ Sphinx extensions | |
package-toctree | ||
lsst-pybtex-style | ||
autodocreset | ||
openapi |
Oops, something went wrong.