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

`File.file_name` marked as `Optional` but is required when used in `multipart/form-data`

未關閉
#993 1 則留言 6 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

主要語言
Python
星號
2k
分支
293
平均合併
34 分鐘
30 天內合併 PR
1

描述

Describe the bug

In types.py:

https://github.com/openapi-generators/openapi-python-client/blob/0399271be4eb012f83ac3023939ff48e364634e9/openapi_python_client/templates/types.py.jinja#L24

file_name is marked Optional, so it seems like the user could omit it. But if they do to a multipart/form endpoint, backends such as DjangoRestFramework will return:

Status Code: 400 (Bad Request)
Content    : {"file":["The submitted data was not a file. Check the encoding type on the form."]}

This means that users will not get a type checking error if they forget to include a file_name in this scenario.

It seems like perhaps a MultipartFile type is needed to be used to correctly model this?

@define
class MultipartFile:
    """Contains information for multipart file uploads"""

    payload: BinaryIO
    file_name: str
    mime_type: Optional[str] = None

    ...

OpenAPI Spec File

  /upload/:
    post:
      operationId: upload
      requestBody:
        content:
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/UploadRequest'
        required: true

components:
  schemas:
    UploadRequest:
      type: object
      properties:
        file:
          type: string
          format: binary
      required:
      - file

Desktop (please complete the following information):

  • OS: macOS 13.6.4
  • Python Version: 3.12.0
  • openapi-python-client version: 0.16.1

Additional context

The generated Swagger UI page for this endpoint looks like:

Screenshot 2024-03-08 at 8 06 45 AM

Backend versions:

  • Django 4.2.11
  • djangorestframework 3.14.0
  • drf-spectacular 0.27.1

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從連結的 templates/types.py.jinja 定義開始,追蹤 multipart/form-data 檔案欄位在產生的用戶端中的表示方式。重現提供的 UploadRequest 規格,然後驗證必要的 multipart 檔案在產生的型別中也需要 file_name,並避免已回報的後端錯誤。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
api
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。