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

Multiple body-types in an endpoint with same model (referenced via `$ref`) results in ambiguity in generated code

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

还没有人认领这个 Issue。

主要语言
Python
星标
2k
派生
293
平均合并
34 分钟
30 天内合并 PR
1

描述

Python Version OpenAPI Client Generator Version
3.11.6 0.18.0

Actual Behavior

When an endpoint has multiple body types, each referencing the same model (via a $ref key), the generator generates the same class name for each body types, which results then in the following generated code:

def _get_kwargs(
    # ...
):
    # ...

    if isinstance(body, WritableJournalEntryRequest):
        _json_body = body.to_dict()
 
        _kwargs["json"] = _json_body
        headers["Content-Type"] = "application/json"
    if isinstance(body, WritableJournalEntryRequest):
        _files_body = body.to_multipart()
 
        _kwargs["files"] = _files_body
        headers["Content-Type"] = "multipart/form-data"
 
    # ...

As you can see, both if-statement's conditions will resolve to True, therefore only the last body type will be used.

Desired Behavior

The body-type should be part of the generated class name to avoid such ambiguities.
Or an if/elseif/else should be used in the template (which would result in the first one being applied ONLY).

OpenAPI Spec File

The OpenAPI spec is from Netbox, but here is the relevant snippet:

"requestBody": {
    "content": {
        "application/json": {
            "schema": {
                "$ref": "#/components/schemas/WritableJournalEntryRequest"
            }
        },
        "multipart/form-data": {
            "schema": {
                "$ref": "#/components/schemas/WritableJournalEntryRequest"
            }
        }
    },
    "required": true
},

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先跟踪生成器如何处理 OpenAPI requestBody 的 content 条目并生成 _get_kwargs 函数。使用提供的 JSON 片段复现该问题,然后为 application/json 和 multipart/form-data 添加覆盖;完成标准是生成的客户端能够选择正确的 body 处理方式,且不存在含义不明确的相同条件。

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

评估

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

把新 issue 发到你的邮箱

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