code gen
This commit is contained in:
53
openapi_first/codegen.py
Normal file
53
openapi_first/codegen.py
Normal file
@@ -0,0 +1,53 @@
|
||||
"""
|
||||
# Summary
|
||||
|
||||
Core logic for generating Python source code from OpenAPI specifications.
|
||||
|
||||
This module provides reusable utilities for code generation, specifically
|
||||
generating Pydantic models and route handler stubs from OpenAPI 3.x schema
|
||||
definitions.
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
from datamodel_code_generator import (
|
||||
InputFileType,
|
||||
PythonVersion,
|
||||
generate,
|
||||
)
|
||||
|
||||
from .codegen_routes import generate_routes
|
||||
|
||||
|
||||
def generate_models(
|
||||
spec_path: Path,
|
||||
output_path: Path,
|
||||
pydantic_version: int = 2,
|
||||
) -> None:
|
||||
"""
|
||||
Generate Pydantic models from an OpenAPI specification.
|
||||
|
||||
Args:
|
||||
spec_path (Path):
|
||||
Path to the OpenAPI specification file (YAML or JSON).
|
||||
output_path (Path):
|
||||
Path where the generated Python code should be written.
|
||||
pydantic_version (int, optional):
|
||||
The Pydantic version to target (1 or 2). Defaults to 2.
|
||||
|
||||
Notes:
|
||||
**Reusability:**
|
||||
This function is designed to be used by the CLI and can be
|
||||
exposed as an MCP tool without modification.
|
||||
"""
|
||||
generate(
|
||||
input_=spec_path,
|
||||
input_file_type=InputFileType.OpenAPI,
|
||||
output=output_path,
|
||||
target_python_version=PythonVersion.PY_310,
|
||||
use_schema_description=True,
|
||||
use_standard_collections=True,
|
||||
use_union_operator=True,
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["generate_models", "generate_routes"]
|
||||
Reference in New Issue
Block a user