Skip to content

Commit 0ef4e9d

Browse files
committed
:feat: Added check for self referencing, in which case we use "" imports
1 parent 45462da commit 0ef4e9d

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

src/openapi_python_generator/language_converters/python/model_generator.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
from openapi_python_generator.models import TypeConversion
2222

2323

24-
def type_converter(schema: Schema, required: bool = False) -> TypeConversion:
24+
def type_converter(schema: Schema, model_name: str | None = None, required: bool = False) -> TypeConversion:
2525
"""
2626
Converts an OpenAPI type to a Python type.
2727
:param schema: Schema containing the type to be converted
28+
:param model_name: Name of the original model on which the type is defined
2829
:param required: Flag indicating if the type is required by the class
2930
:return: The converted type
3031
"""
@@ -45,14 +46,23 @@ def type_converter(schema: Schema, required: bool = False) -> TypeConversion:
4546
conversions.append(type_converter(sub_schema, True))
4647
else:
4748
import_type = sub_schema.ref.split("/")[-1]
48-
import_types = [f"from .{import_type} import {import_type}"]
49-
conversions.append(
50-
TypeConversion(
51-
original_type=sub_schema.ref,
52-
converted_type=import_type,
53-
import_types=import_types,
49+
if import_type == model_name:
50+
conversions.append(
51+
TypeConversion(
52+
original_type=sub_schema.ref,
53+
converted_type='"' + model_name + '"',
54+
import_types=None,
55+
)
56+
)
57+
else:
58+
import_types = [f"from .{import_type} import {import_type}"]
59+
conversions.append(
60+
TypeConversion(
61+
original_type=sub_schema.ref,
62+
converted_type=import_type,
63+
import_types=import_types,
64+
)
5465
)
55-
)
5666

5767
original_type = (
5868
"tuple<" + ",".join([i.original_type for i in conversions]) + ">"
@@ -148,11 +158,12 @@ def type_converter(schema: Schema, required: bool = False) -> TypeConversion:
148158

149159

150160
def _generate_property_from_schema(
151-
name: str, schema: Schema, parent_schema: Optional[Schema] = None
161+
model_name : str, name: str, schema: Schema, parent_schema: Optional[Schema] = None
152162
) -> Property:
153163
"""
154164
Generates a property from a schema. It takes the type of the schema and converts it to a python type, and then
155165
creates the according property.
166+
:param model_name: Name of the model this property belongs to
156167
:param name: Name of the schema
157168
:param schema: schema to be converted
158169
:param parent_schema: Component this belongs to
@@ -165,7 +176,7 @@ def _generate_property_from_schema(
165176
)
166177
return Property(
167178
name=name,
168-
type=type_converter(schema, required),
179+
type=type_converter(schema, model_name, required),
169180
required=required,
170181
default=None if required else "None",
171182
)
@@ -254,7 +265,7 @@ def generate_models(components: Components) -> List[Model]:
254265
)
255266
else:
256267
conv_property = _generate_property_from_schema(
257-
prop_name, property, schema_or_reference
268+
name, prop_name, property, schema_or_reference
258269
)
259270
properties.append(conv_property)
260271

tests/test_model_generator.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,10 @@ def test_type_converter_exceptions():
211211

212212

213213
@pytest.mark.parametrize(
214-
"test_name, test_schema, test_parent_schema, expected_property",
214+
"test_model_name, test_name, test_schema, test_parent_schema, expected_property",
215215
[
216216
(
217+
"SomeModel",
217218
"test_name",
218219
Schema(type="string"),
219220
Schema(type="object"),
@@ -227,6 +228,7 @@ def test_type_converter_exceptions():
227228
),
228229
),
229230
(
231+
"SomeModel",
230232
"test_name",
231233
Schema(type="string"),
232234
Schema(type="object", required=["test_name"]),
@@ -237,13 +239,25 @@ def test_type_converter_exceptions():
237239
imported_type=["test_name"],
238240
),
239241
),
242+
(
243+
"SomeModel",
244+
"SomeModel",
245+
Schema(allOf=[Reference(ref="#/components/schemas/SomeModel")]),
246+
Schema(type="object", required=["SomeModel"]),
247+
Property(
248+
name='SomeModel',
249+
type=TypeConversion(original_type='tuple<#/components/schemas/SomeModel>', converted_type='"SomeModel"',import_types = []),
250+
required=True,
251+
imported_type=[],
252+
),
253+
),
240254
],
241255
)
242256
def test_type_converter_property(
243-
test_name, test_schema, test_parent_schema, expected_property
257+
test_model_name, test_name, test_schema, test_parent_schema, expected_property
244258
):
245259
assert (
246-
_generate_property_from_schema(test_name, test_schema, test_parent_schema)
260+
_generate_property_from_schema(test_model_name,test_name, test_schema, test_parent_schema)
247261
== expected_property
248262
)
249263

0 commit comments

Comments
 (0)