Skip to content

Commit d34ef16

Browse files
committed
Update datamodel-code-generator, add support for modular references, add additional tests
1 parent 5ef88e7 commit d34ef16

File tree

16 files changed

+491
-101
lines changed

16 files changed

+491
-101
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,6 @@ fabric.properties
176176

177177
.idea
178178

179+
.vscode/
180+
179181
version.py

fastapi_code_generator/__main__.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
BUILTIN_VISITOR_DIR = Path(__file__).parent / "visitors"
3030

31-
MODEL_PATH: Path = Path("models.py")
31+
MODEL_PATH: Path = Path("models")
3232

3333

3434
def dynamic_load_module(module_path: Path) -> Any:
@@ -63,7 +63,7 @@ def main(
6363
DataModelType.PydanticBaseModel.value, "--output-model-type", "-d"
6464
),
6565
python_version: PythonVersion = typer.Option(
66-
PythonVersion.PY_38.value, "--python-version", "-p"
66+
PythonVersion.PY_39.value, "--python-version", "-p"
6767
),
6868
) -> None:
6969
input_name: str = input_file
@@ -73,7 +73,7 @@ def main(
7373
input_text = f.read()
7474

7575
if model_file:
76-
model_path = Path(model_file).with_suffix('.py')
76+
model_path = Path(model_file)
7777
else:
7878
model_path = MODEL_PATH
7979

@@ -119,7 +119,7 @@ def generate_code(
119119
generate_routers: Optional[bool] = None,
120120
specify_tags: Optional[str] = None,
121121
output_model_type: DataModelType = DataModelType.PydanticBaseModel,
122-
python_version: PythonVersion = PythonVersion.PY_38,
122+
python_version: PythonVersion = PythonVersion.PY_39,
123123
) -> None:
124124
if not model_path:
125125
model_path = MODEL_PATH
@@ -149,14 +149,16 @@ def generate_code(
149149

150150
with chdir(output_dir):
151151
models = parser.parse()
152-
output = output_dir / model_path
153152
if not models:
154153
# if no models (schemas), just generate an empty model file.
155-
modules = {output: ("", input_name)}
154+
modules = {output_dir / model_path.with_suffix('.py'): ("", input_name)}
156155
elif isinstance(models, str):
157-
modules = {output: (models, input_name)}
156+
modules = {output_dir / model_path.with_suffix('.py'): (models, input_name)}
158157
else:
159-
raise Exception('Modular references are not supported in this version')
158+
modules = {
159+
output_dir / model_path / module_name[0]: (model.body, input_name)
160+
for module_name, model in models.items()
161+
}
160162

161163
environment: Environment = Environment(
162164
loader=FileSystemLoader(

fastapi_code_generator/parser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pathlib
44
import re
5+
from functools import cached_property
56
from typing import (
67
Any,
78
Callable,
@@ -42,7 +43,6 @@
4243
ResponseObject,
4344
)
4445
from datamodel_code_generator.types import DataType, DataTypeManager, StrictTypes
45-
from datamodel_code_generator.util import cached_property
4646
from pydantic import BaseModel, ValidationInfo
4747

4848
RE_APPLICATION_JSON_PATTERN: Pattern[str] = re.compile(r'^application/.*json$')
@@ -153,7 +153,7 @@ def function_name(self) -> str:
153153
return stringcase.snakecase(name)
154154

155155

156-
@snooper_to_methods(max_variable_length=None)
156+
@snooper_to_methods()
157157
class OpenAPIParser(OpenAPIModelParser):
158158
def __init__(
159159
self,
@@ -166,7 +166,7 @@ def __init__(
166166
base_class: Optional[str] = None,
167167
custom_template_dir: Optional[pathlib.Path] = None,
168168
extra_template_data: Optional[DefaultDict[str, Dict[str, Any]]] = None,
169-
target_python_version: PythonVersion = PythonVersion.PY_37,
169+
target_python_version: PythonVersion = PythonVersion.PY_39,
170170
dump_resolve_reference_action: Optional[Callable[[Iterable[str]], str]] = None,
171171
validation: bool = False,
172172
field_constraints: bool = False,

0 commit comments

Comments
 (0)