Skip to content

Commit 72cb706

Browse files
authored
fix: Allow passing data with files in multipart. (Fixes #351) (#355)
* fix: Allow passing data with files in multipart. (Fixes #351) * ci: Remove hard-to-use unit test
1 parent 045089c commit 72cb706

File tree

6 files changed

+41
-195
lines changed

6 files changed

+41
-195
lines changed

end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_file_tests_upload_post.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from ...client import Client
66
from ...models.body_upload_file_tests_upload_post import BodyUploadFileTestsUploadPost
77
from ...models.http_validation_error import HTTPValidationError
8-
from ...types import UNSET, Response, Unset
8+
from ...types import UNSET, File, Response, Unset
99

1010

1111
def _get_kwargs(
@@ -22,12 +22,21 @@ def _get_kwargs(
2222
if keep_alive is not UNSET:
2323
headers["keep-alive"] = keep_alive
2424

25+
files = {}
26+
data = {}
27+
for key, value in multipart_data.to_dict().items():
28+
if isinstance(value, File):
29+
files[key] = value
30+
else:
31+
data[key] = value
32+
2533
return {
2634
"url": url,
2735
"headers": headers,
2836
"cookies": cookies,
2937
"timeout": client.get_timeout(),
30-
"files": multipart_data.to_dict(),
38+
"files": files,
39+
"data": data,
3140
}
3241

3342

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from io import BytesIO
2-
from typing import Any, Dict, Type, TypeVar
2+
from typing import Any, Dict, Type, TypeVar, Union
33

44
import attr
55

6-
from ..types import File
6+
from ..types import UNSET, File, Unset
77

88
T = TypeVar("T", bound="BodyUploadFileTestsUploadPost")
99

@@ -13,16 +13,21 @@ class BodyUploadFileTestsUploadPost:
1313
""" """
1414

1515
some_file: File
16+
some_string: Union[Unset, str] = "some_default_string"
1617

1718
def to_dict(self) -> Dict[str, Any]:
1819
some_file = self.some_file.to_tuple()
1920

21+
some_string = self.some_string
22+
2023
field_dict: Dict[str, Any] = {}
2124
field_dict.update(
2225
{
2326
"some_file": some_file,
2427
}
2528
)
29+
if some_string is not UNSET:
30+
field_dict["some_string"] = some_string
2631

2732
return field_dict
2833

@@ -31,8 +36,11 @@ def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
3136
d = src_dict.copy()
3237
some_file = File(payload=BytesIO(d.pop("some_file")))
3338

39+
some_string = d.pop("some_string", UNSET)
40+
3441
body_upload_file_tests_upload_post = cls(
3542
some_file=some_file,
43+
some_string=some_string,
3644
)
3745

3846
return body_upload_file_tests_upload_post

end_to_end_tests/openapi.json

+5
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,11 @@
923923
"title": "Some File",
924924
"type": "string",
925925
"format": "binary"
926+
},
927+
"some_string": {
928+
"title": "Some String",
929+
"type": "string",
930+
"default": "some_default_string"
926931
}
927932
},
928933
"additionalProperties": false

openapi_python_client/templates/endpoint_module.py.jinja

+15-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import httpx
44
from attr import asdict
55

66
from ...client import AuthenticatedClient, Client
7-
from ...types import Response, UNSET
7+
from ...types import Response, UNSET{% if endpoint.multipart_body_reference %}, File {% endif %}
88

99
{% for relative in endpoint.relative_imports %}
1010
{{ relative }}
@@ -36,18 +36,27 @@ def _get_kwargs(
3636

3737
{{ json_body(endpoint) | indent(4) }}
3838

39+
{% if endpoint.multipart_body_reference %}
40+
files = {}
41+
data = {}
42+
for key, value in multipart_data.to_dict().items():
43+
if isinstance(value, File):
44+
files[key] = value
45+
else:
46+
data[key] = value
47+
{% endif %}
48+
3949
return {
4050
"url": url,
4151
"headers": headers,
4252
"cookies": cookies,
4353
"timeout": client.get_timeout(),
4454
{% if endpoint.form_body_reference %}
4555
"data": asdict(form_data),
46-
{% endif %}
47-
{% if endpoint.multipart_body_reference %}
48-
"files": multipart_data.to_dict(),
49-
{% endif %}
50-
{% if endpoint.json_body %}
56+
{% elif endpoint.multipart_body_reference %}
57+
"files": files,
58+
"data": data,
59+
{% elif endpoint.json_body %}
5160
"json": {{ "json_" + endpoint.json_body.python_name }},
5261
{% endif %}
5362
{% if endpoint.query_parameters %}

tests/test_templates/endpoint_module.py

-129
This file was deleted.

tests/test_templates/test_endpoint_module.py

-56
This file was deleted.

0 commit comments

Comments
 (0)