feat(api): define public problem errors, pagination, and retry contract

This commit is contained in:
shira committed 2026-10-05 22:09:36 +03:00
1 parent b2f6e87bfe
commit 565ca94bbb
86 files changed
+8200 -154

No files matched your search

+34 -1
View File
@@ -6,7 +6,7 @@ import pytest
import yaml
from pydantic import BaseModel, ValidationError
from lincodex_ml.api.models import MLHealthResponse, MLReadinessResponse
from lincodex_ml.api.models import MLHealthResponse, MLReadinessResponse, Problem
from lincodex_ml.main import app
CONTRACT_PATH = Path(__file__).resolve().parents[2] / "api" / "ml" / "openapi.bundle.yml"
@@ -20,6 +20,14 @@ def _resolve_schema(document: dict[str, Any], schema: dict[str, Any]) -> dict[st
return document["components"]["schemas"][component_name]
def _resolve_response(document: dict[str, Any], response: dict[str, Any]) -> dict[str, Any]:
reference = response.get("$ref")
if reference is None:
return response
component_name = reference.rsplit("/", maxsplit=1)[-1]
return document["components"]["responses"][component_name]
def _status_values(document: dict[str, Any], response: dict[str, Any]) -> list[str]:
content = response["content"]["application/json"]["schema"]
response_schema = _resolve_schema(document, content)
@@ -47,6 +55,10 @@ def test_health_and_readiness_match_shared_openapi_contract() -> None:
assert actual_operation["operationId"] == expected_operation["operationId"]
for response_code, expected_response in expected_operation["responses"].items():
# The default problem response is checked at runtime below; FastAPI
# cannot describe it with the problem media type.
if response_code == "default":
continue
actual_response = actual_operation["responses"][response_code]
assert _status_values(contract, expected_response) == _status_values(
implementation, actual_response
@@ -68,6 +80,27 @@ async def test_unready_probe_returns_the_contractual_503_response() -> None:
assert response.json()["status"] in _status_values(contract, readiness["503"])
@pytest.mark.anyio
@pytest.mark.parametrize(("method", "path"), [("POST", "/healthz"), ("GET", "/missing")])
async def test_error_responses_match_the_contractual_default_response(
method: str, path: str
) -> None:
contract = yaml.safe_load(CONTRACT_PATH.read_text(encoding="utf-8"))
default = _resolve_response(
contract, contract["paths"]["/healthz"]["get"]["responses"]["default"]
)
(media_type,) = default["content"]
async with httpx.AsyncClient(
transport=httpx.ASGITransport(app=app), base_url="http://test"
) as client:
response = await client.request(method, path)
assert response.headers["content-type"] == media_type
problem = Problem.model_validate(response.json())
assert problem.status == response.status_code
@pytest.mark.parametrize(
("model", "valid_status"),
[(MLHealthResponse, "ok"), (MLReadinessResponse, "ready"), (MLReadinessResponse, "not_ready")],