Skip to content

Commit 6bd7524

Browse files
(fix) dynamically getting the versioning for openapi spec
1 parent 8507daf commit 6bd7524

3 files changed

Lines changed: 18 additions & 2 deletions

File tree

middleware/openapi.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
from __future__ import annotations
1212

1313
import re
14+
import tomllib
1415
from http import HTTPStatus
16+
from pathlib import Path
1517
from typing import Any
1618

1719
from fastapi import FastAPI
@@ -31,6 +33,17 @@
3133
_HTTP_METHODS = {"get", "post", "put", "patch", "delete", "options", "head", "trace"}
3234
_STEP_PATTERN = r"^[1-9][0-9]*[smhd]$"
3335
_STANDARD_ERROR_CODES = {"400", "401", "403", "404", "429", "500", "502", "503"}
36+
_DEFAULT_APP_VERSION = "0.0.1"
37+
38+
39+
def _project_version() -> str:
40+
pyproject_path = Path(__file__).resolve().parents[1] / "pyproject.toml"
41+
try:
42+
project = tomllib.loads(pyproject_path.read_text(encoding="utf-8")).get("project", {})
43+
except (OSError, tomllib.TOMLDecodeError):
44+
return _DEFAULT_APP_VERSION
45+
version = project.get("version")
46+
return version if isinstance(version, str) and version.strip() else _DEFAULT_APP_VERSION
3447

3548

3649
def _status_description(status_code: int) -> str:
@@ -264,6 +277,9 @@ def custom_openapi() -> Any:
264277
if not isinstance(schema_value, dict):
265278
return schema_value
266279
schema = schema_value
280+
info = schema.get("info")
281+
if isinstance(info, dict):
282+
info["version"] = _project_version()
267283

268284
_ensure_security_schemes(schema)
269285
_ensure_error_schema(schema)

openapi.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

openapi.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ info:
33
title: Resolver Analysis Engine
44
description: AI-powered root cause analysis and anomaly detection over logs, metrics,
55
and traces.
6-
version: 1.0.0
6+
version: 0.0.3
77
paths:
88
/api/v1/health:
99
get:

0 commit comments

Comments
 (0)