Skip to content

Commit

Permalink
Verification if OpenAPI schema is up to date
Browse files Browse the repository at this point in the history
  • Loading branch information
tisnik committed Feb 4, 2025
1 parent cabcf82 commit 50fbbab
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions scripts/generate_openapi_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import os.path
import pprint
import subprocess
import sys

from fastapi.openapi.utils import get_openapi
Expand Down Expand Up @@ -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 <filename>")
Expand Down Expand Up @@ -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}")

0 comments on commit 50fbbab

Please sign in to comment.