6
6
import click
7
7
from openapi_pydantic .v3 .v3_0 import Schema , Reference , Components
8
8
9
+ from openapi_python_generator .common import PydanticVersion
9
10
from openapi_python_generator .language_converters .python import common
10
11
from openapi_python_generator .language_converters .python .jinja_config import (
11
- ENUM_TEMPLATE ,
12
+ ENUM_TEMPLATE , MODELS_TEMPLATE_PYDANTIC_V2 ,
12
13
)
13
14
from openapi_python_generator .language_converters .python .jinja_config import (
14
15
MODELS_TEMPLATE ,
22
23
23
24
24
25
def type_converter ( # noqa: C901
25
- schema : Schema ,
26
- required : bool = False ,
27
- model_name : Optional [str ] = None ,
26
+ schema : Schema ,
27
+ required : bool = False ,
28
+ model_name : Optional [str ] = None ,
28
29
) -> TypeConversion :
29
30
"""
30
31
Converts an OpenAPI type to a Python type.
@@ -69,13 +70,13 @@ def type_converter( # noqa: C901
69
70
)
70
71
71
72
original_type = (
72
- "tuple<" + "," .join ([i .original_type for i in conversions ]) + ">"
73
+ "tuple<" + "," .join ([i .original_type for i in conversions ]) + ">"
73
74
)
74
75
if len (conversions ) == 1 :
75
76
converted_type = conversions [0 ].converted_type
76
77
else :
77
78
converted_type = (
78
- "Tuple[" + "," .join ([i .converted_type for i in conversions ]) + "]"
79
+ "Tuple[" + "," .join ([i .converted_type for i in conversions ]) + "]"
79
80
)
80
81
81
82
converted_type = pre_type + converted_type + post_type
@@ -101,14 +102,14 @@ def type_converter( # noqa: C901
101
102
)
102
103
)
103
104
original_type = (
104
- "union<" + "," .join ([i .original_type for i in conversions ]) + ">"
105
+ "union<" + "," .join ([i .original_type for i in conversions ]) + ">"
105
106
)
106
107
107
108
if len (conversions ) == 1 :
108
109
converted_type = conversions [0 ].converted_type
109
110
else :
110
111
converted_type = (
111
- "Union[" + "," .join ([i .converted_type for i in conversions ]) + "]"
112
+ "Union[" + "," .join ([i .converted_type for i in conversions ]) + "]"
112
113
)
113
114
114
115
converted_type = pre_type + converted_type + post_type
@@ -120,13 +121,13 @@ def type_converter( # noqa: C901
120
121
# We only want to auto convert to datetime if orjson is used throghout the code, otherwise we can not
121
122
# serialize it to JSON.
122
123
elif schema .type == "string" and (
123
- schema .schema_format is None or not common .get_use_orjson ()
124
+ schema .schema_format is None or not common .get_use_orjson ()
124
125
):
125
126
converted_type = pre_type + "str" + post_type
126
127
elif (
127
- schema .type == "string"
128
- and schema .schema_format .startswith ("uuid" )
129
- and common .get_use_orjson ()
128
+ schema .type == "string"
129
+ and schema .schema_format .startswith ("uuid" )
130
+ and common .get_use_orjson ()
130
131
):
131
132
if len (schema .schema_format ) > 4 and schema .schema_format [4 ].isnumeric ():
132
133
uuid_type = schema .schema_format .upper ()
@@ -154,7 +155,8 @@ def type_converter( # noqa: C901
154
155
original_type = "array<" + converted_reference .type .original_type + ">"
155
156
retVal += converted_reference .type .converted_type
156
157
elif isinstance (schema .items , Schema ):
157
- original_type = "array<" + (str (schema .items .type .value ) if schema .items .type is not None else "unknown" )+ ">"
158
+ original_type = "array<" + (
159
+ str (schema .items .type .value ) if schema .items .type is not None else "unknown" ) + ">"
158
160
retVal += type_converter (schema .items , True ).converted_type
159
161
else :
160
162
original_type = "array<unknown>"
@@ -178,7 +180,7 @@ def type_converter( # noqa: C901
178
180
179
181
180
182
def _generate_property_from_schema (
181
- model_name : str , name : str , schema : Schema , parent_schema : Optional [Schema ] = None
183
+ model_name : str , name : str , schema : Schema , parent_schema : Optional [Schema ] = None
182
184
) -> Property :
183
185
"""
184
186
Generates a property from a schema. It takes the type of the schema and converts it to a python type, and then
@@ -190,9 +192,9 @@ def _generate_property_from_schema(
190
192
:return: Property
191
193
"""
192
194
required = (
193
- parent_schema is not None
194
- and parent_schema .required is not None
195
- and name in parent_schema .required
195
+ parent_schema is not None
196
+ and parent_schema .required is not None
197
+ and name in parent_schema .required
196
198
)
197
199
198
200
import_type = None
@@ -209,11 +211,11 @@ def _generate_property_from_schema(
209
211
210
212
211
213
def _generate_property_from_reference (
212
- model_name : str ,
213
- name : str ,
214
- reference : Reference ,
215
- parent_schema : Optional [Schema ] = None ,
216
- force_required : bool = False ,
214
+ model_name : str ,
215
+ name : str ,
216
+ reference : Reference ,
217
+ parent_schema : Optional [Schema ] = None ,
218
+ force_required : bool = False ,
217
219
) -> Property :
218
220
"""
219
221
Generates a property from a reference. It takes the name of the reference as the type, and then
@@ -225,10 +227,10 @@ def _generate_property_from_reference(
225
227
:return: Property and model to be imported by the file
226
228
"""
227
229
required = (
228
- parent_schema is not None
229
- and parent_schema .required is not None
230
- and name in parent_schema .required
231
- ) or force_required
230
+ parent_schema is not None
231
+ and parent_schema .required is not None
232
+ and name in parent_schema .required
233
+ ) or force_required
232
234
import_model = common .normalize_symbol (reference .ref .split ("/" )[- 1 ])
233
235
234
236
if import_model == model_name :
@@ -256,13 +258,14 @@ def _generate_property_from_reference(
256
258
)
257
259
258
260
259
- def generate_models (components : Components ) -> List [Model ]:
261
+ def generate_models (components : Components , pydantic_version : PydanticVersion = PydanticVersion . V2 ) -> List [Model ]:
260
262
"""
261
263
Receives components from an OpenAPI 3.0 specification and generates the models from it.
262
264
It does so, by iterating over the components.schemas dictionary. For each schema, it checks if
263
265
it is a normal schema (i.e. simple type like string, integer, etc.), a reference to another schema, or
264
266
an array of types/references. It then computes pydantic models from it using jinja2
265
267
:param components: The components from an OpenAPI 3.0 specification.
268
+ :param pydantic_version: The version of pydantic to use.
266
269
:return: A list of models.
267
270
"""
268
271
models : List [Model ] = []
@@ -313,7 +316,9 @@ def generate_models(components: Components) -> List[Model]:
313
316
)
314
317
properties .append (conv_property )
315
318
316
- generated_content = jinja_env .get_template (MODELS_TEMPLATE ).render (
319
+ template_name = MODELS_TEMPLATE_PYDANTIC_V2 if pydantic_version == PydanticVersion .V2 else MODELS_TEMPLATE
320
+
321
+ generated_content = jinja_env .get_template (template_name ).render (
317
322
schema_name = name , schema = schema_or_reference , properties = properties
318
323
)
319
324
0 commit comments