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 編號。

研究方向

先追蹤 generator 如何處理 OpenAPI requestBody 的 content 項目,以及如何產生生成的 _get_kwargs 函式。使用提供的 JSON 片段重現問題,然後為 application/json 和 multipart/form-data 兩者新增 coverage;完成的標準是生成的 client 能選擇正確的 body 處理方式,且沒有含義不明的相同條件。

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

評估

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

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

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