swagger-api / swagger-api/swagger-codegen-generators
Can not upload binary data from python client generated by swagger
Open
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 299
- Forks
- 439
- PR merge metrics
- No merged PRs in 30d
Description
Code generated for POST binary code fails with error:
Cannot prepare a request message for provided
arguments. Please check that your arguments match
declared content type.
Specification example:
openapi: 3.0.3
info:
title: Tasks Example
version: 0.0.1
tags:
- name: example
description: Example spec
paths:
/task/{task_id}/attachment:
post:
summary: Add attachment for the task
description: Add binary attachemnt for te task provided by task id
operationId: task_attachment_post
tags:
- tasks
parameters:
- $ref: '#/components/parameters/task_id'
requestBody:
description: Binary attachment format
required: true
content:
application/octet-stream:
schema:
type: string
format: binary
responses:
201:
description: Nothing (void)
components:
parameters:
task_id:
name: task_id
in: path
schema:
type: integer
required: true
description: Id of the tile
example: 1234
config (just for case):
{
"generateSourceCodeOnly": true,
"packageName": "tasks_example",
"projectName": "tasks example",
"packageVersion": "0.0.1"
}
generate:
docker run -it --rm -v ${PWD}:/app -w /app swaggerapi/swagger-codegen-cli-v3:3.0.24 generate --input-spec /app/spec.yml --lang python --config /app/config.json --output /app/
my client code:
import random
import secrets
from tasks_example import TasksApi
if __name__ == "__main__":
file_len = random.randint(1000, 10000)
body_bytes = secrets.token_bytes(file_len)
task_api = TasksApi()
task_api.task_attachment_post(body_bytes, 123)
run my app:
pip install -r requirements.txt
python -m task_client_example
got error:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/Users/risik/work/task_example/task_client_example.py", line 11, in <module>
task_api.task_attachment_post(body_bytes, 123)
File "/Users/risik/work/task_example/tasks_example/api/tasks_api.py", line 55, in task_attachment_post
(data) = self.task_attachment_post_with_http_info(body, task_id, **kwargs) # noqa: E501
File "/Users/risik/work/task_example/tasks_example/api/tasks_api.py", line 122, in task_attachment_post_with_http_info
return self.api_client.call_api(
File "/Users/risik/work/task_example/tasks_example/api_client.py", line 316, in call_api
return self.__call_api(resource_path, method,
File "/Users/risik/work/task_example/tasks_example/api_client.py", line 148, in __call_api
response_data = self.request(
File "/Users/risik/work/task_example/tasks_example/api_client.py", line 358, in request
return self.rest_client.POST(url,
File "/Users/risik/work/task_example/tasks_example/rest.py", line 268, in POST
return self.request("POST", url,
File "/Users/risik/work/task_example/tasks_example/rest.py", line 203, in request
raise ApiException(status=0, reason=msg)
tasks_example.rest.ApiException: (0)
Reason: Cannot prepare a request message for provided
arguments. Please check that your arguments match
declared content type.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the failure with spec.yml using the documented swagger-codegen-cli Docker command, then trace the generated call through tasks_example/api/tasks_api.py, api_client.py, and rest.py. Check the binary POST request path and verify that the generated client can send the supplied bytes without raising ApiException.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- docker, openapi, python
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100