Skip to content

fix: Allow passing data with files in multipart. (Fixes #351) #355

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

Merged
merged 3 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from ...client import Client
from ...models.body_upload_file_tests_upload_post import BodyUploadFileTestsUploadPost
from ...models.http_validation_error import HTTPValidationError
from ...types import UNSET, Response, Unset
from ...types import UNSET, File, Response, Unset


def _get_kwargs(
Expand All @@ -22,12 +22,21 @@ def _get_kwargs(
if keep_alive is not UNSET:
headers["keep-alive"] = keep_alive

files = {}
data = {}
for key, value in multipart_data.to_dict().items():
if isinstance(value, File):
files[key] = value
else:
data[key] = value

return {
"url": url,
"headers": headers,
"cookies": cookies,
"timeout": client.get_timeout(),
"files": multipart_data.to_dict(),
"files": files,
"data": data,
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from io import BytesIO
from typing import Any, Dict, Type, TypeVar
from typing import Any, Dict, Type, TypeVar, Union

import attr

from ..types import File
from ..types import UNSET, File, Unset

T = TypeVar("T", bound="BodyUploadFileTestsUploadPost")

Expand All @@ -13,16 +13,21 @@ class BodyUploadFileTestsUploadPost:
""" """

some_file: File
some_string: Union[Unset, str] = "some_default_string"

def to_dict(self) -> Dict[str, Any]:
some_file = self.some_file.to_tuple()

some_string = self.some_string

field_dict: Dict[str, Any] = {}
field_dict.update(
{
"some_file": some_file,
}
)
if some_string is not UNSET:
field_dict["some_string"] = some_string

return field_dict

Expand All @@ -31,8 +36,11 @@ def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
d = src_dict.copy()
some_file = File(payload=BytesIO(d.pop("some_file")))

some_string = d.pop("some_string", UNSET)

body_upload_file_tests_upload_post = cls(
some_file=some_file,
some_string=some_string,
)

return body_upload_file_tests_upload_post
5 changes: 5 additions & 0 deletions end_to_end_tests/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,11 @@
"title": "Some File",
"type": "string",
"format": "binary"
},
"some_string": {
"title": "Some String",
"type": "string",
"default": "some_default_string"
}
},
"additionalProperties": false
Expand Down
21 changes: 15 additions & 6 deletions openapi_python_client/templates/endpoint_module.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import httpx
from attr import asdict

from ...client import AuthenticatedClient, Client
from ...types import Response, UNSET
from ...types import Response, UNSET{% if endpoint.multipart_body_reference %}, File {% endif %}

{% for relative in endpoint.relative_imports %}
{{ relative }}
Expand Down Expand Up @@ -36,18 +36,27 @@ def _get_kwargs(

{{ json_body(endpoint) | indent(4) }}

{% if endpoint.multipart_body_reference %}
files = {}
data = {}
for key, value in multipart_data.to_dict().items():
if isinstance(value, File):
files[key] = value
else:
data[key] = value
{% endif %}

return {
"url": url,
"headers": headers,
"cookies": cookies,
"timeout": client.get_timeout(),
{% if endpoint.form_body_reference %}
"data": asdict(form_data),
{% endif %}
{% if endpoint.multipart_body_reference %}
"files": multipart_data.to_dict(),
{% endif %}
{% if endpoint.json_body %}
{% elif endpoint.multipart_body_reference %}
"files": files,
"data": data,
{% elif endpoint.json_body %}
"json": {{ "json_" + endpoint.json_body.python_name }},
{% endif %}
{% if endpoint.query_parameters %}
Expand Down
129 changes: 0 additions & 129 deletions tests/test_templates/endpoint_module.py

This file was deleted.

56 changes: 0 additions & 56 deletions tests/test_templates/test_endpoint_module.py

This file was deleted.