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

Urllib error when trying to use list inside a form-data

オープン
#1,394 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
Python
スター
2k
フォーク
293
平均マージ
34分
マージ済み PR(30日)
1

説明

Describe the bug

I have an API that accepts a JSON list inside form-data, so the request is expected to look like this:

input=[{...}, {...}]

However, when I pass the list to the generated client, the request fails with the following error.

Could you please take a look and let me know what might be going wrong here?

from openapi_client import ApiClient, Configuration, DefaultApi, SubmitPostRequestInput, SubmitItem

config = Configuration()
config.host = "https://api.example.com"
api_client = ApiClient(configuration=config)
api_instance = DefaultApi(api_client)


request_input = SubmitPostRequestInput(
    [
        SubmitItem(flag1=True, flag2=False),
        SubmitItem(flag1=False, flag2=True)
    ]
)
api_instance.submit_post(input=request_input, inlcude_ses=True)

With the generated client and the code above I am getting the following error

File c:\Users\1592\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\_request_methods.py:264, in RequestMethods.request_encode_body(self, method, url, fields, headers, encode_multipart, multipart_boundary, **urlopen_kw)
    259     raise TypeError(
    260         "request got values for both 'fields' and 'body', can only specify one."
    261     )
    263 if encode_multipart:
--> [264](file:///C:/Users/1592/AppData/Local/Programs/Python/Python311/Lib/site-packages/urllib3/_request_methods.py:264)     body, content_type = encode_multipart_formdata(
    265         fields, boundary=multipart_boundary
    266     )
    267 else:
    268     body, content_type = (
    269         urlencode(fields),  # type: ignore[arg-type]
    270         "application/x-www-form-urlencoded",
    271     )

File c:\Users\1592\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\filepost.py:[81](file:///C:/Users/1592/AppData/Local/Programs/Python/Python311/Lib/site-packages/urllib3/filepost.py:81), in encode_multipart_formdata(fields, boundary)
     79         writer(body).write(data)
     80     else:
---> 81         body.write(data)
     83     body.write(b"\r\n")
     85 body.write(f"--{boundary}--\r\n".encode("latin-1"))

TypeError: a bytes-like object is required, not 'list'

Please use the below spec file and above sample snippet to reproduce the issue

OpenAPI Spec File

openapi: 3.0.3
info:
  title: Simple Form Data API
  version: 1.0.0

paths:
  /submit:
    post:
      summary: Submit form data with flags
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                input:
                  oneOf:
                    - $ref: '#/components/schemas/SubmitItem'
                    - type: array
                      items:
                        $ref: '#/components/schemas/SubmitItem'
                inlcude_ses:
                  type: boolean
      responses:
        '200':
          description: Successful submission

components:
  schemas:
    SubmitItem:
      type: object
      properties:
        flag1:
          type: boolean
        flag2:
          type: boolean

Desktop (please complete the following information):

  • OS: windows
  • Python Version: 3.11
  • openapi-generator version - 7.20.0-SNAPSHOT (Docker)

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

提供されたOpenAPI仕様とサンプルを使用し、ApiClientとDefaultApi.submit_postで障害を再現します。SubmitPostRequestInputとmultipart/form-dataの処理に焦点を当てます。生成されたリクエストのシリアライズをurllib3まで追跡し、その後、bytes-like-object TypeErrorなしでSubmitItem値のリストが受け入れられることを確認します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
api
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
52/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。