openapi-generators / openapi-generators/openapi-python-client
String Binary File is just expecting BytesIO and not filename and mime-type
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 2k
- 分支
- 293
- 平均合併
- 34 分鐘
- 30 天內合併 PR
- 1
描述
I have Java OpenAPI server which provides an upload endpoint (POST) that expect as schema a FileRequest ( schema = @Schema(implementation = FileRequest.class))
The FileRequest looks like:
@Value.Immutable
@JsonDeserialize(builder = com.mydata.request.FileRequest.Builder.class)
public abstract class FileRequest implements FileFields {
@Schema(type = "string", format = "binary", description = "File")
@JsonProperty
public abstract String file();
public static class Builder extends ImmutableFileRequest.Builder {
}
}
Looking at the generated code, the models looks good. This is the relevant part on types.py:
@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
T = TypeVar("T")
However, looking at the upload endpoint request, it doesn't expect the Optional values above:
@classmethod
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
d = src_dict.copy()
_file = d.pop("file", UNSET)
file: Union[Unset, File]
if isinstance(_file, Unset):
file = UNSET
else:
file = File(
payload = BytesIO(_file)
)
Notice that the File expects only the binary data, losing the filename and mimetype.
Expected behavior
I would expect that the generated code would look something like below, which I patched and am using currently:
else:
file = File(
payload = BytesIO(_file['payload']),
file_name=_file['filename']
)
Desktop (please complete the following information):
- OS: Linux, but irrelevant
- openapi-python-client version: 0.11.1
- Python 3.8.10
- openapi: 3.0.1
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
先從生成的 types.py 和 issue 中所示的 File.from_dict 程式碼開始,接著追蹤二進位字串 schema 在回應或請求解析期間的表示方式。重現提供的 FileRequest 上傳案例,並確認產生的 File 保留了 payload、filename 和 MIME type,而不只是二進位資料。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- openapi, python
- 領域
- api
- Issue 類型
- 缺陷
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100