[BUG] generated python client code cannot send object in multipart/form-data
Created by: itaru2622
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
Have you tested with the latest master to confirm the issue still exists? -
Have you searched for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
generated python client code cannot send object in Content-Type:multipart/form-data
generator version: latest master branch yaml: modules/openapi-generator/src/test/resources/3_0/form-multipart-binary-array.yaml tested API(operationId): multipartMixed
generate python client code from the above yaml and make test app as described in generated doc (docs/MultipartApi.md). the test app receives error before sending HTTP request to the server. Error message says:
./test.py
Traceback (most recent call last):
File "/tmp/tests/test.py", line 19, in <module>
api_instance.multipart_mixed(file, marker=marker)
File "/tmp/tests/openapi_client/api_client.py", line 740, in __call__
return self.callable(self, *args, **kwargs)
File "/tmp/tests/openapi_client/api/multipart_api.py", line 218, in __multipart_mixed
return self.call_with_http_info(**kwargs)
File "/tmp/tests/openapi_client/api_client.py", line 802, in call_with_http_info
return self.api_client.call_api(
File "/tmp/tests/openapi_client/api_client.py", line 382, in call_api
return self.__call_api(resource_path, method,
File "/tmp/tests/openapi_client/api_client.py", line 192, in __call_api
response_data = self.request(
File "/tmp/tests/openapi_client/api_client.py", line 428, in request
return self.rest_client.POST(url,
File "/tmp/tests/openapi_client/rest.py", line 264, in POST
return self.request("POST", url,
File "/tmp/tests/openapi_client/rest.py", line 181, in request
r = self.pool_manager.request(
File "/usr/local/lib/python3.9/site-packages/urllib3/request.py", line 78, in request
return self.request_encode_body(
File "/usr/local/lib/python3.9/site-packages/urllib3/request.py", line 155, in request_encode_body
body, content_type = encode_multipart_formdata(
File "/usr/local/lib/python3.9/site-packages/urllib3/filepost.py", line 90, in encode_multipart_formdata
body.write(data)
TypeError: a bytes-like object is required, not 'dict'
openapi-generator version
latest master branch
OpenAPI declaration file content or url
modules/openapi-generator/src/test/resources/3_0/form-multipart-binary-array.yaml
Generation Details
python test app named 'test.py'
#!/usr/bin/env python3
import time
import openapi_client
from openapi_client.api import multipart_api
from openapi_client.model.multipart_mixed_marker import MultipartMixedMarker
from pprint import pprint
configuration = openapi_client.Configuration(host="http://localhost")
with openapi_client.ApiClient(configuration) as api_client:
api_instance = multipart_api.MultipartApi(api_client)
file= open('./test.py', 'rb')
marker = MultipartMixedMarker( name="name_example",)
try:
api_instance.multipart_mixed(file, marker=marker)
except openapi_client.ApiException as e:
print("Exception when calling MultipartApi->multipart_mixed: %s\n" % e)
Steps to reproduce
docker run -v ${PWD}:${PWD} -v ${PWD}/out:/out -w ${PWD} openapitools/openapi-generator-cli generate -g python -i form-multipart-binary-array.yaml -o /out
run test.py then got error.
Related issues/PRs
#2165 (closed) may be related and #2167 can fix this issue, but not sure.
Suggest a fix
in case of object, encode it in json and add 'content-type: application/json' as described bellow:
Content-Disposition: form-data; name="marker"
Content-Type: application/json
{"name": "name_example"}