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

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

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

还没有人认领这个 Issue。

主要语言
Python
星标
2k
派生
293
平均合并
34 分钟
30 天内合并 PR
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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

使用提供的 OpenAPI 规范和示例,通过 ApiClient 和 DefaultApi.submit_post 重现该故障,重点关注 SubmitPostRequestInput 和 multipart/form-data 的处理。跟踪生成的请求序列化过程直至 urllib3,然后验证 SubmitItem 值列表能够被接受,且不会出现 bytes-like-object TypeError。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
api
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
52/100

把新 issue 发到你的邮箱

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