"""Validate Pydantic models generated from the OpenAPI 3.2 compatibility fixture. make check-pydantic-compat generates the models into a temporary file and passes its path. The checks cover the api/core conventions that datamodel-code-generator turns into models: Problem, PageInfo, Timestamp, and the PageLimit and PageCursor query parameters. It does not generate header parameters or response headers, so Idempotency-Key, If-Match, and ETag have no model to check. """ import importlib.util import sys from pathlib import Path from types import ModuleType from pydantic import BaseModel, ValidationError def load(path: Path) -> ModuleType: spec = importlib.util.spec_from_file_location("fixture_models", path) if spec is None or spec.loader is None: raise SystemExit(f"cannot load generated models from {path}") module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module def rejects(model: type[BaseModel], payload: object) -> bool: try: model.model_validate(payload) except ValidationError: return True return False def main(path: Path) -> None: models = load(path) problem = models.Problem.model_validate( { "type": "urn:lincodex:problem:state_conflict", "title": "Resource state conflict", "status": 409, "code": "state_conflict", "errors": [{"in": "body", "pointer": "/name", "code": "invalid", "message": "m"}], } ) assert problem.code.root == "state_conflict" assert rejects(models.Problem, {"type": "t", "title": "t", "status": 409, "code": "unknown"}) assert rejects(models.Problem, {"type": "t", "title": "t", "status": 200, "code": "internal"}) page = models.PageInfo.model_validate({"has_more": False, "next_cursor": None}) assert page.next_cursor is None assert rejects(models.PageInfo, {"has_more": False}) assert rejects(models.PageInfo, {"has_more": True, "next_cursor": "a" * 513}) stamp = models.Timestamp.model_validate("2026-10-05T09:30:00Z") assert models.Timestamp(stamp.root).model_dump_json() == '"2026-10-05T09:30:00Z"' assert rejects(models.Timestamp, "2026-10-05T09:30:00") query = models.ProbeItemsGetParametersQuery.model_validate({}) assert query.limit == 20 assert rejects(models.ProbeItemsGetParametersQuery, {"limit": 0}) assert rejects(models.ProbeItemsGetParametersQuery, {"limit": 101}) assert rejects(models.ProbeItemsGetParametersQuery, {"cursor": "a b"}) item_page = models.ProbeItemPage.model_validate( { "items": [ { "id": "5f0c6a8e-3b0e-4c47-9f4c-2a8f9e5d7c11", "name": "probe", "updated_at": "2026-10-05T09:30:00Z", } ], "page": {"has_more": False, "next_cursor": None}, } ) assert len(item_page.items) == 1 print("Pydantic compatibility fixture: generated models validated.") if __name__ == "__main__": if len(sys.argv) != 2: raise SystemExit("usage: check_fixture_models.py ") main(Path(sys.argv[1]))