-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmodel_generator.py
More file actions
185 lines (162 loc) · 6.12 KB
/
model_generator.py
File metadata and controls
185 lines (162 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import importlib.util
import tempfile
import uuid
from contextlib import contextmanager, suppress
from pathlib import Path
from datamodel_code_generator import DatetimeClassType, PythonVersion
from datamodel_code_generator.model import pydantic_v2 as pydantic_model
from datamodel_code_generator.parser.openapi import OpenAPIParser
from datamodel_code_generator.types import StrictTypes
from openapi_to_fastapi.logger import logger
def generate_model_from_schema(
schema: str,
format_code: bool = False,
strict_validation: bool = False,
) -> str:
"""
Given an OpenAPI schema, generate pydantic models from everything defined
in the "components/schemas" section
:param schema: Content of an OpenAPI spec, plain text
:param format_code: Whether to format generated code
:param strict_validation: Whether to use strict validation
:return: Importable python code with generated models
"""
if strict_validation:
strict_types = (
StrictTypes.str,
StrictTypes.bytes,
StrictTypes.int,
StrictTypes.float,
StrictTypes.bool,
)
else:
strict_types = None
if strict_validation:
target_datetime_class = DatetimeClassType.Awaredatetime
else:
target_datetime_class = DatetimeClassType.Datetime
parser = OpenAPIParser(
source=schema,
data_model_type=pydantic_model.BaseModel,
data_model_root_type=pydantic_model.RootModel,
data_type_manager_type=pydantic_model.DataTypeManager,
data_model_field_type=pydantic_model.DataModelField,
base_class="pydantic.BaseModel",
custom_template_dir=None,
extra_template_data=None,
target_python_version=PythonVersion.PY_310,
dump_resolve_reference_action=None,
extra_fields="forbid" if strict_validation else None,
strict_types=strict_types,
field_constraints=False,
snake_case_field=False,
strip_default_none=False,
aliases=None,
target_datetime_class=target_datetime_class,
)
result = str(parser.parse(format_=format_code))
if strict_validation:
result = override_with_stricter_dates(result)
return result
@contextmanager
def _clean_tempfile(tmp_file, delete=True):
try:
yield tmp_file
finally:
if delete:
tmp_file.close()
with suppress(FileNotFoundError):
Path(tmp_file.name).unlink()
def load_models(
schema: str,
name: str = "",
cleanup: bool = True,
format_code: bool = False,
strict_validation: bool = False,
):
"""
Generate pydantic models from OpenAPI spec and return a python module,
which contains all the models from the "components/schemas" section.
This function will create a dedicated python file in OS's temporary dir
and imports it.
:param schema: OpenAPI spec, plain text
:param name: Prefix for a module name, optional
:param cleanup: Whether to remove a file with models afterwards
:param format_code: Whether to format generated code
:param strict_validation: Whether to use strict validation
:return: Module with pydantic models
"""
prefix = name.replace("/", "").replace(" ", "").replace("\\", "") + "_"
with _clean_tempfile(
tempfile.NamedTemporaryFile(
prefix=prefix, mode="w", suffix=".py", encoding="utf8", delete=False
),
delete=cleanup,
) as tmp_file:
model_py = generate_model_from_schema(schema, format_code, strict_validation)
tmp_file.write(model_py)
if not cleanup:
logger.info("Generated module %s: %s", name, tmp_file.name)
tmp_file.flush()
module_name = f"oas_models_{uuid.uuid4()}"
spec = importlib.util.spec_from_file_location(module_name, tmp_file.name)
if spec and spec.loader:
return spec.loader.load_module(module_name)
else:
raise ValueError(f"Failed to load module {module_name}")
def override_with_stricter_dates(file_content: str) -> str:
"""
Overrides the AwareDatetime and date in the python file by identifying the first
class definition (after the imports at the top) and injecting a comment and then
importing the StrictAwareDatetime as AwareDatetime and StrictDate as date which will
thus override the earlier imports.
Example of the file before applying changes:
> from __future__ import annotations
> from pydantic import AwareDatetime, BaseModel, ConfigDict, Field, StrictInt, ...
> from typing import List, Optional, Union
> from datetime import date
>
>
> class BadGateway(BaseModel):
> pass
> ...
Example of file after changes:
> from __future__ import annotations
> from pydantic import AwareDatetime, BaseModel, ConfigDict, Field, StrictInt, ...
> from typing import List, Optional, Union
> from datetime import date
>
> # Overriding the AwareDatetime and date with ones that do stricter validation
> from openapi_to_fastapi.pydantic_validators import StrictAwareDatetime as Aware...
> from openapi_to_fastapi.pydantic_validators import StrictDate as date
>
>
> class BadGateway(BaseModel):
> pass
> ...
:param file_content: The file content as a string.
:return: The modified file content as a string.
"""
comment = (
"# Overriding the AwareDatetime and date with ones that do stricter validation"
)
import_strict_date_time = (
"from openapi_to_fastapi.pydantic_validators import "
"StrictAwareDatetime as AwareDatetime"
)
import_strict_date = (
"from openapi_to_fastapi.pydantic_validators import StrictDate as date"
)
if "AwareDatetime" in file_content or "date" in file_content:
nl = "\n"
if "\r\n" in file_content:
nl = "\r\n"
parts = file_content.partition(f"{nl}{nl}class ")
file_content = (
f"{parts[0]}{nl}"
f"{comment}{nl}"
f"{import_strict_date_time}{nl}"
f"{import_strict_date}{nl}"
f"{parts[1]}{parts[2]}"
)
return file_content