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..5bc69e864 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,116 +168,88 @@ def to_dict(self) -> dict[str, Any]: return field_dict - def to_multipart(self) -> dict[str, Any]: - some_file = self.some_file.to_tuple() + def to_multipart(self) -> list[tuple[str, Any]]: + field_list: list[tuple[str, Any]] = [] - some_required_number = (None, str(self.some_required_number).encode(), "text/plain") + field_list.append(("some_file", self.some_file.to_tuple())) - some_object = (None, json.dumps(self.some_object.to_dict()).encode(), "application/json") + field_list.append(("some_required_number", (None, str(self.some_required_number).encode(), "text/plain"))) - some_nullable_object: tuple[None, bytes, str] + field_list.append(("some_object", (None, json.dumps(self.some_object.to_dict()).encode(), "application/json"))) if isinstance(self.some_nullable_object, BodyUploadFileTestsUploadPostSomeNullableObject): - some_nullable_object = (None, json.dumps(self.some_nullable_object.to_dict()).encode(), "application/json") + field_list.append( + ( + "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", (None, str(self.some_nullable_object).encode(), "text/plain"))) - some_optional_file: Union[Unset, FileJsonType] = UNSET if not isinstance(self.some_optional_file, Unset): - some_optional_file = self.some_optional_file.to_tuple() + field_list.append(("some_optional_file", self.some_optional_file.to_tuple())) - some_string = ( - self.some_string - if isinstance(self.some_string, Unset) - else (None, str(self.some_string).encode(), "text/plain") - ) + if not isinstance(self.some_string, Unset): + field_list.append(("some_string", (None, str(self.some_string).encode(), "text/plain"))) - a_datetime: Union[Unset, bytes] = UNSET if not isinstance(self.a_datetime, Unset): - a_datetime = self.a_datetime.isoformat().encode() + field_list.append(("a_datetime", self.a_datetime.isoformat().encode())) - a_date: Union[Unset, bytes] = UNSET if not isinstance(self.a_date, Unset): - a_date = self.a_date.isoformat().encode() + field_list.append(("a_date", self.a_date.isoformat().encode())) - some_number = ( - self.some_number - if isinstance(self.some_number, Unset) - else (None, str(self.some_number).encode(), "text/plain") - ) + if not isinstance(self.some_number, Unset): + field_list.append(("some_number", (None, str(self.some_number).encode(), "text/plain"))) - some_nullable_number: Union[Unset, tuple[None, bytes, str]] + if not isinstance(self.some_nullable_number, Unset): + if isinstance(self.some_nullable_number, float): + field_list.append( + ("some_nullable_number", (None, str(self.some_nullable_number).encode(), "text/plain")) + ) + else: + field_list.append( + ("some_nullable_number", (None, str(self.some_nullable_number).encode(), "text/plain")) + ) - if isinstance(self.some_nullable_number, Unset): - some_nullable_number = UNSET - elif isinstance(self.some_nullable_number, float): - some_nullable_number = (None, str(self.some_nullable_number).encode(), "text/plain") - 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") + for some_int_array_item_element in self.some_int_array: + if isinstance(some_int_array_item_element, int): + field_list.append( + ("some_int_array", (None, str(some_int_array_item_element).encode(), "text/plain")) + ) + else: + field_list.append( + ("some_int_array", (None, str(some_int_array_item_element).encode(), "text/plain")) + ) + + if not isinstance(self.some_array, Unset): + if isinstance(self.some_array, list): + for some_array_type_0_item_element in self.some_array: + field_list.append( + ( + "some_array", + (None, json.dumps(some_array_type_0_item_element.to_dict()).encode(), "application/json"), + ) + ) + else: + field_list.append(("some_array", (None, str(self.some_array).encode(), "text/plain"))) - some_array: Union[Unset, tuple[None, bytes, str]] - - if isinstance(self.some_array, Unset): - some_array = UNSET - elif isinstance(self.some_array, list): - _temp_some_array = [] - for some_array_type_0_item_data in self.some_array: - some_array_type_0_item = some_array_type_0_item_data.to_dict() - _temp_some_array.append(some_array_type_0_item) - some_array = (None, json.dumps(_temp_some_array).encode(), "application/json") - else: - some_array = (None, str(self.some_array).encode(), "text/plain") - - 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") + field_list.append( + ( + "some_optional_object", + (None, json.dumps(self.some_optional_object.to_dict()).encode(), "application/json"), + ) + ) - 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") + field_list.append(("some_enum", (None, str(self.some_enum.value).encode(), "text/plain"))) - 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 + field_list.append((prop_name, (None, json.dumps(prop.to_dict()).encode(), "application/json"))) - 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/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..81707d241 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,16 @@ def to_dict(self) -> dict[str, Any]: return field_dict - def to_multipart(self) -> dict[str, Any]: - a = self.a if isinstance(self.a, Unset) else (None, str(self.a).encode(), "text/plain") + def to_multipart(self) -> list[tuple[str, Any]]: + field_list: list[tuple[str, Any]] = [] - field_dict: dict[str, Any] = {} - for prop_name, prop in self.additional_properties.items(): - field_dict[prop_name] = (None, str(prop).encode(), "text/plain") + if not isinstance(self.a, Unset): + field_list.append(("a", (None, str(self.a).encode(), "text/plain"))) - field_dict.update({}) - if a is not UNSET: - field_dict["a"] = a + for prop_name, prop in self.additional_properties.items(): + field_list.append((prop_name, (None, str(prop).encode(), "text/plain"))) - 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..e582bd869 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 @@ -1,4 +1,3 @@ -import json from collections.abc import Mapping from typing import Any, TypeVar, Union, cast @@ -94,79 +93,66 @@ 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 + def to_multipart(self) -> list[tuple[str, Any]]: + field_list: list[tuple[str, Any]] = [] + 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") + for an_enum_value_item_element in self.an_enum_value: + field_list.append(("an_enum_value", (None, str(an_enum_value_item_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 + for an_enum_value_with_null_item_element in self.an_enum_value_with_null: + if an_enum_value_with_null_item_element is None: + field_list.append( + ( + "an_enum_value_with_null", + (None, str(an_enum_value_with_null_item_element).encode(), "text/plain"), + ) + ) 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_with_null", + (None, str(an_enum_value_with_null_item_element).encode(), "text/plain"), + ) + ) - 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_only_null_item_element in self.an_enum_value_with_only_null: + field_list.append( + ( + "an_enum_value_with_only_null", + (None, str(an_enum_value_with_only_null_item_element).encode(), "text/plain"), + ) + ) - an_allof_enum_with_overridden_default: Union[Unset, tuple[None, bytes, str]] = UNSET if not isinstance(self.an_allof_enum_with_overridden_default, Unset): - an_allof_enum_with_overridden_default = ( - None, - str(self.an_allof_enum_with_overridden_default).encode(), - "text/plain", + field_list.append( + ( + "an_allof_enum_with_overridden_default", + (None, str(self.an_allof_enum_with_overridden_default).encode(), "text/plain"), + ) ) - 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") + field_list.append( + ("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) - - _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") + for nested_list_of_enums_item_element in self.nested_list_of_enums: + for nested_list_of_enums_item_item_element in nested_list_of_enums_item_element: + field_list.append( + ( + "nested_list_of_enums", + (None, str(nested_list_of_enums_item_item_element).encode(), "text/plain"), + ) + ) - 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.append((prop_name, (None, str(prop).encode(), "text/plain"))) - 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..114f632f8 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,20 @@ def to_dict(self) -> dict[str, Any]: return field_dict - def to_multipart(self) -> dict[str, Any]: - a_string = (None, str(self.a_string).encode(), "text/plain") + def to_multipart(self) -> list[tuple[str, Any]]: + field_list: list[tuple[str, Any]] = [] - file = self.file.to_tuple() + field_list.append(("a_string", (None, str(self.a_string).encode(), "text/plain"))) - description = ( - self.description - if isinstance(self.description, Unset) - else (None, str(self.description).encode(), "text/plain") - ) + field_list.append(("file", self.file.to_tuple())) - field_dict: dict[str, Any] = {} - for prop_name, prop in self.additional_properties.items(): - field_dict[prop_name] = (None, str(prop).encode(), "text/plain") + if not isinstance(self.description, Unset): + field_list.append(("description", (None, str(self.description).encode(), "text/plain"))) - field_dict.update( - { - "a_string": a_string, - "file": file, - } - ) - if description is not UNSET: - field_dict["description"] = description + for prop_name, prop in self.additional_properties.items(): + field_list.append((prop_name, (None, str(prop).encode(), "text/plain"))) - return field_dict + return field_list @classmethod def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T: diff --git a/openapi_python_client/templates/model.py.jinja b/openapi_python_client/templates/model.py.jinja index eed2ea3a0..2f48654ec 100644 --- a/openapi_python_client/templates/model.py.jinja +++ b/openapi_python_client/templates/model.py.jinja @@ -82,32 +82,45 @@ 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) %} {% import "property_templates/" + property.template as prop_template %} -{% if multipart %} -{{ prop_template.transform_multipart(property, "self." + property.python_name, property.python_name) }} -{% elif prop_template.transform %} -{{ prop_template.transform(property=property, source="self." + property.python_name, destination=property.python_name) }} +{%- if prop_template.transform -%} +{{ prop_template.transform(property=property, source=content, destination=property.python_name) }} +{%- else -%} +{{ property.python_name }} = {{ content }} +{%- endif -%} +{% endmacro %} + +{% macro multipart(property, source, destination) %} +{% import "property_templates/" + property.template as prop_template %} +{% if not property.required %} +if not isinstance({{source}}, Unset): + {{ prop_template.multipart(property, source, destination) | indent(4) }} {% else %} -{{ property.python_name }} = self.{{ property.python_name }} +{{ prop_template.multipart(property, source, destination) }} {% endif %} +{% endmacro %} -{% endfor %} - +{% macro _prepare_field_dict() %} field_dict: dict[str, Any] = {} {% if model.additional_properties %} {% import "property_templates/" + model.additional_properties.template as prop_template %} -{% if multipart %} -for prop_name, prop in self.additional_properties.items(): - {{ prop_template.transform_multipart(model.additional_properties, "prop", "field_dict[prop_name]") | indent(4) }} -{% elif prop_template.transform %} +{% if prop_template.transform %} 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 +147,22 @@ 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 %} + {% set destination = "\"" + property.name + "\"" %} + {{ multipart(property, "self." + property.python_name, destination) | indent(8) }} + + {% endfor %} + + {% if model.additional_properties %} + for prop_name, prop in self.additional_properties.items(): + {{ multipart(model.additional_properties, "prop", "prop_name") | indent(4) }} + {% endif %} + + return field_list + {% endif %} @classmethod diff --git a/openapi_python_client/templates/property_templates/any_property.py.jinja b/openapi_python_client/templates/property_templates/any_property.py.jinja index 25a16516a..0eff2d50e 100644 --- a/openapi_python_client/templates/property_templates/any_property.py.jinja +++ b/openapi_python_client/templates/property_templates/any_property.py.jinja @@ -1,7 +1,3 @@ -{% macro transform_multipart(property, source, destination) %} -{% if not property.required %} -{{ destination }} = {{source}} if isinstance({{source}}, Unset) else (None, str({{source}}).encode(), "text/plain") -{% else %} -{{ destination }} = (None, str({{ source }}).encode(), "text/plain") -{% endif %} +{% macro multipart(property, source, destination) %} +field_list.append(({{ destination }}, (None, str({{source}}).encode(), "text/plain"))) {% endmacro %} \ No newline at end of file diff --git a/openapi_python_client/templates/property_templates/boolean_property.py.jinja b/openapi_python_client/templates/property_templates/boolean_property.py.jinja index 52fda08e1..b9a837e9c 100644 --- a/openapi_python_client/templates/property_templates/boolean_property.py.jinja +++ b/openapi_python_client/templates/property_templates/boolean_property.py.jinja @@ -2,10 +2,6 @@ "true" if {{ source }} else "false" {% endmacro %} -{% macro transform_multipart(property, source, destination) %} -{% if not property.required %} -{{ destination }} = {{source}} if isinstance({{source}}, Unset) else (None, str({{source}}).encode(), "text/plain") -{% else %} -{{ destination }} = (None, str({{ source }}).encode(), "text/plain") -{% endif %} +{% macro multipart(property, source, destination) %} +field_list.append(({{ destination }}, (None, str({{source}}).encode(), "text/plain"))) {% endmacro %} diff --git a/openapi_python_client/templates/property_templates/const_property.py.jinja b/openapi_python_client/templates/property_templates/const_property.py.jinja index fa635b5b9..8ad5bbff9 100644 --- a/openapi_python_client/templates/property_templates/const_property.py.jinja +++ b/openapi_python_client/templates/property_templates/const_property.py.jinja @@ -3,3 +3,7 @@ if {{ property.python_name }} != {{ property.value.python_code }}{% if not property.required %}and not isinstance({{ property.python_name }}, Unset){% endif %}: raise ValueError(f"{{ property.name }} must match const {{ property.value.python_code }}, got '{{'{' + property.python_name + '}' }}'") {%- endmacro %} + +{% macro multipart(property, source, destination) %} +field_list.append(({{ destination }}, source)) +{% endmacro %} diff --git a/openapi_python_client/templates/property_templates/date_property.py.jinja b/openapi_python_client/templates/property_templates/date_property.py.jinja index b5da665f7..bf88444fe 100644 --- a/openapi_python_client/templates/property_templates/date_property.py.jinja +++ b/openapi_python_client/templates/property_templates/date_property.py.jinja @@ -26,14 +26,7 @@ if not isinstance({{ source }}, Unset): {%- endif %} {% endmacro %} -{% macro transform_multipart(property, source, destination) %} +{% macro multipart(property, source, destination) %} {% set transformed = source + ".isoformat().encode()" %} -{% if property.required %} -{{ destination }} = {{ transformed }} -{%- else %} -{% set type_annotation = property.get_type_string(json=True) | replace("str", "bytes") %} -{{ destination }}: {{ type_annotation }} = UNSET -if not isinstance({{ source }}, Unset): - {{ destination }} = {{ transformed }} -{%- endif %} +field_list.append(({{ destination }}, {{ transformed }})) {% endmacro %} diff --git a/openapi_python_client/templates/property_templates/datetime_property.py.jinja b/openapi_python_client/templates/property_templates/datetime_property.py.jinja index 32e3f2ee6..29c38bc97 100644 --- a/openapi_python_client/templates/property_templates/datetime_property.py.jinja +++ b/openapi_python_client/templates/property_templates/datetime_property.py.jinja @@ -26,14 +26,7 @@ if not isinstance({{ source }}, Unset): {%- endif %} {% endmacro %} -{% macro transform_multipart(property, source, destination) %} +{% macro multipart(property, source, destination) %} {% set transformed = source + ".isoformat().encode()" %} -{% if property.required %} -{{ destination }} = {{ transformed }} -{%- else %} -{% set type_annotation = property.get_type_string(json=True) | replace("str", "bytes") %} -{{ destination }}: {{ type_annotation }} = UNSET -if not isinstance({{ source }}, Unset): - {{ destination }} = {{ transformed }} -{%- endif %} +field_list.append(({{ destination }}, {{ transformed }})) {% endmacro %} diff --git a/openapi_python_client/templates/property_templates/enum_property.py.jinja b/openapi_python_client/templates/property_templates/enum_property.py.jinja index c46538eac..aac89aeac 100644 --- a/openapi_python_client/templates/property_templates/enum_property.py.jinja +++ b/openapi_python_client/templates/property_templates/enum_property.py.jinja @@ -22,16 +22,9 @@ if not isinstance({{ source }}, Unset): {% endif %} {% endmacro %} -{% macro transform_multipart(property, source, destination) %} +{% macro multipart(property, source, destination) %} {% set transformed = "(None, str(" + source + ".value" + ").encode(), \"text/plain\")" %} -{% set type_string = "Union[Unset, tuple[None, bytes, str]]" %} -{% if property.required %} -{{ destination }} = {{ transformed }} -{%- else %} -{{ destination }}: {{ type_string }} = UNSET -if not isinstance({{ source }}, Unset): - {{ destination }} = {{ transformed }} -{% endif %} +field_list.append(({{ destination }}, {{ transformed }})) {% endmacro %} {% macro transform_header(source) %} diff --git a/openapi_python_client/templates/property_templates/file_property.py.jinja b/openapi_python_client/templates/property_templates/file_property.py.jinja index 8d27ae617..275650db3 100644 --- a/openapi_python_client/templates/property_templates/file_property.py.jinja +++ b/openapi_python_client/templates/property_templates/file_property.py.jinja @@ -22,12 +22,6 @@ if not isinstance({{ source }}, Unset): {% endif %} {% endmacro %} -{% macro transform_multipart(property, source, destination) %} -{% if property.required %} -{{ destination }} = {{ source }}.to_tuple() -{% else %} -{{ destination }}: {{ property.get_type_string(json=True) }} = UNSET -if not isinstance({{ source }}, Unset): - {{ destination }} = {{ source }}.to_tuple() -{% endif %} +{% macro multipart(property, source, destination) %} +field_list.append(({{ destination }}, {{ source }}.to_tuple())) {% endmacro %} diff --git a/openapi_python_client/templates/property_templates/float_property.py.jinja b/openapi_python_client/templates/property_templates/float_property.py.jinja index 12ffb0fb4..0729c82fb 100644 --- a/openapi_python_client/templates/property_templates/float_property.py.jinja +++ b/openapi_python_client/templates/property_templates/float_property.py.jinja @@ -2,10 +2,6 @@ str({{ source }}) {% endmacro %} -{% macro transform_multipart(property, source, destination) %} -{% if not property.required %} -{{ destination }} = {{source}} if isinstance({{source}}, Unset) else (None, str({{source}}).encode(), "text/plain") -{% else %} -{{ destination }} = (None, str({{ source }}).encode(), "text/plain") -{% endif %} +{% macro multipart(property, source, destination) %} +field_list.append(({{ destination }}, (None, str({{source}}).encode(), "text/plain"))) {% endmacro %} diff --git a/openapi_python_client/templates/property_templates/int_property.py.jinja b/openapi_python_client/templates/property_templates/int_property.py.jinja index 12ffb0fb4..0729c82fb 100644 --- a/openapi_python_client/templates/property_templates/int_property.py.jinja +++ b/openapi_python_client/templates/property_templates/int_property.py.jinja @@ -2,10 +2,6 @@ str({{ source }}) {% endmacro %} -{% macro transform_multipart(property, source, destination) %} -{% if not property.required %} -{{ destination }} = {{source}} if isinstance({{source}}, Unset) else (None, str({{source}}).encode(), "text/plain") -{% else %} -{{ destination }} = (None, str({{ source }}).encode(), "text/plain") -{% endif %} +{% macro multipart(property, source, destination) %} +field_list.append(({{ destination }}, (None, str({{source}}).encode(), "text/plain"))) {% endmacro %} diff --git a/openapi_python_client/templates/property_templates/list_property.py.jinja b/openapi_python_client/templates/property_templates/list_property.py.jinja index 94a9c6d65..fc68cb2ec 100644 --- a/openapi_python_client/templates/property_templates/list_property.py.jinja +++ b/openapi_python_client/templates/property_templates/list_property.py.jinja @@ -17,12 +17,8 @@ for {{ inner_source }} in (_{{ property.python_name }} or []): {% endif %} {% endmacro %} -{% macro _transform(property, source, destination, multipart, transform_method) %} +{% macro _transform(property, source, destination, transform_method) %} {% set inner_property = property.inner_property %} -{% if multipart %} -{% set multipart_destination = destination %} -{% set destination = "_temp_" + destination %} -{% endif %} {% import "property_templates/" + inner_property.template as inner_template %} {% if inner_template.transform %} {% set inner_source = inner_property.python_name + "_data" %} @@ -33,9 +29,6 @@ for {{ inner_source }} in {{ source }}: {% else %} {{ destination }} = {{ source }} {% endif %} -{% if multipart %} -{{ multipart_destination }} = (None, json.dumps({{ destination }}).encode(), 'application/json') -{% endif %} {% endmacro %} {% macro check_type_for_construct(property, source) %}isinstance({{ source }}, list){% endmacro %} @@ -44,34 +37,30 @@ for {{ inner_source }} in {{ source }}: {% set inner_property = property.inner_property %} {% set type_string = property.get_type_string(json=True) %} {% if property.required %} -{{ _transform(property, source, destination, False, "to_dict") }} +{{ _transform(property, source, destination, "to_dict") }} {% else %} {{ destination }}{% if declare_type %}: {{ type_string }}{% endif %} = UNSET if not isinstance({{ source }}, Unset): - {{ _transform(property, source, destination, False, "to_dict") | indent(4)}} + {{ _transform(property, source, destination, "to_dict") | indent(4)}} {% endif %} {% endmacro %} -{% macro transform_multipart(property, source, destination) %} +{% macro transform_multipart_body(property, source, destination) %} {% set inner_property = property.inner_property %} -{% set type_string = "Union[Unset, tuple[None, bytes, str]]" %} +{% set type_string = property.get_type_string(json=True) %} {% if property.required %} -{{ _transform(property, source, destination, True, "to_dict") }} +{{ _transform(property, source, destination, "to_multipart") }} {% else %} {{ destination }}: {{ type_string }} = UNSET if not isinstance({{ source }}, Unset): - {{ _transform(property, source, destination, True, "to_dict") | indent(4)}} + {{ _transform(property, source, destination, "to_multipart") | indent(4)}} {% endif %} {% endmacro %} -{% macro transform_multipart_body(property, source, destination) %} +{% macro multipart(property, source, destination) %} {% set inner_property = property.inner_property %} -{% set type_string = property.get_type_string(json=True) %} -{% if property.required %} -{{ _transform(property, source, destination, False, "to_multipart") }} -{% else %} -{{ destination }}: {{ type_string }} = UNSET -if not isinstance({{ source }}, Unset): - {{ _transform(property, source, destination, False, "to_multipart") | indent(4)}} -{% endif %} +{% import "property_templates/" + inner_property.template as inner_template %} +{% set inner_source = inner_property.python_name + "_element" %} +for {{ inner_source }} in {{ source }}: + {{ inner_template.multipart(inner_property, inner_source, destination) | indent(4) }} {% endmacro %} diff --git a/openapi_python_client/templates/property_templates/literal_enum_property.py.jinja b/openapi_python_client/templates/property_templates/literal_enum_property.py.jinja index 1506284d7..442d102ea 100644 --- a/openapi_python_client/templates/property_templates/literal_enum_property.py.jinja +++ b/openapi_python_client/templates/property_templates/literal_enum_property.py.jinja @@ -21,16 +21,9 @@ if not isinstance({{ source }}, Unset): {% endif %} {% endmacro %} -{% macro transform_multipart(property, source, destination) %} +{% macro multipart(property, source, destination) %} {% set transformed = "(None, str(" + source + ").encode(), \"text/plain\")" %} -{% set type_string = "Union[Unset, tuple[None, bytes, str]]" %} -{% if property.required %} -{{ destination }} = {{ transformed }} -{%- else %} -{{ destination }}: {{ type_string }} = UNSET -if not isinstance({{ source }}, Unset): - {{ destination }} = {{ transformed }} -{% endif %} +field_list.append(({{ destination }}, {{ transformed }})) {% endmacro %} {% macro transform_header(source) %} diff --git a/openapi_python_client/templates/property_templates/model_property.py.jinja b/openapi_python_client/templates/property_templates/model_property.py.jinja index 308b7478b..fbc517b43 100644 --- a/openapi_python_client/templates/property_templates/model_property.py.jinja +++ b/openapi_python_client/templates/property_templates/model_property.py.jinja @@ -34,14 +34,7 @@ if not isinstance({{ source }}, Unset): {%- endif %} {% endmacro %} -{% macro transform_multipart(property, source, destination) %} +{% macro multipart(property, source, destination) %} {% set transformed = "(None, json.dumps(" + source + ".to_dict()" + ").encode(), 'application/json')" %} -{% set type_string = property.get_type_string(multipart=True) %} -{% if property.required %} -{{ destination }} = {{ transformed }} -{%- else %} -{{ destination }}: {{ type_string }} = UNSET -if not isinstance({{ source }}, Unset): - {{ destination }} = {{ transformed }} -{%- endif %} +field_list.append(({{ destination }}, {{ transformed }})) {% endmacro %} 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..5f76702f9 100644 --- a/openapi_python_client/templates/property_templates/union_property.py.jinja +++ b/openapi_python_client/templates/property_templates/union_property.py.jinja @@ -75,25 +75,28 @@ else: {% endmacro %} -{% macro transform_multipart(property, source, destination) %} -{% set ns = namespace(has_if = false) %} -{{ destination }}: {{ property.get_type_string(json=False, multipart=True) }} - -{% if not property.required %} -if isinstance({{ source }}, Unset): - {{ destination }} = UNSET - {% set ns.has_if = true %} +{% 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 multipart(property, source, destination) %} +{% set ns = namespace(has_if = false) %} {% 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 %} {% import "property_templates/" + inner_property.template as inner_template %} - {{ inner_template.transform_multipart(inner_property, source, destination) | indent(4) | trim }} -{% endfor %} + {{ inner_template.multipart(inner_property, source, destination) | indent(4) | trim }} +{%- endfor -%} {% endmacro %} diff --git a/openapi_python_client/templates/property_templates/uuid_property.py.jinja b/openapi_python_client/templates/property_templates/uuid_property.py.jinja index 1f91c1e3a..922cf0f89 100644 --- a/openapi_python_client/templates/property_templates/uuid_property.py.jinja +++ b/openapi_python_client/templates/property_templates/uuid_property.py.jinja @@ -26,13 +26,6 @@ if not isinstance({{ source }}, Unset): {%- endif %} {% endmacro %} -{% macro transform_multipart(property, source, destination) %} -{% if property.required %} -{{ destination }} = str({{ source }}) -{%- else %} -{% set type_annotation = property.get_type_string(json=True) | replace("str", "bytes") %} -{{ destination }}: {{ type_annotation }} = UNSET -if not isinstance({{ source }}, Unset): - {{ destination }} = str({{ source }}) -{%- endif %} +{% macro multipart(property, source, destination) %} +field_list.append(({{ destination }},str({{ source }}))) {% endmacro %}