-
Notifications
You must be signed in to change notification settings - Fork 17
/
generate_openapi_schema.py
41 lines (31 loc) · 1.1 KB
/
generate_openapi_schema.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""Utility script to generate OpenAPI schema."""
import json
import os.path
import sys
from fastapi.openapi.utils import get_openapi
# we need to import OLS app from directory above, so it is needed to update
# search path accordingly
sys.path.append(
os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
)
from ols import config
# it is needed to read proper configuration in order to start the app to generate schema
cfg_file = os.environ.get("RCS_CONFIG_FILE", "rcsconfig.yaml")
config.reload_from_yaml_file(cfg_file)
from ols.app.main import app # noqa: E402
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python generate_openapi_schema.py <filename>")
sys.exit(1)
filename = sys.argv[1]
# retrieve OpenAPI schema via initialized app
open_api = get_openapi(
title=app.title,
version=app.version,
openapi_version=app.openapi_version,
description=app.description,
routes=app.routes,
)
# dump the schema into file
with open(filename, "w") as fout:
json.dump(open_api, fout, indent=4)