Skip to content

Commit 9ac2678

Browse files
committed
Clean up templates and document changes
1 parent a10bba7 commit 9ac2678

17 files changed

+62
-131
lines changed

.changeset/fix_lists_within_unions.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
default: patch
3+
---
4+
5+
# Fix lists within unions
6+
7+
Fixes #756 and #928. Arrays within unions (which, as of 0.17 includes nullable arrays) would generate invalid code.
8+
9+
Thanks @kgutwin and @diesieben07!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
default: patch
3+
---
4+
5+
# Simplify type checks for non-required unions

end_to_end_tests/golden-record/my_test_api_client/api/defaults/defaults_tests_defaults_post.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,7 @@ def _get_kwargs(
5858
if isinstance(union_prop_with_ref, Unset):
5959
json_union_prop_with_ref = UNSET
6060
elif isinstance(union_prop_with_ref, AnEnum):
61-
json_union_prop_with_ref = UNSET
62-
if not isinstance(union_prop_with_ref, Unset):
63-
json_union_prop_with_ref = union_prop_with_ref.value
64-
61+
json_union_prop_with_ref = union_prop_with_ref.value
6562
else:
6663
json_union_prop_with_ref = union_prop_with_ref
6764
params["union_prop_with_ref"] = json_union_prop_with_ref

end_to_end_tests/golden-record/my_test_api_client/api/location/get_location_query_optionality.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ def _get_kwargs(
3232
if isinstance(null_not_required, Unset):
3333
json_null_not_required = UNSET
3434
elif isinstance(null_not_required, datetime.datetime):
35-
json_null_not_required = UNSET
36-
if not isinstance(null_not_required, Unset):
37-
json_null_not_required = null_not_required.isoformat()
35+
json_null_not_required = null_not_required.isoformat()
3836
else:
3937
json_null_not_required = null_not_required
4038
params["null_not_required"] = json_null_not_required

end_to_end_tests/golden-record/my_test_api_client/models/a_model.py

+10-51
Original file line numberDiff line numberDiff line change
@@ -159,25 +159,17 @@ def to_dict(self) -> Dict[str, Any]:
159159
if isinstance(self.not_required_one_of_models, Unset):
160160
not_required_one_of_models = UNSET
161161
elif isinstance(self.not_required_one_of_models, FreeFormModel):
162-
not_required_one_of_models = UNSET
163-
if not isinstance(self.not_required_one_of_models, Unset):
164-
not_required_one_of_models = self.not_required_one_of_models.to_dict()
162+
not_required_one_of_models = self.not_required_one_of_models.to_dict()
165163
else:
166-
not_required_one_of_models = UNSET
167-
if not isinstance(self.not_required_one_of_models, Unset):
168-
not_required_one_of_models = self.not_required_one_of_models.to_dict()
164+
not_required_one_of_models = self.not_required_one_of_models.to_dict()
169165

170166
not_required_nullable_one_of_models: Union[Dict[str, Any], None, Unset, str]
171167
if isinstance(self.not_required_nullable_one_of_models, Unset):
172168
not_required_nullable_one_of_models = UNSET
173169
elif isinstance(self.not_required_nullable_one_of_models, FreeFormModel):
174-
not_required_nullable_one_of_models = UNSET
175-
if not isinstance(self.not_required_nullable_one_of_models, Unset):
176-
not_required_nullable_one_of_models = self.not_required_nullable_one_of_models.to_dict()
170+
not_required_nullable_one_of_models = self.not_required_nullable_one_of_models.to_dict()
177171
elif isinstance(self.not_required_nullable_one_of_models, ModelWithUnionProperty):
178-
not_required_nullable_one_of_models = UNSET
179-
if not isinstance(self.not_required_nullable_one_of_models, Unset):
180-
not_required_nullable_one_of_models = self.not_required_nullable_one_of_models.to_dict()
172+
not_required_nullable_one_of_models = self.not_required_nullable_one_of_models.to_dict()
181173
else:
182174
not_required_nullable_one_of_models = self.not_required_nullable_one_of_models
183175

@@ -189,9 +181,7 @@ def to_dict(self) -> Dict[str, Any]:
189181
if isinstance(self.not_required_nullable_model, Unset):
190182
not_required_nullable_model = UNSET
191183
elif isinstance(self.not_required_nullable_model, ModelWithUnionProperty):
192-
not_required_nullable_model = UNSET
193-
if not isinstance(self.not_required_nullable_model, Unset):
194-
not_required_nullable_model = self.not_required_nullable_model.to_dict()
184+
not_required_nullable_model = self.not_required_nullable_model.to_dict()
195185
else:
196186
not_required_nullable_model = self.not_required_nullable_model
197187

@@ -401,24 +391,14 @@ def _parse_not_required_one_of_models(data: object) -> Union["FreeFormModel", "M
401391
try:
402392
if not isinstance(data, dict):
403393
raise TypeError()
404-
_not_required_one_of_models_type_0 = data
405-
not_required_one_of_models_type_0: Union[Unset, FreeFormModel]
406-
if isinstance(_not_required_one_of_models_type_0, Unset):
407-
not_required_one_of_models_type_0 = UNSET
408-
else:
409-
not_required_one_of_models_type_0 = FreeFormModel.from_dict(_not_required_one_of_models_type_0)
394+
not_required_one_of_models_type_0 = FreeFormModel.from_dict(data)
410395

411396
return not_required_one_of_models_type_0
412397
except: # noqa: E722
413398
pass
414399
if not isinstance(data, dict):
415400
raise TypeError()
416-
_not_required_one_of_models_type_1 = data
417-
not_required_one_of_models_type_1: Union[Unset, ModelWithUnionProperty]
418-
if isinstance(_not_required_one_of_models_type_1, Unset):
419-
not_required_one_of_models_type_1 = UNSET
420-
else:
421-
not_required_one_of_models_type_1 = ModelWithUnionProperty.from_dict(_not_required_one_of_models_type_1)
401+
not_required_one_of_models_type_1 = ModelWithUnionProperty.from_dict(data)
422402

423403
return not_required_one_of_models_type_1
424404

@@ -434,29 +414,15 @@ def _parse_not_required_nullable_one_of_models(
434414
try:
435415
if not isinstance(data, dict):
436416
raise TypeError()
437-
_not_required_nullable_one_of_models_type_0 = data
438-
not_required_nullable_one_of_models_type_0: Union[Unset, FreeFormModel]
439-
if isinstance(_not_required_nullable_one_of_models_type_0, Unset):
440-
not_required_nullable_one_of_models_type_0 = UNSET
441-
else:
442-
not_required_nullable_one_of_models_type_0 = FreeFormModel.from_dict(
443-
_not_required_nullable_one_of_models_type_0
444-
)
417+
not_required_nullable_one_of_models_type_0 = FreeFormModel.from_dict(data)
445418

446419
return not_required_nullable_one_of_models_type_0
447420
except: # noqa: E722
448421
pass
449422
try:
450423
if not isinstance(data, dict):
451424
raise TypeError()
452-
_not_required_nullable_one_of_models_type_1 = data
453-
not_required_nullable_one_of_models_type_1: Union[Unset, ModelWithUnionProperty]
454-
if isinstance(_not_required_nullable_one_of_models_type_1, Unset):
455-
not_required_nullable_one_of_models_type_1 = UNSET
456-
else:
457-
not_required_nullable_one_of_models_type_1 = ModelWithUnionProperty.from_dict(
458-
_not_required_nullable_one_of_models_type_1
459-
)
425+
not_required_nullable_one_of_models_type_1 = ModelWithUnionProperty.from_dict(data)
460426

461427
return not_required_nullable_one_of_models_type_1
462428
except: # noqa: E722
@@ -482,14 +448,7 @@ def _parse_not_required_nullable_model(data: object) -> Union["ModelWithUnionPro
482448
try:
483449
if not isinstance(data, dict):
484450
raise TypeError()
485-
_not_required_nullable_model_type_1 = data
486-
not_required_nullable_model_type_1: Union[Unset, ModelWithUnionProperty]
487-
if isinstance(_not_required_nullable_model_type_1, Unset):
488-
not_required_nullable_model_type_1 = UNSET
489-
else:
490-
not_required_nullable_model_type_1 = ModelWithUnionProperty.from_dict(
491-
_not_required_nullable_model_type_1
492-
)
451+
not_required_nullable_model_type_1 = ModelWithUnionProperty.from_dict(data)
493452

494453
return not_required_nullable_model_type_1
495454
except: # noqa: E722

end_to_end_tests/golden-record/my_test_api_client/models/body_upload_file_tests_upload_post.py

+10-14
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,10 @@ def to_dict(self) -> Dict[str, Any]:
9494
if isinstance(self.some_array, Unset):
9595
some_array = UNSET
9696
elif isinstance(self.some_array, list):
97-
some_array = UNSET
98-
if not isinstance(self.some_array, Unset):
99-
some_array = []
100-
for some_array_type_0_item_data in self.some_array:
101-
some_array_type_0_item = some_array_type_0_item_data.to_dict()
102-
some_array.append(some_array_type_0_item)
97+
some_array = []
98+
for some_array_type_0_item_data in self.some_array:
99+
some_array_type_0_item = some_array_type_0_item_data.to_dict()
100+
some_array.append(some_array_type_0_item)
103101

104102
else:
105103
some_array = self.some_array
@@ -180,13 +178,11 @@ def to_multipart(self) -> Dict[str, Any]:
180178
if isinstance(self.some_array, Unset):
181179
some_array = UNSET
182180
elif isinstance(self.some_array, list):
183-
some_array = UNSET
184-
if not isinstance(self.some_array, Unset):
185-
_temp_some_array = []
186-
for some_array_type_0_item_data in self.some_array:
187-
some_array_type_0_item = some_array_type_0_item_data.to_dict()
188-
_temp_some_array.append(some_array_type_0_item)
189-
some_array = (None, json.dumps(_temp_some_array).encode(), "application/json")
181+
_temp_some_array = []
182+
for some_array_type_0_item_data in self.some_array:
183+
some_array_type_0_item = some_array_type_0_item_data.to_dict()
184+
_temp_some_array.append(some_array_type_0_item)
185+
some_array = (None, json.dumps(_temp_some_array).encode(), "application/json")
190186

191187
else:
192188
some_array = self.some_array
@@ -297,7 +293,7 @@ def _parse_some_array(data: object) -> Union[List["AFormData"], None, Unset]:
297293
raise TypeError()
298294
some_array_type_0 = []
299295
_some_array_type_0 = data
300-
for some_array_type_0_item_data in _some_array_type_0 or []:
296+
for some_array_type_0_item_data in _some_array_type_0:
301297
some_array_type_0_item = AFormData.from_dict(some_array_type_0_item_data)
302298

303299
some_array_type_0.append(some_array_type_0_item)

end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py

+4-19
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,9 @@ def to_dict(self) -> Dict[str, Any]:
2323
if isinstance(self.a_property, Unset):
2424
a_property = UNSET
2525
elif isinstance(self.a_property, AnEnum):
26-
a_property = UNSET
27-
if not isinstance(self.a_property, Unset):
28-
a_property = self.a_property.value
29-
26+
a_property = self.a_property.value
3027
else:
31-
a_property = UNSET
32-
if not isinstance(self.a_property, Unset):
33-
a_property = self.a_property.value
28+
a_property = self.a_property.value
3429

3530
field_dict: Dict[str, Any] = {}
3631
field_dict.update({})
@@ -49,24 +44,14 @@ def _parse_a_property(data: object) -> Union[AnEnum, AnIntEnum, Unset]:
4944
try:
5045
if not isinstance(data, str):
5146
raise TypeError()
52-
_a_property_type_0 = data
53-
a_property_type_0: Union[Unset, AnEnum]
54-
if isinstance(_a_property_type_0, Unset):
55-
a_property_type_0 = UNSET
56-
else:
57-
a_property_type_0 = AnEnum(_a_property_type_0)
47+
a_property_type_0 = AnEnum(data)
5848

5949
return a_property_type_0
6050
except: # noqa: E722
6151
pass
6252
if not isinstance(data, int):
6353
raise TypeError()
64-
_a_property_type_1 = data
65-
a_property_type_1: Union[Unset, AnIntEnum]
66-
if isinstance(_a_property_type_1, Unset):
67-
a_property_type_1 = UNSET
68-
else:
69-
a_property_type_1 = AnIntEnum(_a_property_type_1)
54+
a_property_type_1 = AnIntEnum(data)
7055

7156
return a_property_type_1
7257

end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property_inlined.py

+4-18
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,9 @@ def to_dict(self) -> Dict[str, Any]:
2828
if isinstance(self.fruit, Unset):
2929
fruit = UNSET
3030
elif isinstance(self.fruit, ModelWithUnionPropertyInlinedFruitType0):
31-
fruit = UNSET
32-
if not isinstance(self.fruit, Unset):
33-
fruit = self.fruit.to_dict()
31+
fruit = self.fruit.to_dict()
3432
else:
35-
fruit = UNSET
36-
if not isinstance(self.fruit, Unset):
37-
fruit = self.fruit.to_dict()
33+
fruit = self.fruit.to_dict()
3834

3935
field_dict: Dict[str, Any] = {}
4036
field_dict.update({})
@@ -58,24 +54,14 @@ def _parse_fruit(
5854
try:
5955
if not isinstance(data, dict):
6056
raise TypeError()
61-
_fruit_type_0 = data
62-
fruit_type_0: Union[Unset, ModelWithUnionPropertyInlinedFruitType0]
63-
if isinstance(_fruit_type_0, Unset):
64-
fruit_type_0 = UNSET
65-
else:
66-
fruit_type_0 = ModelWithUnionPropertyInlinedFruitType0.from_dict(_fruit_type_0)
57+
fruit_type_0 = ModelWithUnionPropertyInlinedFruitType0.from_dict(data)
6758

6859
return fruit_type_0
6960
except: # noqa: E722
7061
pass
7162
if not isinstance(data, dict):
7263
raise TypeError()
73-
_fruit_type_1 = data
74-
fruit_type_1: Union[Unset, ModelWithUnionPropertyInlinedFruitType1]
75-
if isinstance(_fruit_type_1, Unset):
76-
fruit_type_1 = UNSET
77-
else:
78-
fruit_type_1 = ModelWithUnionPropertyInlinedFruitType1.from_dict(_fruit_type_1)
64+
fruit_type_1 = ModelWithUnionPropertyInlinedFruitType1.from_dict(data)
7965

8066
return fruit_type_1
8167

openapi_python_client/parser/properties/union.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def build(
5757
for i, sub_prop_data in enumerate(chain(data.anyOf, data.oneOf, type_list_data)):
5858
sub_prop, schemas = property_from_data(
5959
name=f"{name}_type_{i}",
60-
required=required,
60+
required=True,
6161
data=sub_prop_data,
6262
schemas=schemas,
6363
parent_name=parent_name,

openapi_python_client/templates/property_templates/date_property.py.jinja

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ isoparse({{ source }}).date()
44

55
{% from "property_templates/property_macros.py.jinja" import construct_template %}
66

7-
{% macro construct(property, source, initial_value=None) %}
8-
{{ construct_template(construct_function, property, source, initial_value=initial_value) }}
7+
{% macro construct(property, source) %}
8+
{{ construct_template(construct_function, property, source) }}
99
{% endmacro %}
1010

1111
{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, str){% endmacro %}

openapi_python_client/templates/property_templates/datetime_property.py.jinja

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ isoparse({{ source }})
44

55
{% from "property_templates/property_macros.py.jinja" import construct_template %}
66

7-
{% macro construct(property, source, initial_value=None) %}
8-
{{ construct_template(construct_function, property, source, initial_value=initial_value) }}
7+
{% macro construct(property, source) %}
8+
{{ construct_template(construct_function, property, source) }}
99
{% endmacro %}
1010

1111
{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, str){% endmacro %}

openapi_python_client/templates/property_templates/enum_property.py.jinja

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
{% from "property_templates/property_macros.py.jinja" import construct_template %}
66

7-
{% macro construct(property, source, initial_value=None) %}
8-
{{ construct_template(construct_function, property, source, initial_value=initial_value) }}
7+
{% macro construct(property, source) %}
8+
{{ construct_template(construct_function, property, source) }}
99
{% endmacro %}
1010

1111
{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, {{ property.value_type.__name__ }}){% endmacro %}

openapi_python_client/templates/property_templates/file_property.py.jinja

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ File(
66

77
{% from "property_templates/property_macros.py.jinja" import construct_template %}
88

9-
{% macro construct(property, source, initial_value=None) %}
10-
{{ construct_template(construct_function, property, source, initial_value=initial_value) }}
9+
{% macro construct(property, source) %}
10+
{{ construct_template(construct_function, property, source) }}
1111
{% endmacro %}
1212

1313
{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, bytes){% endmacro %}

openapi_python_client/templates/property_templates/list_property.py.jinja

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
{% macro construct(property, source, initial_value="[]") %}
1+
{% macro construct(property, source) %}
22
{% set inner_property = property.inner_property %}
33
{% import "property_templates/" + inner_property.template as inner_template %}
44
{% if inner_template.construct %}
55
{% set inner_source = inner_property.python_name + "_data" %}
6-
{% if initial_value == "UNSET" %}
7-
{% set initial_value = "[]" %}
8-
{% endif %}
9-
{{ property.python_name }} = {{ initial_value }}
6+
{{ property.python_name }} = []
107
_{{ property.python_name }} = {{ source }}
118
{% if property.required %}
129
for {{ inner_source }} in (_{{ property.python_name }}):

openapi_python_client/templates/property_templates/model_property.py.jinja

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
{% from "property_templates/property_macros.py.jinja" import construct_template %}
66

7-
{% macro construct(property, source, initial_value=None) %}
8-
{{ construct_template(construct_function, property, source, initial_value=initial_value) }}
7+
{% macro construct(property, source) %}
8+
{{ construct_template(construct_function, property, source) }}
99
{% endmacro %}
1010

1111
{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, dict){% endmacro %}

openapi_python_client/templates/property_templates/property_macros.py.jinja

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
{% macro construct_template(construct_function, property, source, initial_value=None) %}
1+
{% macro construct_template(construct_function, property, source) %}
22
{% if property.required %}
33
{{ property.python_name }} = {{ construct_function(property, source) }}
44
{% else %}{# Must be non-required #}
55
_{{ property.python_name }} = {{ source }}
66
{{ property.python_name }}: {{ property.get_type_string() }}
77
{% if not property.required %}
88
if isinstance(_{{ property.python_name }}, Unset):
9-
{{ property.python_name }} = {% if initial_value != None %}{{ initial_value }}{% else %}UNSET{% endif %}
10-
9+
{{ property.python_name }} = UNSET
1110
{% endif %}
1211
else:
1312
{{ property.python_name }} = {{ construct_function(property, "_" + property.python_name) }}

0 commit comments

Comments
 (0)