Skip to content

Commit d24b3e5

Browse files
committed
Improve grouping of each multipart field
1 parent 0eba399 commit d24b3e5

File tree

8 files changed

+35
-29
lines changed

8 files changed

+35
-29
lines changed

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -170,29 +170,29 @@ def to_dict(self) -> dict[str, Any]:
170170

171171
def to_multipart(self) -> list[tuple[str, Any]]:
172172
field_list: list[tuple[str, Any]] = []
173-
some_file = self.some_file.to_tuple()
174173

174+
some_file = self.some_file.to_tuple()
175175
field_list.append(("some_file", some_file))
176-
some_required_number = (None, str(self.some_required_number).encode(), "text/plain")
177176

177+
some_required_number = (None, str(self.some_required_number).encode(), "text/plain")
178178
field_list.append(("some_required_number", some_required_number))
179-
some_object = (None, json.dumps(self.some_object.to_dict()).encode(), "application/json")
180179

180+
some_object = (None, json.dumps(self.some_object.to_dict()).encode(), "application/json")
181181
field_list.append(("some_object", some_object))
182-
some_nullable_object: tuple[None, bytes, str]
183182

183+
some_nullable_object: tuple[None, bytes, str]
184184
if isinstance(self.some_nullable_object, BodyUploadFileTestsUploadPostSomeNullableObject):
185185
some_nullable_object = (None, json.dumps(self.some_nullable_object.to_dict()).encode(), "application/json")
186186
else:
187187
some_nullable_object = (None, str(self.some_nullable_object).encode(), "text/plain")
188-
189188
field_list.append(("some_nullable_object", some_nullable_object))
189+
190190
some_optional_file: Union[Unset, FileJsonType] = UNSET
191191
if not isinstance(self.some_optional_file, Unset):
192192
some_optional_file = self.some_optional_file.to_tuple()
193-
194193
if some_optional_file is not UNSET:
195194
field_list.append(("some_optional_file", some_optional_file))
195+
196196
some_string = (
197197
self.some_string
198198
if isinstance(self.some_string, Unset)
@@ -201,18 +201,19 @@ def to_multipart(self) -> list[tuple[str, Any]]:
201201

202202
if some_string is not UNSET:
203203
field_list.append(("some_string", some_string))
204+
204205
a_datetime: Union[Unset, bytes] = UNSET
205206
if not isinstance(self.a_datetime, Unset):
206207
a_datetime = self.a_datetime.isoformat().encode()
207-
208208
if a_datetime is not UNSET:
209209
field_list.append(("a_datetime", a_datetime))
210+
210211
a_date: Union[Unset, bytes] = UNSET
211212
if not isinstance(self.a_date, Unset):
212213
a_date = self.a_date.isoformat().encode()
213-
214214
if a_date is not UNSET:
215215
field_list.append(("a_date", a_date))
216+
216217
some_number = (
217218
self.some_number
218219
if isinstance(self.some_number, Unset)
@@ -221,31 +222,30 @@ def to_multipart(self) -> list[tuple[str, Any]]:
221222

222223
if some_number is not UNSET:
223224
field_list.append(("some_number", some_number))
224-
some_nullable_number: Union[Unset, tuple[None, bytes, str]]
225225

226+
some_nullable_number: Union[Unset, tuple[None, bytes, str]]
226227
if isinstance(self.some_nullable_number, Unset):
227228
some_nullable_number = UNSET
229+
228230
elif isinstance(self.some_nullable_number, float):
229231
some_nullable_number = (None, str(self.some_nullable_number).encode(), "text/plain")
230232
else:
231233
some_nullable_number = (None, str(self.some_nullable_number).encode(), "text/plain")
232-
233234
if some_nullable_number is not UNSET:
234235
field_list.append(("some_nullable_number", some_nullable_number))
236+
235237
for some_int_array_element in self.some_int_array or []:
236238
some_int_array_item: tuple[None, bytes, str]
237-
238239
if isinstance(some_int_array_element, int):
239240
some_int_array_item = (None, str(some_int_array_element).encode(), "text/plain")
240241
else:
241242
some_int_array_item = (None, str(some_int_array_element).encode(), "text/plain")
242-
243243
field_list.append(("some_int_array", some_int_array_item))
244244

245245
some_array: Union[Unset, tuple[None, bytes, str]]
246-
247246
if isinstance(self.some_array, Unset):
248247
some_array = UNSET
248+
249249
elif isinstance(self.some_array, list):
250250
_temp_some_array = []
251251
for some_array_type_0_item_data in self.some_array:
@@ -254,15 +254,15 @@ def to_multipart(self) -> list[tuple[str, Any]]:
254254
some_array = (None, json.dumps(_temp_some_array).encode(), "application/json")
255255
else:
256256
some_array = (None, str(self.some_array).encode(), "text/plain")
257-
258257
if some_array is not UNSET:
259258
field_list.append(("some_array", some_array))
259+
260260
some_optional_object: Union[Unset, tuple[None, bytes, str]] = UNSET
261261
if not isinstance(self.some_optional_object, Unset):
262262
some_optional_object = (None, json.dumps(self.some_optional_object.to_dict()).encode(), "application/json")
263-
264263
if some_optional_object is not UNSET:
265264
field_list.append(("some_optional_object", some_optional_object))
265+
266266
some_enum: Union[Unset, tuple[None, bytes, str]] = UNSET
267267
if not isinstance(self.some_enum, Unset):
268268
some_enum = (None, str(self.some_enum.value).encode(), "text/plain")

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def to_dict(self) -> dict[str, Any]:
3232

3333
def to_multipart(self) -> list[tuple[str, Any]]:
3434
field_list: list[tuple[str, Any]] = []
35+
3536
a = self.a if isinstance(self.a, Unset) else (None, str(self.a).encode(), "text/plain")
3637

3738
if a is not UNSET:

end_to_end_tests/literal-enums-golden-record/my_enum_api_client/models/post_user_list_body.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,17 @@ def to_dict(self) -> dict[str, Any]:
9696

9797
def to_multipart(self) -> list[tuple[str, Any]]:
9898
field_list: list[tuple[str, Any]] = []
99+
99100
for an_enum_value_element in self.an_enum_value or []:
100101
an_enum_value_item = (None, str(an_enum_value_element).encode(), "text/plain")
101-
102102
field_list.append(("an_enum_value", an_enum_value_item))
103103

104104
for an_enum_value_with_null_element in self.an_enum_value_with_null or []:
105105
an_enum_value_with_null_item: tuple[None, bytes, str]
106-
107106
if an_enum_value_with_null_element is None:
108107
an_enum_value_with_null_item = (None, str(an_enum_value_with_null_element).encode(), "text/plain")
109108
else:
110109
an_enum_value_with_null_item = (None, str(an_enum_value_with_null_element).encode(), "text/plain")
111-
112110
field_list.append(("an_enum_value_with_null", an_enum_value_with_null_item))
113111

114112
for an_enum_value_with_only_null_element in self.an_enum_value_with_only_null or []:
@@ -126,12 +124,14 @@ def to_multipart(self) -> list[tuple[str, Any]]:
126124

127125
if an_allof_enum_with_overridden_default is not UNSET:
128126
field_list.append(("an_allof_enum_with_overridden_default", an_allof_enum_with_overridden_default))
127+
129128
an_optional_allof_enum: Union[Unset, tuple[None, bytes, str]] = UNSET
130129
if not isinstance(self.an_optional_allof_enum, Unset):
131130
an_optional_allof_enum = (None, str(self.an_optional_allof_enum).encode(), "text/plain")
132131

133132
if an_optional_allof_enum is not UNSET:
134133
field_list.append(("an_optional_allof_enum", an_optional_allof_enum))
134+
135135
for nested_list_of_enums_element in self.nested_list_of_enums or []:
136136
_temp_nested_list_of_enums_item = []
137137
for nested_list_of_enums_item_item_data in nested_list_of_enums_element:

integration-tests/integration_tests/models/post_body_multipart_body.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ def to_dict(self) -> dict[str, Any]:
4747

4848
def to_multipart(self) -> list[tuple[str, Any]]:
4949
field_list: list[tuple[str, Any]] = []
50+
5051
a_string = (None, str(self.a_string).encode(), "text/plain")
5152

5253
field_list.append(("a_string", a_string))
53-
file = self.file.to_tuple()
5454

55+
file = self.file.to_tuple()
5556
field_list.append(("file", file))
57+
5658
description = (
5759
self.description
5860
if isinstance(self.description, Unset)

openapi_python_client/templates/model.py.jinja

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,13 @@ class {{ class_name }}:
8484

8585
{% macro _transform_property(property, content, multipart=False) %}
8686
{% import "property_templates/" + property.template as prop_template %}
87-
{% if multipart %}
87+
{%- if multipart -%}
8888
{{ prop_template.transform_multipart(property, content, property.python_name) }}
89-
{% elif prop_template.transform %}
89+
{%- elif prop_template.transform -%}
9090
{{ prop_template.transform(property=property, source=content, destination=property.python_name) }}
91-
{% else %}
91+
{%- else -%}
9292
{{ property.python_name }} = {{ content }}
93-
{% endif %}
94-
93+
{%- endif -%}
9594
{% endmacro %}
9695

9796
{% macro _prepare_field_dict(multipart=False) %}
@@ -113,6 +112,7 @@ field_dict.update(self.additional_properties)
113112
{% macro _to_dict() %}
114113
{% for property in model.required_properties + model.optional_properties -%}
115114
{{ _transform_property(property, "self." + property.python_name) }}
115+
116116
{% endfor %}
117117

118118
{{ _prepare_field_dict() }}
@@ -144,6 +144,7 @@ return field_dict
144144
{% if model.is_multipart_body %}
145145
def to_multipart(self) -> list[tuple[str, Any]]:
146146
field_list: list[tuple[str, Any]] = []
147+
147148
{% for property in model.required_properties + model.optional_properties %}
148149
{% if property.__class__.__name__ == 'ListProperty' %}
149150
{% if not property.required %}
@@ -163,6 +164,7 @@ return field_dict
163164
field_list.append(("{{ property.python_name }}", {{property.python_name}}))
164165
{% endif %}
165166
{% endif %}
167+
166168
{% endfor %}
167169

168170
{{ _prepare_field_dict(True) | indent(8) }}

openapi_python_client/templates/property_templates/file_property.py.jinja

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ if not isinstance({{ source }}, Unset):
2525
{% macro transform_multipart(property, source, destination) %}
2626
{% if property.required %}
2727
{{ destination }} = {{ source }}.to_tuple()
28-
{% else %}
28+
{%- else %}
2929
{{ destination }}: {{ property.get_type_string(json=True) }} = UNSET
3030
if not isinstance({{ source }}, Unset):
3131
{{ destination }} = {{ source }}.to_tuple()
32-
{% endif %}
32+
{%- endif -%}
3333
{% endmacro %}

openapi_python_client/templates/property_templates/float_property.py.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ str({{ source }})
77
{{ destination }} = {{source}} if isinstance({{source}}, Unset) else (None, str({{source}}).encode(), "text/plain")
88
{% else %}
99
{{ destination }} = (None, str({{ source }}).encode(), "text/plain")
10-
{% endif %}
10+
{%- endif -%}
1111
{% endmacro %}

openapi_python_client/templates/property_templates/union_property.py.jinja

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ if isinstance({{ source }}, {{ inner_property.get_instance_type_string() }}):
8686
{% macro transform_multipart(property, source, destination) %}
8787
{% set ns = namespace(has_if = false) %}
8888
{{ destination }}: {{ property.get_type_string(json=False, multipart=True) }}
89-
9089
{% if not property.required %}
9190
if isinstance({{ source }}, Unset):
9291
{{ destination }} = UNSET
@@ -97,11 +96,13 @@ if isinstance({{ source }}, Unset):
9796
{{ instance_check(inner_property, source) }}
9897
{% set ns.has_if = true %}
9998
{% elif not loop.last %}
99+
100100
el{{ instance_check(inner_property, source) }}
101101
{% else %}
102+
102103
else:
103104
{% endif %}
104105
{% import "property_templates/" + inner_property.template as inner_template %}
105106
{{ inner_template.transform_multipart(inner_property, source, destination) | indent(4) | trim }}
106-
{% endfor %}
107+
{%- endfor -%}
107108
{% endmacro %}

0 commit comments

Comments
 (0)