openapi-generators / openapi-generators/openapi-python-client

String Binary File is just expecting BytesIO and not filename and mime-type

未关闭
#609 1 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

🐞bug
主要语言
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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 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

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。