Skip to content

Commit

Permalink
docs: bundle external OpenAPI spec visualizer (#1780)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyujin-cho authored Dec 12, 2023
1 parent e9480e4 commit cb46cd8
Show file tree
Hide file tree
Showing 8 changed files with 1,858 additions and 13 deletions.
6 changes: 6 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ Example:

Please ask the docs maintainer for help.

### Previewing built HTML documentation
Simply create a nginx server which serves `_build/html` folder. For example (docker required):
```bash
docker run --rm -it -v $(pwd)/_build/html:/usr/share/nginx/html -p 8000:80 nginx
```
Executing the command above inside `docs` folder will serve the documentation page on port 8000 (http://localhost:8000).

## References for newcomers

Expand Down
1,820 changes: 1,820 additions & 0 deletions docs/_static/js/spec-viewer.js

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions docs/_templates/layout.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{% extends "!layout.html" %}
{% block extrahead %}
<link href="{{ pathto("_static/custom.css", True) }}" rel="stylesheet" type="text/css">
{% endblock %}
{%- if meta is mapping and meta.get("render_mode") == "raw" %}
{% block body %}{% endblock %}
{%- else %}
{% extends "!layout.html" %}
{% block extrahead %}
<link href="{{ pathto("_static/custom.css", True) }}" rel="stylesheet" type="text/css">
{% endblock %}
{%- endif %}
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"sphinx_rtd_theme",
"sphinxcontrib_trio",
"sphinxcontrib.mermaid",
"sphinxcontrib.openapi",
"sphinx_autodoc_typehints",
]

Expand Down Expand Up @@ -151,6 +150,7 @@
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["_static"]
html_extra_path = ["manager/rest-reference/openapi.json"]

# Custom sidebar templates, maps document names to template names.
# html_sidebars = {}
Expand Down
2 changes: 1 addition & 1 deletion docs/manager/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ Backend.AI Manager Reference
common-api/index
user-api/intro
admin-api/intro
rest-reference/index
rest-reference/index
19 changes: 17 additions & 2 deletions docs/manager/rest-reference/index.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
:render_mode: raw

Backend.AI REST API Reference
=====
.. openapi:httpdomain:: openapi.json
=============================

.. raw:: html

<redoc spec-url="../../openapi.json"></redoc>
<script src="../../_static/js/spec-viewer.js"> </script>
<style>
body {
margin: 0;
padding: 0;
}
body > section > h1 {
display: none;
}
</style>
1 change: 0 additions & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ sphinx-autodoc-typehints~=1.24.0
sphinx-intl>=2.1
sphinx-rtd-theme~=1.3
sphinxcontrib-mermaid~=0.7.1
sphinxcontrib-openapi~=0.8.1
rtds-action~=1.1.0
pygments~=2.16.1
# replace the following line with native support of Sphinx in Pants
Expand Down
9 changes: 5 additions & 4 deletions src/ai/backend/manager/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _traverse(scheme: t.Trafaret) -> dict:
x for x in trafarets if not (isinstance(x, t.Null) or isinstance(x, UndefChecker))
]
if len(valid_trafarets) >= 2:
return {"oneOf": list(_traverse(s) for s in valid_trafarets)}
return {"anyOf": list(_traverse(s) for s in valid_trafarets)}
else:
scheme = valid_trafarets[0]
if isinstance(scheme, t.Any):
Expand Down Expand Up @@ -122,7 +122,7 @@ def _traverse(scheme: t.Trafaret) -> dict:
return {"type": "string", "pattern": str(scheme._rx_slug.pattern)}
if isinstance(scheme, tx.TimeDuration):
return {
"oneOf": [
"anyOf": [
{
"type": "string",
"description": (
Expand Down Expand Up @@ -195,7 +195,7 @@ def parse_trafaret_definition(root: t.Dict) -> list[dict]:

def generate_openapi(subapps: list[web.Application], verbose=False) -> dict[str, Any]:
openapi: dict[str, Any] = {
"openapi": "3.0.0",
"openapi": "3.1.0",
"info": {
"title": "Backend.AI Manager API",
"description": "Backend.AI Manager REST API specification",
Expand Down Expand Up @@ -357,11 +357,12 @@ def generate_openapi(subapps: list[web.Application], verbose=False) -> dict[str,
openapi["components"]["schemas"][schema_name] = response_schema
route_def["responses"] = {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {"$ref": f"#/components/schemas/{schema_name}"}
}
}
},
}
}
openapi["paths"][path][method.lower()] = route_def
Expand Down

0 comments on commit cb46cd8

Please sign in to comment.