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:
@@ -197,3 +197,72 @@ def test_missing_operation_id_raises(tmp_path):
|
||||
)
|
||||
with pytest.raises(ValueError):
|
||||
generate_routes(spec, tmp_path / "routes")
|
||||
|
||||
|
||||
def test_template_style_async_body_model(tmp_path):
|
||||
spec = _write_spec(
|
||||
tmp_path,
|
||||
_spec(
|
||||
{
|
||||
"/items": {
|
||||
"post": {
|
||||
"operationId": "create_item",
|
||||
"requestBody": {
|
||||
"required": True,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/ItemInput"}
|
||||
}
|
||||
},
|
||||
},
|
||||
"responses": {"201": {"description": "Created"}},
|
||||
}
|
||||
}
|
||||
},
|
||||
{"ItemInput": {"type": "object", "properties": {"name": {"type": "string"}}}},
|
||||
),
|
||||
)
|
||||
files = generate_routes(
|
||||
spec,
|
||||
tmp_path / "routes",
|
||||
use_models=True,
|
||||
models_module="_gen.models",
|
||||
inject_request=True,
|
||||
template_style=True,
|
||||
)
|
||||
source = files[0].read_text(encoding="utf-8")
|
||||
assert "async def create_item(request: Request, body: ItemInput, response: Response):" in source
|
||||
assert "payload" not in source
|
||||
assert "db_resources" in source
|
||||
compile(source, str(files[0]), "exec")
|
||||
|
||||
|
||||
def test_template_style_dict_body_without_models(tmp_path):
|
||||
spec = _write_spec(
|
||||
tmp_path,
|
||||
_spec(
|
||||
{
|
||||
"/items": {
|
||||
"post": {
|
||||
"operationId": "create_item",
|
||||
"requestBody": {
|
||||
"required": True,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/ItemInput"}
|
||||
}
|
||||
},
|
||||
},
|
||||
"responses": {"201": {"description": "Created"}},
|
||||
}
|
||||
}
|
||||
}
|
||||
),
|
||||
)
|
||||
files = generate_routes(
|
||||
spec, tmp_path / "routes", inject_request=True, template_style=True
|
||||
)
|
||||
source = files[0].read_text(encoding="utf-8")
|
||||
assert "async def create_item(request: Request, body: dict, response: Response):" in source
|
||||
assert "payload" not in source
|
||||
compile(source, str(files[0]), "exec")
|
||||
|
||||
Reference in New Issue
Block a user