-
Notifications
You must be signed in to change notification settings - Fork 23
/
generate_openapi_schema.py
54 lines (42 loc) · 1.43 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
42
43
44
45
46
47
48
49
50
51
52
53
54
"""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))
)
# pylint: disable-next=C0413
from ols import config
# pylint: disable-next=C0413
from ols.constants import (
CONFIGURATION_FILE_NAME_ENV_VARIABLE,
DEFAULT_CONFIGURATION_FILE,
)
# it is needed to read proper configuration in order to start the app to generate schema
cfg_file = os.environ.get(
CONFIGURATION_FILE_NAME_ENV_VARIABLE, "scripts/" + DEFAULT_CONFIGURATION_FILE
)
config.reload_from_yaml_file(cfg_file)
from ols.app.main import app # noqa: E402 pylint: disable=C0413
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python generate_openapi_schema.py <filename>")
sys.exit(1)
filename = sys.argv[1]
print("Service metadata:")
print(app.title)
print(app.description)
# 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", encoding="utf-8") as fout:
json.dump(open_api, fout, indent=4)