Skip to content

Commit

Permalink
feat(python): handle multiple file parameters (#19329)
Browse files Browse the repository at this point in the history
* test: fix broken python test

* fix: handle multiple file parameters

Previously an array of files was not handled correctly, despite the type
annotation implying it was.

* feat: handle filename,filedata tuples in file param

This allows for users to pass filenames with their data in file params,
which is useful for multipart formdata requests. Without this, the list
of files added in the previous commit would have the same filename for
all files (the parameter name).
  • Loading branch information
roryschadler authored Aug 21, 2024
1 parent c2472b0 commit cc98333
Show file tree
Hide file tree
Showing 38 changed files with 673 additions and 235 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1951,9 +1951,22 @@ private PythonType binaryType(IJsonSchemaValidationProperties cp) {
}

moduleImports.add("typing", "Union");

PythonType pt = new PythonType("Union");
pt.addTypeParam(bytest);
pt.addTypeParam(strt);

if (cp.getIsBinary()) {
moduleImports.add("typing", "Tuple");

PythonType tt = new PythonType("Tuple");
// this string is a filename, not a validated value
tt.addTypeParam(new PythonType("str"));
tt.addTypeParam(bytest);

pt.addTypeParam(tt);
}

return pt;
} else {
// same as above which has validation
Expand All @@ -1964,6 +1977,17 @@ private PythonType binaryType(IJsonSchemaValidationProperties cp) {
PythonType pt = new PythonType("Union");
pt.addTypeParam(new PythonType("StrictBytes"));
pt.addTypeParam(new PythonType("StrictStr"));

if (cp.getIsBinary()) {
moduleImports.add("typing", "Tuple");

PythonType tt = new PythonType("Tuple");
tt.addTypeParam(new PythonType("StrictStr"));
tt.addTypeParam(new PythonType("StrictBytes"));

pt.addTypeParam(tt);
}

return pt;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ class {{classname}}:
_query_params: List[Tuple[str, str]] = []
_header_params: Dict[str, Optional[str]] = _headers or {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, Union[str, bytes]] = {}
_files: Dict[
str, Union[str, bytes, List[str], List[bytes], List[Tuple[str, bytes]]]
] = {}
_body_params: Optional[bytes] = None

# process the path parameters
Expand Down Expand Up @@ -165,6 +167,9 @@ class {{classname}}:
if isinstance({{paramName}}, str):
with open({{paramName}}, "rb") as _fp:
_body_params = _fp.read()
elif isinstance({{paramName}}, tuple):
# drop the filename from the tuple
_body_params = {{paramName}}[1]
else:
_body_params = {{paramName}}
{{/isBinary}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,10 @@ class ApiClient:

return "&".join(["=".join(map(str, item)) for item in new_params])

def files_parameters(self, files: Dict[str, Union[str, bytes]]):
def files_parameters(
self,
files: Dict[str, Union[str, bytes, List[str], List[bytes], Tuple[str, bytes]]],
):
"""Builds form parameters.

:param files: File parameters.
Expand All @@ -558,6 +561,12 @@ class ApiClient:
elif isinstance(v, bytes):
filename = k
filedata = v
elif isinstance(v, tuple):
filename, filedata = v
elif isinstance(v, list):
for file_param in v:
params.extend(self.files_parameters({k: file_param}))
continue
else:
raise ValueError("Unsupported file value")
mimetype = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ def _test_auth_http_basic_serialize(
_query_params: List[Tuple[str, str]] = []
_header_params: Dict[str, Optional[str]] = _headers or {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, Union[str, bytes]] = {}
_files: Dict[
str, Union[str, bytes, List[str], List[bytes], List[Tuple[str, bytes]]]
] = {}
_body_params: Optional[bytes] = None

# process the path parameters
Expand Down Expand Up @@ -483,7 +485,9 @@ def _test_auth_http_bearer_serialize(
_query_params: List[Tuple[str, str]] = []
_header_params: Dict[str, Optional[str]] = _headers or {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, Union[str, bytes]] = {}
_files: Dict[
str, Union[str, bytes, List[str], List[bytes], List[Tuple[str, bytes]]]
] = {}
_body_params: Optional[bytes] = None

# process the path parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from typing_extensions import Annotated

from pydantic import Field, StrictBytes, StrictStr
from typing import Any, Dict, List, Optional, Union
from typing import Any, Dict, List, Optional, Tuple, Union
from typing_extensions import Annotated
from openapi_client.models.pet import Pet
from openapi_client.models.string_enum_ref import StringEnumRef
Expand Down Expand Up @@ -244,7 +244,9 @@ def _test_binary_gif_serialize(
_query_params: List[Tuple[str, str]] = []
_header_params: Dict[str, Optional[str]] = _headers or {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, Union[str, bytes]] = {}
_files: Dict[
str, Union[str, bytes, List[str], List[bytes], List[Tuple[str, bytes]]]
] = {}
_body_params: Optional[bytes] = None

# process the path parameters
Expand Down Expand Up @@ -288,7 +290,7 @@ def _test_binary_gif_serialize(
@validate_call
def test_body_application_octetstream_binary(
self,
body: Optional[Union[StrictBytes, StrictStr]] = None,
body: Optional[Union[StrictBytes, StrictStr, Tuple[StrictStr, StrictBytes]]] = None,
_request_timeout: Union[
None,
Annotated[StrictFloat, Field(gt=0)],
Expand Down Expand Up @@ -355,7 +357,7 @@ def test_body_application_octetstream_binary(
@validate_call
def test_body_application_octetstream_binary_with_http_info(
self,
body: Optional[Union[StrictBytes, StrictStr]] = None,
body: Optional[Union[StrictBytes, StrictStr, Tuple[StrictStr, StrictBytes]]] = None,
_request_timeout: Union[
None,
Annotated[StrictFloat, Field(gt=0)],
Expand Down Expand Up @@ -422,7 +424,7 @@ def test_body_application_octetstream_binary_with_http_info(
@validate_call
def test_body_application_octetstream_binary_without_preload_content(
self,
body: Optional[Union[StrictBytes, StrictStr]] = None,
body: Optional[Union[StrictBytes, StrictStr, Tuple[StrictStr, StrictBytes]]] = None,
_request_timeout: Union[
None,
Annotated[StrictFloat, Field(gt=0)],
Expand Down Expand Up @@ -500,7 +502,9 @@ def _test_body_application_octetstream_binary_serialize(
_query_params: List[Tuple[str, str]] = []
_header_params: Dict[str, Optional[str]] = _headers or {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, Union[str, bytes]] = {}
_files: Dict[
str, Union[str, bytes, List[str], List[bytes], List[Tuple[str, bytes]]]
] = {}
_body_params: Optional[bytes] = None

# process the path parameters
Expand All @@ -513,6 +517,9 @@ def _test_body_application_octetstream_binary_serialize(
if isinstance(body, str):
with open(body, "rb") as _fp:
_body_params = _fp.read()
elif isinstance(body, tuple):
# drop the filename from the tuple
_body_params = body[1]
else:
_body_params = body

Expand Down Expand Up @@ -564,7 +571,7 @@ def _test_body_application_octetstream_binary_serialize(
@validate_call
def test_body_multipart_formdata_array_of_binary(
self,
files: List[Union[StrictBytes, StrictStr]],
files: List[Union[StrictBytes, StrictStr, Tuple[StrictStr, StrictBytes]]],
_request_timeout: Union[
None,
Annotated[StrictFloat, Field(gt=0)],
Expand Down Expand Up @@ -631,7 +638,7 @@ def test_body_multipart_formdata_array_of_binary(
@validate_call
def test_body_multipart_formdata_array_of_binary_with_http_info(
self,
files: List[Union[StrictBytes, StrictStr]],
files: List[Union[StrictBytes, StrictStr, Tuple[StrictStr, StrictBytes]]],
_request_timeout: Union[
None,
Annotated[StrictFloat, Field(gt=0)],
Expand Down Expand Up @@ -698,7 +705,7 @@ def test_body_multipart_formdata_array_of_binary_with_http_info(
@validate_call
def test_body_multipart_formdata_array_of_binary_without_preload_content(
self,
files: List[Union[StrictBytes, StrictStr]],
files: List[Union[StrictBytes, StrictStr, Tuple[StrictStr, StrictBytes]]],
_request_timeout: Union[
None,
Annotated[StrictFloat, Field(gt=0)],
Expand Down Expand Up @@ -777,7 +784,9 @@ def _test_body_multipart_formdata_array_of_binary_serialize(
_query_params: List[Tuple[str, str]] = []
_header_params: Dict[str, Optional[str]] = _headers or {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, Union[str, bytes]] = {}
_files: Dict[
str, Union[str, bytes, List[str], List[bytes], List[Tuple[str, bytes]]]
] = {}
_body_params: Optional[bytes] = None

# process the path parameters
Expand Down Expand Up @@ -836,7 +845,7 @@ def _test_body_multipart_formdata_array_of_binary_serialize(
@validate_call
def test_body_multipart_formdata_single_binary(
self,
my_file: Optional[Union[StrictBytes, StrictStr]] = None,
my_file: Optional[Union[StrictBytes, StrictStr, Tuple[StrictStr, StrictBytes]]] = None,
_request_timeout: Union[
None,
Annotated[StrictFloat, Field(gt=0)],
Expand Down Expand Up @@ -903,7 +912,7 @@ def test_body_multipart_formdata_single_binary(
@validate_call
def test_body_multipart_formdata_single_binary_with_http_info(
self,
my_file: Optional[Union[StrictBytes, StrictStr]] = None,
my_file: Optional[Union[StrictBytes, StrictStr, Tuple[StrictStr, StrictBytes]]] = None,
_request_timeout: Union[
None,
Annotated[StrictFloat, Field(gt=0)],
Expand Down Expand Up @@ -970,7 +979,7 @@ def test_body_multipart_formdata_single_binary_with_http_info(
@validate_call
def test_body_multipart_formdata_single_binary_without_preload_content(
self,
my_file: Optional[Union[StrictBytes, StrictStr]] = None,
my_file: Optional[Union[StrictBytes, StrictStr, Tuple[StrictStr, StrictBytes]]] = None,
_request_timeout: Union[
None,
Annotated[StrictFloat, Field(gt=0)],
Expand Down Expand Up @@ -1048,7 +1057,9 @@ def _test_body_multipart_formdata_single_binary_serialize(
_query_params: List[Tuple[str, str]] = []
_header_params: Dict[str, Optional[str]] = _headers or {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, Union[str, bytes]] = {}
_files: Dict[
str, Union[str, bytes, List[str], List[bytes], List[Tuple[str, bytes]]]
] = {}
_body_params: Optional[bytes] = None

# process the path parameters
Expand Down Expand Up @@ -1319,7 +1330,9 @@ def _test_echo_body_all_of_pet_serialize(
_query_params: List[Tuple[str, str]] = []
_header_params: Dict[str, Optional[str]] = _headers or {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, Union[str, bytes]] = {}
_files: Dict[
str, Union[str, bytes, List[str], List[bytes], List[Tuple[str, bytes]]]
] = {}
_body_params: Optional[bytes] = None

# process the path parameters
Expand Down Expand Up @@ -1590,7 +1603,9 @@ def _test_echo_body_free_form_object_response_string_serialize(
_query_params: List[Tuple[str, str]] = []
_header_params: Dict[str, Optional[str]] = _headers or {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, Union[str, bytes]] = {}
_files: Dict[
str, Union[str, bytes, List[str], List[bytes], List[Tuple[str, bytes]]]
] = {}
_body_params: Optional[bytes] = None

# process the path parameters
Expand Down Expand Up @@ -1861,7 +1876,9 @@ def _test_echo_body_pet_serialize(
_query_params: List[Tuple[str, str]] = []
_header_params: Dict[str, Optional[str]] = _headers or {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, Union[str, bytes]] = {}
_files: Dict[
str, Union[str, bytes, List[str], List[bytes], List[Tuple[str, bytes]]]
] = {}
_body_params: Optional[bytes] = None

# process the path parameters
Expand Down Expand Up @@ -2132,7 +2149,9 @@ def _test_echo_body_pet_response_string_serialize(
_query_params: List[Tuple[str, str]] = []
_header_params: Dict[str, Optional[str]] = _headers or {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, Union[str, bytes]] = {}
_files: Dict[
str, Union[str, bytes, List[str], List[bytes], List[Tuple[str, bytes]]]
] = {}
_body_params: Optional[bytes] = None

# process the path parameters
Expand Down Expand Up @@ -2403,7 +2422,9 @@ def _test_echo_body_string_enum_serialize(
_query_params: List[Tuple[str, str]] = []
_header_params: Dict[str, Optional[str]] = _headers or {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, Union[str, bytes]] = {}
_files: Dict[
str, Union[str, bytes, List[str], List[bytes], List[Tuple[str, bytes]]]
] = {}
_body_params: Optional[bytes] = None

# process the path parameters
Expand Down Expand Up @@ -2674,7 +2695,9 @@ def _test_echo_body_tag_response_string_serialize(
_query_params: List[Tuple[str, str]] = []
_header_params: Dict[str, Optional[str]] = _headers or {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, Union[str, bytes]] = {}
_files: Dict[
str, Union[str, bytes, List[str], List[bytes], List[Tuple[str, bytes]]]
] = {}
_body_params: Optional[bytes] = None

# process the path parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,9 @@ def _test_form_integer_boolean_string_serialize(
_query_params: List[Tuple[str, str]] = []
_header_params: Dict[str, Optional[str]] = _headers or {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, Union[str, bytes]] = {}
_files: Dict[
str, Union[str, bytes, List[str], List[bytes], List[Tuple[str, bytes]]]
] = {}
_body_params: Optional[bytes] = None

# process the path parameters
Expand Down Expand Up @@ -555,7 +557,9 @@ def _test_form_object_multipart_serialize(
_query_params: List[Tuple[str, str]] = []
_header_params: Dict[str, Optional[str]] = _headers or {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, Union[str, bytes]] = {}
_files: Dict[
str, Union[str, bytes, List[str], List[bytes], List[Tuple[str, bytes]]]
] = {}
_body_params: Optional[bytes] = None

# process the path parameters
Expand Down Expand Up @@ -891,7 +895,9 @@ def _test_form_oneof_serialize(
_query_params: List[Tuple[str, str]] = []
_header_params: Dict[str, Optional[str]] = _headers or {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, Union[str, bytes]] = {}
_files: Dict[
str, Union[str, bytes, List[str], List[bytes], List[Tuple[str, bytes]]]
] = {}
_body_params: Optional[bytes] = None

# process the path parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ def _test_header_integer_boolean_string_enums_serialize(
_query_params: List[Tuple[str, str]] = []
_header_params: Dict[str, Optional[str]] = _headers or {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, Union[str, bytes]] = {}
_files: Dict[
str, Union[str, bytes, List[str], List[bytes], List[Tuple[str, bytes]]]
] = {}
_body_params: Optional[bytes] = None

# process the path parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,9 @@ def _tests_path_string_path_string_integer_path_integer_enum_nonref_string_path_
_query_params: List[Tuple[str, str]] = []
_header_params: Dict[str, Optional[str]] = _headers or {}
_form_params: List[Tuple[str, str]] = []
_files: Dict[str, Union[str, bytes]] = {}
_files: Dict[
str, Union[str, bytes, List[str], List[bytes], List[Tuple[str, bytes]]]
] = {}
_body_params: Optional[bytes] = None

# process the path parameters
Expand Down
Loading

0 comments on commit cc98333

Please sign in to comment.