2121from 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
150160def _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
0 commit comments