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

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

Đang mở
#609 1 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

🐞bug
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 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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với types.py được sinh ra và mã File.from_dict được hiển thị trong issue, sau đó theo dõi cách các schema chuỗi nhị phân được biểu diễn trong quá trình phân tích response hoặc request. Tái hiện trường hợp upload FileRequest đã cung cấp và xác minh rằng File kết quả giữ lại payload, filename và MIME type, thay vì chỉ có dữ liệu nhị phân.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
openapi, python
Lĩnh vực
api
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 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
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.