Skip to content

Commit 16790be

Browse files
committed
fix: support multiple files for multipart arrays
1 parent 89dc670 commit 16790be

File tree

1 file changed

+54
-19
lines changed

1 file changed

+54
-19
lines changed

openapi_python_client/templates/model.py.jinja

+54-19
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,26 @@ class {{ class_name }}:
7777
additional_properties: Dict[str, {{ additional_property_type }}] = _attrs_field(init=False, factory=dict)
7878
{% endif %}
7979

80-
{% macro _to_dict(multipart=False) %}
81-
{% for property in model.required_properties + model.optional_properties %}
80+
{% macro _transform_property(property, content, multipart=False) %}
8281
{% import "property_templates/" + property.template as prop_template %}
83-
{% if prop_template.transform %}
84-
{{ prop_template.transform(property, "self." + property.python_name, property.python_name, multipart=multipart) }}
85-
{% elif multipart %}
86-
{{ property.python_name }} = self.{{ property.python_name }} if isinstance(self.{{ property.python_name }}, Unset) else (None, str(self.{{ property.python_name }}).encode(), "text/plain")
87-
{% else %}
88-
{{ property.python_name }} = self.{{ property.python_name }}
89-
{% endif %}
82+
{%- if prop_template.transform -%}
83+
{{ prop_template.transform(property, content, property.python_name, multipart=multipart) }}
84+
{%- elif multipart -%}
85+
{{ property.python_name }} = {{ content }} if isinstance({{ content }}, Unset) else (None, str({{ content }}).encode(), "text/plain")
86+
{%- else -%}
87+
{{ property.python_name }} = {{ content }}
88+
{%- endif -%}
9089

91-
{% endfor %}
90+
{%- endmacro -%}
9291

92+
{% macro _prepare_field_dict(multipart=False) %}
9393
field_dict: Dict[str, Any] = {}
9494
{% if model.additional_properties %}
95-
{% if model.additional_properties.template %}{# Can be a bool instead of an object #}
96-
{% import "property_templates/" + model.additional_properties.template as prop_template %}
97-
{% else %}
98-
{% set prop_template = None %}
99-
{% endif %}
95+
{%- if model.additional_properties.template -%}{# Can be a bool instead of an object #}
96+
{%- import "property_templates/" + model.additional_properties.template as prop_template -%}
97+
{%- else -%}
98+
{%- set prop_template = None -%}
99+
{%- endif -%}
100100
{% if prop_template and prop_template.transform %}
101101
for prop_name, prop in self.additional_properties.items():
102102
{{ prop_template.transform(model.additional_properties, "prop", "field_dict[prop_name]", multipart=multipart, declare_type=false) | indent(4) }}
@@ -107,8 +107,16 @@ field_dict.update({
107107
})
108108
{% else %}
109109
field_dict.update(self.additional_properties)
110-
{% endif %}
111-
{% endif %}
110+
{%- endif -%}
111+
{%- endif -%}
112+
{% endmacro %}
113+
114+
{% macro _to_dict() %}
115+
{% for property in model.required_properties + model.optional_properties -%}
116+
{{ _transform_property(property, "self." + property.python_name) }}
117+
{% endfor %}
118+
119+
{{ _prepare_field_dict() }}
112120
field_dict.update({
113121
{% for property in model.required_properties + model.optional_properties %}
114122
{% if property.required %}
@@ -133,8 +141,35 @@ return field_dict
133141
{{ _to_dict() | indent(8) }}
134142

135143
{% if model.is_multipart_body %}
136-
def to_multipart(self) -> Dict[str, Any]:
137-
{{ _to_dict(multipart=True) | indent(8) }}
144+
def to_multipart(self) -> List[Tuple[str, Any]]:
145+
field_list: List[Tuple[str, Any]] = []
146+
{% for property in model.required_properties + model.optional_properties %}
147+
{% if property.__class__.__name__ == 'ListProperty' %}
148+
{% if not property.required %}
149+
for cont in self.{{ property.python_name }} or []:
150+
{% else %}
151+
for cont in self.{{ property.python_name }}:
152+
{% endif %}
153+
{{ _transform_property(property.inner_property, "cont", True) | indent(12) }}
154+
field_list.append(("{{ property.python_name }}", {{property.inner_property.python_name}}))
155+
156+
{% else %}
157+
{{ _transform_property(property, "self." + property.python_name, True) | indent(8) }}
158+
{% if not property.required %}
159+
if {{ property.python_name }} is not UNSET:
160+
field_list.append(("{{ property.python_name }}", {{property.python_name}}))
161+
{% else %}
162+
field_list.append(("{{ property.python_name }}", {{property.python_name}}))
163+
{% endif %}
164+
{% endif %}
165+
{% endfor %}
166+
167+
{{ _prepare_field_dict(True) | indent(8) }}
168+
169+
field_list += list(field_dict.items())
170+
171+
return field_list
172+
138173
{% endif %}
139174

140175
@classmethod

0 commit comments

Comments
 (0)