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

Upload file logic seems to be wrong on 3.1 schema

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

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ả

Describe the bug
I just upgraded my API, and the schema changed from 3.0 to 3.1. The uploads suddenly broke. It seems that the code generated for the body class now looks like this:

    file: str
 
    ...
    def to_multipart(self) -> types.RequestFiles:
        files: types.RequestFiles = []

        files.append(("file", (None, str(self.file).encode(), "text/plain")))

        for prop_name, prop in self.additional_properties.items():
            files.append((prop_name, (None, str(prop).encode(), "text/plain")))

        return files

While the old looked like this:

    file: File

    python
    def to_multipart(self) -> types.RequestFiles:
        files: types.RequestFiles = []

        files.append(("file", self.file.to_tuple()))

        for prop_name, prop in self.additional_properties.items():
            files.append((prop_name, (None, str(prop).encode(), "text/plain")))

        return files

Where the File object is this:

@define
class File:
    """Contains information for file uploads"""

    payload: BinaryIO
    file_name: str | None = None
    mime_type: str | None = None

    def to_tuple(self) -> FileTypes:
        """Return a tuple representation that httpx will accept for multipart/form-data"""
        return self.file_name, self.payload, self.mime_type

OpenAPI Spec File
A link to an OpenAPI document which produces this issue. Ideally, write a minimal reproduction only containing the problematic pieces.

The API spec for the body (new 3.1):

          "file": {
            "type": "string",
            "contentMediaType": "application/octet-stream",
            "title": "File"
          }

Old (3.0) schema:

          "file": {
            "type": "string",
            "format": "binary",
            "title": "File"
          }

Desktop:

  • OS: [e.g. macOS 10.15.1]: MacOS (M4) 26.3.1
  • Python Version: [e.g. 3.8.0]: 3.11.14
  • openapi-python-client version [e.g. 0.1.0]: 0.28.3

Additional context
I have some code pre-massaging around a different name-related issue and adding this to the pre-processing before this tool is called fixed it, but it's definitely more of a temporary workaround type of thing:

for prop in props.values():
      if prop.get("contentMediaType") == "application/octet-stream":
          prop["format"] = "binary"

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 bằng cách tái tạo lớp body được sinh từ schema OpenAPI 3.1 được cung cấp và so sánh nó với schema 3.0 bằng format: binary. Theo dõi cách contentMediaType: application/octet-stream được diễn giải trong quá trình sinh; hoàn tất khi trường được sinh sử dụng File và quá trình tuần tự hóa multipart gọi to_tuple() như trong đầu ra cũ.

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, tooling
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
55/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.