openapi-generators / openapi-generators/openapi-python-client
[Multi-part upload] TypeError: Object of type BytesIO is not JSON serializable
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 2k
- Forks
- 293
- Avg merge
- 34m
- Merged PRs (30d)
- 1
Description
I have a generated client code for FastAPI multi-file upload endpoint which I'm using like this:
file_names = ['fd1.py', 'fd2.py']
files = []
for f in file_names:
with open(f, 'rb') as fh:
buf = BytesIO(fh.read())
files.append(buf)
multipart_data = BodyUploadFeatureDefinitionFeatureDefinitionPost(
files=[File(payload=files[i], file_name=file_names[i], mime_type='text/x-python') for i in range(len(files))]
tags=[] # not relevant
)
res = upload_feature_definition_feature_definition_post.sync(
client=self.client,
multipart_data=multipart_data
)
I get an error:
Traceback (most recent call last):
File "/Users/anov/IdeaProjects/svoe/client/featurizer_client/featurizer_client.py", line 77, in <module>
client.register_feature_definition(
File "/Users/anov/IdeaProjects/svoe/client/featurizer_client/featurizer_client.py", line 63, in register_feature_definition
res = upload_feature_definition_feature_definition_post.sync(
File "/Users/anov/IdeaProjects/svoe/client/fast_api_client/api/default/upload_feature_definition_feature_definition_post.py", line 146, in sync
return sync_detailed(
File "/Users/anov/IdeaProjects/svoe/client/fast_api_client/api/default/upload_feature_definition_feature_definition_post.py", line 103, in sync_detailed
kwargs = _get_kwargs(
File "/Users/anov/IdeaProjects/svoe/client/fast_api_client/api/default/upload_feature_definition_feature_definition_post.py", line 40, in _get_kwargs
multipart_multipart_data = multipart_data.to_multipart()
File "/Users/anov/IdeaProjects/svoe/client/fast_api_client/models/body_upload_feature_definition_feature_definition_post.py", line 67, in to_multipart
files = (None, json.dumps(_temp_files).encode(), "application/json")
File "/Users/anov/miniconda3/envs/env_py_3-10/lib/python3.10/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/Users/anov/miniconda3/envs/env_py_3-10/lib/python3.10/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/Users/anov/miniconda3/envs/env_py_3-10/lib/python3.10/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/Users/anov/miniconda3/envs/env_py_3-10/lib/python3.10/site-packages/frozendict/__init__.py", line 32, in default
return BaseJsonEncoder.default(self, obj)
File "/Users/anov/miniconda3/envs/env_py_3-10/lib/python3.10/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type BytesIO is not JSON serializable
Generated File class:
@attr.s(auto_attribs=True)
class File:
"""Contains information for file uploads"""
payload: BinaryIO
file_name: Optional[str] = None
mime_type: Optional[str] = None
def to_tuple(self) -> FileJsonType:
"""Return a tuple representation that httpx will accept for multipart/form-data"""
return self.file_name, self.payload, self.mime_type
Looks like generated code tries to call json.dumps() on tuple representation of File, which has BinaryIO field, this causes the exception.
What am I doing wrong?
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
Start with the generated body_upload_feature_definition_feature_definition_post.py model and its to_multipart() method, then trace the _get_kwargs() call in upload_feature_definition_feature_definition_post.py. Reproduce the multipart request using the shown File and BytesIO values; done means valid file payloads no longer fail during JSON serialization.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- fastapi, openapi, python
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 32/100