openapi-generators / openapi-generators/openapi-python-client
[Multi-part upload] TypeError: Object of type BytesIO is not JSON serializable
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 2k
- Fork
- 293
- Merge trung bình
- 34 phút
- Pull request đã merge (30 ngày)
- 1
Mô tả
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?
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với model được tạo body_upload_feature_definition_feature_definition_post.py và phương thức to_multipart() của nó, sau đó lần theo lệnh gọi _get_kwargs() trong upload_feature_definition_feature_definition_post.py. Tái tạo yêu cầu multipart bằng các giá trị File và BytesIO được nêu; hoàn thành khi các payload tệp hợp lệ không còn thất bại trong quá trình tuần tự hóa JSON.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- fastapi, openapi, python
- Lĩnh vực
- api, tooling
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 32/100