diff --git a/scripts/generate_openapi_schema.py b/scripts/generate_openapi_schema.py index c32b0d5d..4c851c76 100644 --- a/scripts/generate_openapi_schema.py +++ b/scripts/generate_openapi_schema.py @@ -3,6 +3,7 @@ import json import os.path import pprint +import subprocess import sys from fastapi.openapi.utils import get_openapi @@ -31,6 +32,32 @@ from ols.app.main import app # noqa: E402 pylint: disable=C0413 + +def read_version_from_openapi(filename: str) -> str: + """Read version from OpenAPI.json file.""" + # retrieve pre-generated OpenAPI schema + with open(filename, encoding="utf-8") as fin: + pre_generated_schema = json.load(fin) + assert pre_generated_schema is not None + assert "info" in pre_generated_schema, "node 'info' not found in openapi.json" + info = pre_generated_schema["info"] + assert "version" in info, "node 'version' not found in 'info'" + return info["version"] + + +def read_version_from_pyproject(): + """Read version from pyproject.toml file.""" + # it is not safe to just try to read version from pyproject.toml file directly + # the PDM tool itself is able to retrieve the version, even if the version + # is generated dynamically + completed = subprocess.run( # noqa: S603 + ["pdm", "show", "--version"], # noqa: S607 + capture_output=True, + check=True, + ) + return completed.stdout.decode("utf-8").strip() + + if __name__ == "__main__": if len(sys.argv) != 2: print("Usage: python generate_openapi_schema.py ") @@ -59,3 +86,8 @@ # dump the schema into file with open(filename, "w", encoding="utf-8") as fout: json.dump(open_api, fout, indent=4) + + openapi_version = read_version_from_openapi(filename) + project_version = read_version_from_pyproject() + assert openapi_version == project_version + print(f"OpenAPI schema generated into file {filename}")