Skip to content

fix: Fix multipart body file array #938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions end_to_end_tests/baseline_openapi_3.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,55 @@
}
}
},
"/tests/upload/multiple-files-in-object": {
"post": {
"tags": [
"tests"
],
"summary": "Array of files in object",
"description": "Upload an array of files as part of an object",
"operationId": "upload_array_of_files_in_object_tests_upload_post",
"parameters": [],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type" : "object",
"files" : {
"type" : "array",
"items" : {
"type" : "string",
"description" : "attachments content",
"format" : "binary"
}
}
}
}
},
"required": true
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {}
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
}
}
}
}
},
"/tests/json_body": {
"post": {
"tags": [
Expand Down
49 changes: 49 additions & 0 deletions end_to_end_tests/baseline_openapi_3.1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,55 @@ info:
}
}
},
"/tests/upload/multiple-files-in-object": {
"post": {
"tags": [
"tests"
],
"summary": "Array of files in object",
"description": "Upload an array of files as part of an object",
"operationId": "upload_array_of_files_in_object_tests_upload_post",
"parameters": [ ],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"files": {
"type": "array",
"items": {
"type": "string",
"description": "attachments content",
"format": "binary"
}
}
}
}
},
"required": true
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": { }
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
}
}
}
}
},
"/tests/json_body": {
"post": {
"tags": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
test_inline_objects,
token_with_cookie_auth_token_with_cookie_get,
unsupported_content_tests_unsupported_content_get,
upload_array_of_files_in_object_tests_upload_post,
upload_file_tests_upload_post,
upload_multiple_files_tests_upload_post,
)
Expand Down Expand Up @@ -89,6 +90,13 @@ def upload_multiple_files_tests_upload_post(cls) -> types.ModuleType:
"""
return upload_multiple_files_tests_upload_post

@classmethod
def upload_array_of_files_in_object_tests_upload_post(cls) -> types.ModuleType:
"""
Upload an array of files as part of an object
"""
return upload_array_of_files_in_object_tests_upload_post

@classmethod
def json_body_tests_json_body_post(cls) -> types.ModuleType:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
)
from .test_inline_objects_body import TestInlineObjectsBody
from .test_inline_objects_response_200 import TestInlineObjectsResponse200
from .upload_array_of_files_in_object_tests_upload_post_body import UploadArrayOfFilesInObjectTestsUploadPostBody
from .validation_error import ValidationError

__all__ = (
Expand Down Expand Up @@ -167,5 +168,6 @@
"PostResponsesUnionsSimpleBeforeComplexResponse200AType1",
"TestInlineObjectsBody",
"TestInlineObjectsResponse200",
"UploadArrayOfFilesInObjectTestsUploadPostBody",
"ValidationError",
)
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ def to_dict(self) -> dict[str, Any]:
not_required_nullable_model = self.not_required_nullable_model

field_dict: dict[str, Any] = {}

field_dict.update(
{
"an_enum_value": an_enum_value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def to_dict(self) -> dict[str, Any]:
field_dict: dict[str, Any] = {}
for prop_name, prop in self.additional_properties.items():
field_dict[prop_name] = prop.to_dict()

field_dict.update(
{
"some_file": some_file,
Expand Down Expand Up @@ -167,44 +168,59 @@ def to_dict(self) -> dict[str, Any]:

return field_dict

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

field_list.append(("some_file", some_file))
some_required_number = (None, str(self.some_required_number).encode(), "text/plain")

field_list.append(("some_required_number", some_required_number))
some_object = (None, json.dumps(self.some_object.to_dict()).encode(), "application/json")

field_list.append(("some_object", some_object))
some_nullable_object: tuple[None, bytes, str]

if isinstance(self.some_nullable_object, BodyUploadFileTestsUploadPostSomeNullableObject):
some_nullable_object = (None, json.dumps(self.some_nullable_object.to_dict()).encode(), "application/json")
else:
some_nullable_object = (None, str(self.some_nullable_object).encode(), "text/plain")

field_list.append(("some_nullable_object", some_nullable_object))
some_optional_file: Union[Unset, FileJsonType] = UNSET
if not isinstance(self.some_optional_file, Unset):
some_optional_file = self.some_optional_file.to_tuple()

if some_optional_file is not UNSET:
field_list.append(("some_optional_file", some_optional_file))
some_string = (
self.some_string
if isinstance(self.some_string, Unset)
else (None, str(self.some_string).encode(), "text/plain")
)

if some_string is not UNSET:
field_list.append(("some_string", some_string))
a_datetime: Union[Unset, bytes] = UNSET
if not isinstance(self.a_datetime, Unset):
a_datetime = self.a_datetime.isoformat().encode()

if a_datetime is not UNSET:
field_list.append(("a_datetime", a_datetime))
a_date: Union[Unset, bytes] = UNSET
if not isinstance(self.a_date, Unset):
a_date = self.a_date.isoformat().encode()

if a_date is not UNSET:
field_list.append(("a_date", a_date))
some_number = (
self.some_number
if isinstance(self.some_number, Unset)
else (None, str(self.some_number).encode(), "text/plain")
)

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

if isinstance(self.some_nullable_number, Unset):
Expand All @@ -214,14 +230,17 @@ def to_multipart(self) -> dict[str, Any]:
else:
some_nullable_number = (None, str(self.some_nullable_number).encode(), "text/plain")

some_int_array: Union[Unset, tuple[None, bytes, str]] = UNSET
if not isinstance(self.some_int_array, Unset):
_temp_some_int_array = []
for some_int_array_item_data in self.some_int_array:
some_int_array_item: Union[None, int]
some_int_array_item = some_int_array_item_data
_temp_some_int_array.append(some_int_array_item)
some_int_array = (None, json.dumps(_temp_some_int_array).encode(), "application/json")
if some_nullable_number is not UNSET:
field_list.append(("some_nullable_number", some_nullable_number))
for some_int_array_element in self.some_int_array or []:
some_int_array_item: tuple[None, bytes, str]

if isinstance(some_int_array_element, int):
some_int_array_item = (None, str(some_int_array_element).encode(), "text/plain")
else:
some_int_array_item = (None, str(some_int_array_element).encode(), "text/plain")

field_list.append(("some_int_array", some_int_array_item))

some_array: Union[Unset, tuple[None, bytes, str]]

Expand All @@ -236,47 +255,28 @@ def to_multipart(self) -> dict[str, Any]:
else:
some_array = (None, str(self.some_array).encode(), "text/plain")

if some_array is not UNSET:
field_list.append(("some_array", some_array))
some_optional_object: Union[Unset, tuple[None, bytes, str]] = UNSET
if not isinstance(self.some_optional_object, Unset):
some_optional_object = (None, json.dumps(self.some_optional_object.to_dict()).encode(), "application/json")

if some_optional_object is not UNSET:
field_list.append(("some_optional_object", some_optional_object))
some_enum: Union[Unset, tuple[None, bytes, str]] = UNSET
if not isinstance(self.some_enum, Unset):
some_enum = (None, str(self.some_enum.value).encode(), "text/plain")

if some_enum is not UNSET:
field_list.append(("some_enum", some_enum))

field_dict: dict[str, Any] = {}
for prop_name, prop in self.additional_properties.items():
field_dict[prop_name] = (None, json.dumps(prop.to_dict()).encode(), "application/json")
field_dict.update(
{
"some_file": some_file,
"some_required_number": some_required_number,
"some_object": some_object,
"some_nullable_object": some_nullable_object,
}
)
if some_optional_file is not UNSET:
field_dict["some_optional_file"] = some_optional_file
if some_string is not UNSET:
field_dict["some_string"] = some_string
if a_datetime is not UNSET:
field_dict["a_datetime"] = a_datetime
if a_date is not UNSET:
field_dict["a_date"] = a_date
if some_number is not UNSET:
field_dict["some_number"] = some_number
if some_nullable_number is not UNSET:
field_dict["some_nullable_number"] = some_nullable_number
if some_int_array is not UNSET:
field_dict["some_int_array"] = some_int_array
if some_array is not UNSET:
field_dict["some_array"] = some_array
if some_optional_object is not UNSET:
field_dict["some_optional_object"] = some_optional_object
if some_enum is not UNSET:
field_dict["some_enum"] = some_enum

return field_dict
field_list += list(field_dict.items())

return field_list

@classmethod
def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def to_dict(self) -> dict[str, Any]:
detail.append(detail_item)

field_dict: dict[str, Any] = {}

field_dict.update({})
if detail is not UNSET:
field_dict["detail"] = detail
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def to_dict(self) -> dict[str, Any]:
field_dict: dict[str, Any] = {}
for prop_name, prop in self.additional_properties.items():
field_dict[prop_name] = prop.to_dict()

field_dict.update({})
if a_number is not UNSET:
field_dict["a_number"] = a_number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def to_dict(self) -> dict[str, Any]:
a_property = self.a_property.value

field_dict: dict[str, Any] = {}

field_dict.update({})
if a_property is not UNSET:
field_dict["a_property"] = a_property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def to_dict(self) -> dict[str, Any]:
fruit = self.fruit.to_dict()

field_dict: dict[str, Any] = {}

field_dict.update({})
if fruit is not UNSET:
field_dict["fruit"] = fruit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,20 @@ def to_dict(self) -> dict[str, Any]:

return field_dict

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

if a is not UNSET:
field_list.append(("a", a))

field_dict: dict[str, Any] = {}
for prop_name, prop in self.additional_properties.items():
field_dict[prop_name] = (None, str(prop).encode(), "text/plain")

field_dict.update({})
if a is not UNSET:
field_dict["a"] = a
field_list += list(field_dict.items())

return field_dict
return field_list

@classmethod
def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def to_dict(self) -> dict[str, Any]:
a_property = self.a_property

field_dict: dict[str, Any] = {}

field_dict.update({})
if a_property is not UNSET:
field_dict["a_property"] = a_property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def to_dict(self) -> dict[str, Any]:
a_property = self.a_property

field_dict: dict[str, Any] = {}

field_dict.update({})
if a_property is not UNSET:
field_dict["a_property"] = a_property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def to_dict(self) -> dict[str, Any]:
type_ = self.type_

field_dict: dict[str, Any] = {}

field_dict.update(
{
"loc": loc,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def to_dict(self) -> dict[str, Any]:
nested_list_of_enums.append(nested_list_of_enums_item)

field_dict: dict[str, Any] = {}

field_dict.update(
{
"an_enum_value": an_enum_value,
Expand Down
Loading