Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verification if OpenAPI schema is up to date #352

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}")