Codegen Routes
openapi_first.codegen_routes
Summary
Route handler code generation from OpenAPI specifications.
This module generates Python route handler stubs from an OpenAPI 3.x
specification. Each resource (derived from the first path segment)
gets its own file under the output directory. Every OpenAPI operation
must define an operationId, which becomes the handler function name.
Notes
Design constraints:
1 2 3 4 5 6 7 | |
Functions
generate_routes
Generate route handler stubs from an OpenAPI specification.
Creates one <resource>.py file per resource in output_dir.
Resources are derived from the first path segment (e.g. /pets
and /pets/{id} both group under pets).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec_path |
Path
|
Path to the OpenAPI specification file (YAML or JSON). |
required |
output_dir |
Path
|
Directory where the generated route files are written. Created automatically if it does not exist. |
required |
use_models |
bool
|
If |
False
|
models_module |
str
|
Dotted Python module path from which to import models
(e.g. |
'models'
|
Returns:
| Type | Description |
|---|---|
list[Path]
|
list[Path]: Absolute paths of every generated route file. |
Raises:
| Type | Description |
|---|---|
OpenAPISpecLoadError
|
If the spec cannot be loaded or validated. |
ValueError
|
If any operation is missing |