openapi-generators / openapi-generators/openapi-python-client
Generation for multiple content types for a single endpoint fails when content types expect identical models
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 2k
- 分支
- 293
- 平均合併
- 34 分鐘
- 30 天內合併 PR
- 1
描述
Describe the bug
Support for multiple content types for a single endpoint was added in #453 / #822. However, the implemented fix generates non-functional code if the content types expect the same model. For instance, take this Api Spec snippet:
paths./api/dns/add/...
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/DnsCreate'
application/x-www-form-urlencoded:
schema:
$ref: '#/components/schemas/DnsCreate'
multipart/form-data:
schema:
$ref: '#/components/schemas/DnsCreate'
required: true
...
components.schemas...:
DnsCreate:
type: object
properties:
name:
type: string
dns_type:
oneOf:
- $ref: '#/components/schemas/DnsTypeEnum'
- $ref: '#/components/schemas/BlankEnum'
content:
type: string
ttl:
type: integer
maximum: 2147483647
minimum: -2147483648
nullable: true
required:
- content
- dns_type
- name
All 3 content types expect the same content type: DnsCreate. However, this generates this Python code:
def _get_kwargs(
*,
body: Union[
DnsCreate,
DnsCreate,
DnsCreate,
],
) -> dict[str, Any]:
headers: dict[str, Any] = {}
_kwargs: dict[str, Any] = {
"method": "post",
"url": "/api/dns/add/",
}
if isinstance(body, DnsCreate):
_kwargs["json"] = body.to_dict()
headers["Content-Type"] = "application/json"
if isinstance(body, DnsCreate):
_kwargs["data"] = body.to_dict()
headers["Content-Type"] = "application/x-www-form-urlencoded"
if isinstance(body, DnsCreate):
_kwargs["files"] = body.to_multipart()
headers["Content-Type"] = "multipart/form-data"
_kwargs["headers"] = headers
return _kwargs
This results in all requests made to this endpoint being treated as multipart form data, and it is impossible to make a JSON or form-urlencoded request.
Desktop (please complete the following information):
- Python Version: 3.12.3
- openapi-python-client version: 0.25.0
Additional context
While it may seem odd to support 3 different content types for the same endpoint, this specification has the advantage of making Django Rest Framework's interactive API support form uploads (instead of requiring you to manually edit JSON). While I'm sure this is a more niche problem, I think we can all agree that this is problematic code generation. While I haven't contributed to this repo, perhaps supporting an additional content type parameter is the solution (rather than relying on isinstance calls).
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
首先,使用 issue 中的 OpenAPI 片段重現生成流程,並檢查產生的 endpoint _get_kwargs 函式。追蹤相同模型如何與每種請求內容類型建立關聯;當 JSON、form-urlencoded 和 multipart 請求仍可供選擇,而不是被最後一個分支覆寫時,即完成。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- openapi, python
- 領域
- api, tooling
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100