diff --git a/end_to_end_tests/baseline_openapi_3.0.json b/end_to_end_tests/baseline_openapi_3.0.json index eec3e39ac..40a91f628 100644 --- a/end_to_end_tests/baseline_openapi_3.0.json +++ b/end_to_end_tests/baseline_openapi_3.0.json @@ -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": [ diff --git a/end_to_end_tests/baseline_openapi_3.1.yaml b/end_to_end_tests/baseline_openapi_3.1.yaml index caddda8eb..ff68931bf 100644 --- a/end_to_end_tests/baseline_openapi_3.1.yaml +++ b/end_to_end_tests/baseline_openapi_3.1.yaml @@ -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": [ diff --git a/end_to_end_tests/custom-templates-golden-record/my_test_api_client/api/tests/__init__.py b/end_to_end_tests/custom-templates-golden-record/my_test_api_client/api/tests/__init__.py index 1b91acc98..821489ab4 100644 --- a/end_to_end_tests/custom-templates-golden-record/my_test_api_client/api/tests/__init__.py +++ b/end_to_end_tests/custom-templates-golden-record/my_test_api_client/api/tests/__init__.py @@ -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, ) @@ -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: """ diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/__init__.py b/end_to_end_tests/golden-record/my_test_api_client/models/__init__.py index f354c31c7..cae3ca303 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/__init__.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/__init__.py @@ -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__ = ( @@ -167,5 +168,6 @@ "PostResponsesUnionsSimpleBeforeComplexResponse200AType1", "TestInlineObjectsBody", "TestInlineObjectsResponse200", + "UploadArrayOfFilesInObjectTestsUploadPostBody", "ValidationError", ) diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/a_model.py b/end_to_end_tests/golden-record/my_test_api_client/models/a_model.py index 5a9ba85ae..db3c56629 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/a_model.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/a_model.py @@ -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, diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/body_upload_file_tests_upload_post.py b/end_to_end_tests/golden-record/my_test_api_client/models/body_upload_file_tests_upload_post.py index f5db27451..91fe12617 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/body_upload_file_tests_upload_post.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/body_upload_file_tests_upload_post.py @@ -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, @@ -167,13 +168,17 @@ 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): @@ -181,30 +186,41 @@ def to_multipart(self) -> dict[str, Any]: 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): @@ -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]] @@ -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: diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/http_validation_error.py b/end_to_end_tests/golden-record/my_test_api_client/models/http_validation_error.py index cfe0a9a0c..43009994a 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/http_validation_error.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/http_validation_error.py @@ -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 diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_additional_properties_inlined.py b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_additional_properties_inlined.py index bcdbf868c..bb70f94a8 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_additional_properties_inlined.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_additional_properties_inlined.py @@ -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 diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py index d704baada..aa3019f97 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py @@ -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 diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property_inlined.py b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property_inlined.py index 8011b7707..e2ebb7acd 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property_inlined.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property_inlined.py @@ -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 diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/post_bodies_multiple_files_body.py b/end_to_end_tests/golden-record/my_test_api_client/models/post_bodies_multiple_files_body.py index 5f4aefccc..756eec0f1 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/post_bodies_multiple_files_body.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/post_bodies_multiple_files_body.py @@ -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: diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/test_inline_objects_body.py b/end_to_end_tests/golden-record/my_test_api_client/models/test_inline_objects_body.py index e03ada2ef..66b4b3dcc 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/test_inline_objects_body.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/test_inline_objects_body.py @@ -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 diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/test_inline_objects_response_200.py b/end_to_end_tests/golden-record/my_test_api_client/models/test_inline_objects_response_200.py index 02d7888ea..37c3005c2 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/test_inline_objects_response_200.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/test_inline_objects_response_200.py @@ -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 diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/validation_error.py b/end_to_end_tests/golden-record/my_test_api_client/models/validation_error.py index 3c8fc9d04..613e44d4e 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/validation_error.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/validation_error.py @@ -27,6 +27,7 @@ def to_dict(self) -> dict[str, Any]: type_ = self.type_ field_dict: dict[str, Any] = {} + field_dict.update( { "loc": loc, diff --git a/end_to_end_tests/literal-enums-golden-record/my_enum_api_client/models/a_model.py b/end_to_end_tests/literal-enums-golden-record/my_enum_api_client/models/a_model.py index 881286e74..5c3508cf5 100644 --- a/end_to_end_tests/literal-enums-golden-record/my_enum_api_client/models/a_model.py +++ b/end_to_end_tests/literal-enums-golden-record/my_enum_api_client/models/a_model.py @@ -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, diff --git a/end_to_end_tests/literal-enums-golden-record/my_enum_api_client/models/post_user_list_body.py b/end_to_end_tests/literal-enums-golden-record/my_enum_api_client/models/post_user_list_body.py index bfddb8c02..8b0788c5e 100644 --- a/end_to_end_tests/literal-enums-golden-record/my_enum_api_client/models/post_user_list_body.py +++ b/end_to_end_tests/literal-enums-golden-record/my_enum_api_client/models/post_user_list_body.py @@ -94,35 +94,27 @@ def to_dict(self) -> dict[str, Any]: return field_dict - def to_multipart(self) -> dict[str, Any]: - an_enum_value: Union[Unset, tuple[None, bytes, str]] = UNSET - if not isinstance(self.an_enum_value, Unset): - _temp_an_enum_value = [] - for an_enum_value_item_data in self.an_enum_value: - an_enum_value_item: str = an_enum_value_item_data - _temp_an_enum_value.append(an_enum_value_item) - an_enum_value = (None, json.dumps(_temp_an_enum_value).encode(), "application/json") + def to_multipart(self) -> list[tuple[str, Any]]: + field_list: list[tuple[str, Any]] = [] + for an_enum_value_element in self.an_enum_value or []: + an_enum_value_item = (None, str(an_enum_value_element).encode(), "text/plain") - an_enum_value_with_null: Union[Unset, tuple[None, bytes, str]] = UNSET - if not isinstance(self.an_enum_value_with_null, Unset): - _temp_an_enum_value_with_null = [] - for an_enum_value_with_null_item_data in self.an_enum_value_with_null: - an_enum_value_with_null_item: Union[None, str] - if isinstance(an_enum_value_with_null_item_data, str): - an_enum_value_with_null_item = an_enum_value_with_null_item_data - else: - an_enum_value_with_null_item = an_enum_value_with_null_item_data - _temp_an_enum_value_with_null.append(an_enum_value_with_null_item) - an_enum_value_with_null = (None, json.dumps(_temp_an_enum_value_with_null).encode(), "application/json") + field_list.append(("an_enum_value", an_enum_value_item)) - an_enum_value_with_only_null: Union[Unset, tuple[None, bytes, str]] = UNSET - if not isinstance(self.an_enum_value_with_only_null, Unset): - _temp_an_enum_value_with_only_null = self.an_enum_value_with_only_null - an_enum_value_with_only_null = ( - None, - json.dumps(_temp_an_enum_value_with_only_null).encode(), - "application/json", - ) + for an_enum_value_with_null_element in self.an_enum_value_with_null or []: + an_enum_value_with_null_item: tuple[None, bytes, str] + + if an_enum_value_with_null_element is None: + an_enum_value_with_null_item = (None, str(an_enum_value_with_null_element).encode(), "text/plain") + else: + an_enum_value_with_null_item = (None, str(an_enum_value_with_null_element).encode(), "text/plain") + + field_list.append(("an_enum_value_with_null", an_enum_value_with_null_item)) + + for an_enum_value_with_only_null_element in self.an_enum_value_with_only_null or []: + an_enum_value_with_only_null_item = (None, str(an_enum_value_with_only_null_element).encode(), "text/plain") + + field_list.append(("an_enum_value_with_only_null", an_enum_value_with_only_null_item)) an_allof_enum_with_overridden_default: Union[Unset, tuple[None, bytes, str]] = UNSET if not isinstance(self.an_allof_enum_with_overridden_default, Unset): @@ -132,41 +124,30 @@ def to_multipart(self) -> dict[str, Any]: "text/plain", ) + if an_allof_enum_with_overridden_default is not UNSET: + field_list.append(("an_allof_enum_with_overridden_default", an_allof_enum_with_overridden_default)) an_optional_allof_enum: Union[Unset, tuple[None, bytes, str]] = UNSET if not isinstance(self.an_optional_allof_enum, Unset): an_optional_allof_enum = (None, str(self.an_optional_allof_enum).encode(), "text/plain") - nested_list_of_enums: Union[Unset, tuple[None, bytes, str]] = UNSET - if not isinstance(self.nested_list_of_enums, Unset): - _temp_nested_list_of_enums = [] - for nested_list_of_enums_item_data in self.nested_list_of_enums: - nested_list_of_enums_item = [] - for nested_list_of_enums_item_item_data in nested_list_of_enums_item_data: - nested_list_of_enums_item_item: str = nested_list_of_enums_item_item_data - nested_list_of_enums_item.append(nested_list_of_enums_item_item) + if an_optional_allof_enum is not UNSET: + field_list.append(("an_optional_allof_enum", an_optional_allof_enum)) + for nested_list_of_enums_element in self.nested_list_of_enums or []: + _temp_nested_list_of_enums_item = [] + for nested_list_of_enums_item_item_data in nested_list_of_enums_element: + nested_list_of_enums_item_item: str = nested_list_of_enums_item_item_data + _temp_nested_list_of_enums_item.append(nested_list_of_enums_item_item) + nested_list_of_enums_item = (None, json.dumps(_temp_nested_list_of_enums_item).encode(), "application/json") - _temp_nested_list_of_enums.append(nested_list_of_enums_item) - nested_list_of_enums = (None, json.dumps(_temp_nested_list_of_enums).encode(), "application/json") + field_list.append(("nested_list_of_enums", nested_list_of_enums_item)) 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 an_enum_value is not UNSET: - field_dict["an_enum_value"] = an_enum_value - if an_enum_value_with_null is not UNSET: - field_dict["an_enum_value_with_null"] = an_enum_value_with_null - if an_enum_value_with_only_null is not UNSET: - field_dict["an_enum_value_with_only_null"] = an_enum_value_with_only_null - if an_allof_enum_with_overridden_default is not UNSET: - field_dict["an_allof_enum_with_overridden_default"] = an_allof_enum_with_overridden_default - if an_optional_allof_enum is not UNSET: - field_dict["an_optional_allof_enum"] = an_optional_allof_enum - if nested_list_of_enums is not UNSET: - field_dict["nested_list_of_enums"] = nested_list_of_enums + 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: diff --git a/integration-tests/integration_tests/models/post_body_multipart_body.py b/integration-tests/integration_tests/models/post_body_multipart_body.py index f2100a10e..912137295 100644 --- a/integration-tests/integration_tests/models/post_body_multipart_body.py +++ b/integration-tests/integration_tests/models/post_body_multipart_body.py @@ -45,31 +45,30 @@ 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_string = (None, str(self.a_string).encode(), "text/plain") + field_list.append(("a_string", a_string)) file = self.file.to_tuple() + field_list.append(("file", file)) description = ( self.description if isinstance(self.description, Unset) else (None, str(self.description).encode(), "text/plain") ) + if description is not UNSET: + field_list.append(("description", description)) + 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( - { - "a_string": a_string, - "file": file, - } - ) - if description is not UNSET: - field_dict["description"] = description + 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: diff --git a/integration-tests/pyproject.toml b/integration-tests/pyproject.toml index 536a55b32..6052a3d70 100644 --- a/integration-tests/pyproject.toml +++ b/integration-tests/pyproject.toml @@ -14,22 +14,12 @@ dependencies = [ [tool.pdm] distribution = true -[tool.pdm.dev-dependencies] -dev = [ - "pytest", - "mypy", - "pytest-asyncio>=0.23.5", -] - [build-system] requires = ["pdm-backend"] build-backend = "pdm.backend" - + [tool.ruff] line-length = 120 [tool.ruff.lint] -select = ["F", "I"] - -[tool.mypy] -# Just to get mypy to _not_ look at the parent directory's config \ No newline at end of file +select = ["F", "I", "UP"] diff --git a/openapi_python_client/templates/model.py.jinja b/openapi_python_client/templates/model.py.jinja index eed2ea3a0..b2e082b6a 100644 --- a/openapi_python_client/templates/model.py.jinja +++ b/openapi_python_client/templates/model.py.jinja @@ -82,19 +82,19 @@ class {{ class_name }}: additional_properties: dict[str, {{ additional_property_type }}] = _attrs_field(init=False, factory=dict) {% endif %} -{% macro _to_dict(multipart=False) %} -{% for property in model.required_properties + model.optional_properties %} +{% macro _transform_property(property, content, multipart=False) %} {% import "property_templates/" + property.template as prop_template %} {% if multipart %} -{{ prop_template.transform_multipart(property, "self." + property.python_name, property.python_name) }} +{{ prop_template.transform_multipart(property, content, property.python_name) }} {% elif prop_template.transform %} -{{ prop_template.transform(property=property, source="self." + property.python_name, destination=property.python_name) }} +{{ prop_template.transform(property=property, source=content, destination=property.python_name) }} {% else %} -{{ property.python_name }} = self.{{ property.python_name }} +{{ property.python_name }} = {{ content }} {% endif %} -{% endfor %} +{% endmacro %} +{% macro _prepare_field_dict(multipart=False) %} field_dict: dict[str, Any] = {} {% if model.additional_properties %} {% import "property_templates/" + model.additional_properties.template as prop_template %} @@ -106,8 +106,16 @@ for prop_name, prop in self.additional_properties.items(): {{ prop_template.transform(model.additional_properties, "prop", "field_dict[prop_name]", declare_type=false) | indent(4) }} {% else %} field_dict.update(self.additional_properties) -{% endif %} -{% endif %} +{%- endif -%} +{%- endif -%} +{% endmacro %} + +{% macro _to_dict() %} +{% for property in model.required_properties + model.optional_properties -%} +{{ _transform_property(property, "self." + property.python_name) }} +{% endfor %} + +{{ _prepare_field_dict() }} {% if model.required_properties | length > 0 or model.optional_properties | length > 0 %} field_dict.update({ {% for property in model.required_properties + model.optional_properties %} @@ -134,8 +142,35 @@ return field_dict {{ _to_dict() | indent(8) }} {% if model.is_multipart_body %} - def to_multipart(self) -> dict[str, Any]: - {{ _to_dict(multipart=True) | indent(8) }} + def to_multipart(self) -> list[tuple[str, Any]]: + field_list: list[tuple[str, Any]] = [] + {% for property in model.required_properties + model.optional_properties %} + {% if property.__class__.__name__ == 'ListProperty' %} + {% if not property.required %} + for {{ property.python_name }}_element in self.{{ property.python_name }} or []: + {% else %} + for {{ property.python_name }}_element in self.{{ property.python_name }}: + {% endif %} + {{ _transform_property(property.inner_property, property.python_name + "_element", True) | indent(12) }} + field_list.append(("{{ property.python_name }}", {{property.inner_property.python_name}})) + + {% else %} + {{ _transform_property(property, "self." + property.python_name, True) | indent(8) }} + {% if not property.required %} + if {{ property.python_name }} is not UNSET: + field_list.append(("{{ property.python_name }}", {{property.python_name}})) + {% else %} + field_list.append(("{{ property.python_name }}", {{property.python_name}})) + {% endif %} + {% endif %} + {% endfor %} + + {{ _prepare_field_dict(True) | indent(8) }} + + field_list += list(field_dict.items()) + + return field_list + {% endif %} @classmethod diff --git a/openapi_python_client/templates/property_templates/union_property.py.jinja b/openapi_python_client/templates/property_templates/union_property.py.jinja index dbf7ee9dc..1881d3eae 100644 --- a/openapi_python_client/templates/property_templates/union_property.py.jinja +++ b/openapi_python_client/templates/property_templates/union_property.py.jinja @@ -75,6 +75,14 @@ else: {% endmacro %} +{% macro instance_check(inner_property, source) %} +{% if inner_property.get_instance_type_string() == "None" %} +if {{ source }} is None: +{% else %} +if isinstance({{ source }}, {{ inner_property.get_instance_type_string() }}): +{% endif %} +{% endmacro %} + {% macro transform_multipart(property, source, destination) %} {% set ns = namespace(has_if = false) %} {{ destination }}: {{ property.get_type_string(json=False, multipart=True) }} @@ -86,10 +94,10 @@ if isinstance({{ source }}, Unset): {% endif %} {% for inner_property in property.inner_properties %} {% if not ns.has_if %} -if isinstance({{ source }}, {{ inner_property.get_instance_type_string() }}): +{{ instance_check(inner_property, source) }} {% set ns.has_if = true %} {% elif not loop.last %} -elif isinstance({{ source }}, {{ inner_property.get_instance_type_string() }}): +el{{ instance_check(inner_property, source) }} {% else %} else: {% endif %}