Commit b9eedbc
committed
Derive JSON Schema from Python type hints in OpenAPI output
qh builds every route with a generic endpoint(request: Request) signature,
so the wrapped function's parameters and return type are invisible to
FastAPI's OpenAPI machinery: /openapi.json had empty {} request/response
schemas and no components.schemas, and openapi-typescript produced no
useful types.
This derives a complete OpenAPI document from the wrapped functions' Python
type hints — additive, with the request-handling path untouched:
- qh/openapi.py: python_type_to_json_schema() converts Python type hints to
JSON Schema (primitives, list/dict/tuple, Optional/Union incl. PEP 604,
Literal, Any, dataclasses, TypedDicts incl. total=False, Pydantic models,
NamedTuples, Enums). Named composites are registered in components.schemas
and referenced by $ref; the recursion guard handles self-referential types.
- enhance_openapi_schema() now fills requestBody, parameters (path/query),
responses.200 and components.schemas.
- install_enhanced_openapi() overrides app.openapi to serve the enriched doc
at /openapi.json, with a defensive fallback to FastAPI's plain schema.
- endpoint.py exposes the resolved param -> HTTP-location map; app.py
surfaces it via inspect_routes — single source of truth for body/path/query
classification.
- mk_app(enhanced_openapi=True) installs it by default.
Adds qh/tests/test_openapi_schema.py (27 tests).
Closes #81 parent 8ffb74d commit b9eedbc
5 files changed
Lines changed: 901 additions & 46 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
27 | 32 | | |
28 | 33 | | |
29 | 34 | | |
| |||
117 | 122 | | |
118 | 123 | | |
119 | 124 | | |
| 125 | + | |
| 126 | + | |
120 | 127 | | |
121 | 128 | | |
122 | 129 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
| |||
69 | 70 | | |
70 | 71 | | |
71 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
72 | 79 | | |
73 | 80 | | |
74 | 81 | | |
| |||
254 | 261 | | |
255 | 262 | | |
256 | 263 | | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
257 | 271 | | |
258 | 272 | | |
259 | 273 | | |
| |||
280 | 294 | | |
281 | 295 | | |
282 | 296 | | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
283 | 301 | | |
284 | 302 | | |
285 | 303 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
312 | 312 | | |
313 | 313 | | |
314 | 314 | | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
315 | 321 | | |
316 | 322 | | |
317 | 323 | | |
| |||
0 commit comments