feat: emit template-style route stubs (async, body param, service hint)

Add generate_routes(template_style=...) and CLI --template-style. When on,
stubs use async def, a JSON body parameter named body (dict or Model), and
with --inject-request a service-lookup hint comment before NotImplementedError.
Default behaviour (def/payload) unchanged. +2 tests (32 passed), wiki + MCP
bundle regenerated.
This commit is contained in:
2026-10-13 19:35:04 +05:30
parent 51ffb4aae9
commit 9cb8bf8f1c
8 changed files with 125 additions and 16 deletions

View File

@@ -55,6 +55,7 @@ One file per resource (derived from the first path segment). Flags:
| `--models-module MOD` | Dotted module for those imports (default `models`) |
| `--inject-request` | Prepend `request: Request` to every handler (DI convention) |
| `--package` | Emit `<resource>/routes.py` packages (with `__init__.py`) instead of flat `<resource>.py` |
| `--template-style` | Emit `async def` handlers with a JSON body parameter named `body` (the standards-template convention) plus a service-resolution hint, instead of sync `def` handlers taking `payload` |
Why bother? Because `bind_routes` (in [02 – Components](../02_components.md)) needs an `operationId` → handler **exactly** matching the spec. Codegen guarantees you never type a handler name wrong — it wears the same `operationId` the spec declares.
@@ -65,6 +66,15 @@ Why bother? Because `bind_routes` (in [02 – Components](../02_components.md))
The stubs `raise NotImplementedError`; fill each body in (typically by resolving a service from `request.app.state`).
### Handler shapes
Both shapes bind equally — the router resolves the handler by `operationId` and introspects the FastAPI signature, so it never inspects the request body:
- **Default (sync stub):** `def create_item(payload: ItemInput, response: Response):` — the body parameter is `payload`.
- **`--template-style`:** `async def create_item(request: Request, body: ItemInput, response: Response):` — matches the standards template (`async def`, `body` parameter, a `request.app.state.db_resources["<resource>_svc"]` hint).
Use `--template-style` when generating against the standards template so the scaffold matches the hand-written handlers you will keep.
> **Note:** `models` / `routes` are build-time tooling. They do **not** run at runtime, and templates (the Bake-your-stuff section of [02 – Templates](02_templates.md)) scaffold a working server out-of-the-box for greenfield projects.
---