[Multi-part upload] TypeError: Object of type BytesIO is not JSON serializable
還沒有人認領這個 Issue。
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 32/100
研究方向
先從產生的 body_upload_feature_definition_feature_definition_post.py 模型及其 to_multipart() 方法開始,接著追蹤 upload_feature_definition_feature_definition_post.py 中對 _get_kwargs() 的呼叫。使用所示的 File 和 BytesIO 值重現 multipart 請求;當有效的檔案 payload 不再於 JSON 序列化期間失敗時,即表示完成。
由索引模型根據 Issue 內容生成。
描述
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?
- 主要語言
- Python
- 星號
- 2k
- 分支
- 293
- 平均合併
- 34 分鐘
- 30 天內合併 PR
- 1
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
openapi-generators/openapi-python-client 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 68/100
-
難度 2/5 1-3 小時 新手友好度 65/100
-
難度 3/5 1-2 天 新手友好度 72/100
openapi-generators/openapi-python-client#1451 · 1 則留言 ·
-
難度 3/5 1-2 天 新手友好度 52/100
-
難度 3/5 1-2 天 新手友好度 64/100
openapi-generators/openapi-python-client#1435 · 1 則留言 ·
查看 openapi-generators/openapi-python-client 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 75/100
anthropics/skills#1811 · 1 則留言 ·
-
難度 2/5 1-3 小時 新手友好度 75/100
speaches-ai/speaches#678 ·
-
bug
難度 2/5 1-3 小時 新手友好度 75/100
datalayer/mcp-compose#42 ·
-
難度 2/5 1-3 小時 新手友好度 75/100
conda-forge/spacy-feedstock#177 ·
-
難度 2/5 1-3 小時 新手友好度 70/100
UKGovernmentBEIS/inspect_evals#2523 ·