Generation for multiple content types for a single endpoint fails when content types expect identical models
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 45/100
調査の方向性
まず、issue の OpenAPI スニペットを使って生成を再現し、生成された endpoint の _get_kwargs 関数を調べます。同一のモデルが各リクエストのコンテンツタイプにどのように関連付けられているかを追跡します。JSON、form-urlencoded、multipart のリクエストが最後のブランチによって上書きされず、選択可能なままであれば完了です。
索引モデルが issue の本文から書いたものです。
説明
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).
- 主要言語
- Python
- スター
- 2k
- フォーク
- 293
- 平均マージ
- 34分
- マージ済み PR(30日)
- 1
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
openapi-generators/openapi-python-client のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 72/100
openapi-generators/openapi-python-client#1451 · コメント 1 件 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 52/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 64/100
openapi-generators/openapi-python-client#1435 · コメント 1 件 ·
openapi-generators/openapi-python-client の issue をすべて見る
似ている issue
-
link-check link-check:sphinx-theme
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
qgis/QGIS-Documentation#11275 ·
-
bug priority:normal ready-for-dev
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
OpenHands/extensions#626 · コメント 1 件 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
CSCfi/sd-search-api#39 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100