Skip to content

Commit ae7a60b

Browse files
committed
:feat: Fixed tests for lists
1 parent a6deec7 commit ae7a60b

File tree

5 files changed

+82
-17
lines changed

5 files changed

+82
-17
lines changed

src/openapi_python_generator/language_converters/python/model_generator.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def type_converter(schema: Schema, required: bool = False, model_name: str | No
131131
elif schema.type == "array":
132132
retVal = pre_type + "List["
133133
if isinstance(schema.items, Reference):
134-
converted_reference = _generate_property_from_reference(model_name, "", schema.items, schema)
134+
converted_reference = _generate_property_from_reference(model_name, "", schema.items, schema, required)
135135
import_types = converted_reference.type.import_types
136136
original_type = "array<" + converted_reference.type.original_type + ">"
137137
retVal += converted_reference.type.converted_type
@@ -183,21 +183,22 @@ def _generate_property_from_schema(
183183

184184

185185
def _generate_property_from_reference(
186-
model_name: str, name: str, reference: Reference, parent_schema: Optional[Schema] = None,
186+
model_name: str, name: str, reference: Reference, parent_schema: Optional[Schema] = None, force_required: bool = False
187187
) -> Property:
188188
"""
189189
Generates a property from a reference. It takes the name of the reference as the type, and then
190190
returns a property type
191191
:param name: Name of the schema
192192
:param reference: reference to be converted
193193
:param parent_schema: Component this belongs to
194+
:param force_required: Force the property to be required
194195
:return: Property and model to be imported by the file
195196
"""
196197
required = (
197198
parent_schema is not None
198199
and parent_schema.required is not None
199200
and name in parent_schema.required
200-
)
201+
) or force_required
201202
import_model = reference.ref.split("/")[-1]
202203

203204
if import_model == model_name:
File renamed without changes.

tests/test_generated_code.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import os
3+
import traceback
34
from datetime import datetime
45

56
import orjson
@@ -205,9 +206,14 @@ def test_generate_code(model_data_with_cleanup, library, use_orjson):
205206
exec(exec_code_base, globals(), _locals)
206207
assert root_route.called
207208

208-
exec_code_base = f"from .test_result import *\nresp_result = get_users_users_get()"
209+
exec_code_base = f"try:\n\tfrom .test_result import *\n\tresp_result = get_users_users_get()\nexcept Exception as e:\n\tprint(e)\n\traise e"
209210

210-
exec(exec_code_base, globals(), _locals)
211+
try:
212+
exec(exec_code_base, globals(), _locals)
213+
except Exception as e:
214+
print(e)
215+
print(traceback.format_exc())
216+
raise e
211217

212218
exec(
213219
"from .test_result.services.general_service import *\nassert isinstance(resp_result, list)",

tests/test_model_generator.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
(
6060
Schema(type="array", items=Reference(ref="#/components/schemas/test_name")),
6161
TypeConversion(
62-
original_type="array<test_name>",
62+
original_type="array<#/components/schemas/test_name>",
6363
converted_type="List[test_name]",
6464
import_types=["from .test_name import test_name"],
6565
),
@@ -72,10 +72,19 @@
7272
)
7373
def test_type_converter_simple(test_openapi_types, expected_python_types):
7474
assert type_converter(test_openapi_types, True) == expected_python_types
75-
assert (
76-
type_converter(test_openapi_types, False).converted_type
77-
== "Optional[" + expected_python_types.converted_type + "]"
78-
)
75+
76+
if test_openapi_types.type == "array" and isinstance(test_openapi_types.items, Reference):
77+
expected_type = expected_python_types.converted_type.split("[")[-1].split("]")[0]
78+
79+
assert (
80+
type_converter(test_openapi_types, False).converted_type
81+
== "Optional[List[Optional[" + expected_type + "]]]"
82+
)
83+
else:
84+
assert (
85+
type_converter(test_openapi_types, False).converted_type
86+
== "Optional[" + expected_python_types.converted_type + "]"
87+
)
7988

8089

8190
@pytest.mark.parametrize(
@@ -120,7 +129,7 @@ def test_type_converter_simple(test_openapi_types, expected_python_types):
120129
(
121130
Schema(type="array", items=Reference(ref="#/components/schemas/test_name")),
122131
TypeConversion(
123-
original_type="array<test_name>",
132+
original_type="array<#/components/schemas/test_name>",
124133
converted_type="List[test_name]",
125134
import_types=["from .test_name import test_name"],
126135
),
@@ -135,11 +144,19 @@ def test_type_converter_simple_orjson(test_openapi_types, expected_python_types)
135144
orjson_usage = common.get_use_orjson()
136145
common.set_use_orjson(True)
137146
assert type_converter(test_openapi_types, True) == expected_python_types
138-
assert (
139-
type_converter(test_openapi_types, False).converted_type
140-
== "Optional[" + expected_python_types.converted_type + "]"
141-
)
142-
common.set_use_orjson(orjson_usage)
147+
if test_openapi_types.type == "array" and isinstance(test_openapi_types.items, Reference):
148+
expected_type = expected_python_types.converted_type.split("[")[-1].split("]")[0]
149+
150+
assert (
151+
type_converter(test_openapi_types, False).converted_type
152+
== "Optional[List[Optional[" + expected_type + "]]]"
153+
)
154+
else:
155+
assert (
156+
type_converter(test_openapi_types, False).converted_type
157+
== "Optional[" + expected_python_types.converted_type + "]"
158+
)
159+
common.set_use_orjson(orjson_usage)
143160

144161

145162
def test_type_converter_all_of_reference():
@@ -303,7 +320,7 @@ def test_type_converter_property_reference(
303320
test_name, test_reference, parent_schema, expected_property
304321
):
305322
assert (
306-
_generate_property_from_reference(test_name, test_reference, parent_schema)
323+
_generate_property_from_reference("",test_name, test_reference, parent_schema)
307324
== expected_property
308325
)
309326

tests/test_service_generator.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,47 @@ def test_generate_query_params(test_openapi_operation, expected_result):
370370
complex_type=True,
371371
),
372372
),
373+
(
374+
Operation(
375+
responses={
376+
"200": Response(
377+
description="Successful Response",
378+
content={"application/json": MediaType(media_type_schema=Reference(ref="#/components/schemas/User"))}
379+
)
380+
}
381+
),
382+
OpReturnType(
383+
type=TypeConversion(
384+
original_type="#/components/schemas/User",
385+
converted_type="User",
386+
import_types=["User"],
387+
),
388+
status_code="200",
389+
complex_type=True,
390+
)
391+
392+
),
393+
(
394+
Operation(
395+
responses={
396+
"200": Response(
397+
description="Successful Response",
398+
content={"application/json": MediaType(media_type_schema=Schema(type='array',items=Reference(ref="#/components/schemas/User")))}
399+
)
400+
}
401+
),
402+
OpReturnType(
403+
type=TypeConversion(
404+
original_type="array<#/components/schemas/User>",
405+
converted_type="List[User]",
406+
import_types=['from .User import User'],
407+
),
408+
status_code="200",
409+
complex_type=True,
410+
list_type="User"
411+
)
412+
413+
),
373414
(
374415
Operation(
375416
responses={

0 commit comments

Comments
 (0)