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

union types and nullables can create unnecessary class suffixes (and related problems)

オープン
#1,120 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
Python
スター
2k
フォーク
293
平均マージ
34分
マージ済み PR(30日)
1

説明

Describe the bug
In some valid specs, the use of anyOf, oneOf, nullable, and/or type as a list, can cause generated class names to have unnecessary suffixes (and, sometimes, for spurious extra copies of classes to appear). These problems seem to all ultimately come from the behavior of UnionProperty.

The problem cases are all included in the attached specs files. Looking at the code generated from these specs—

in both 3.0 & 3.1:

  • ExampleModel.nullableObjectWithOneOf correctly has the type Union[MyObject, None, Unset].
  • ExampleModel.inlineNullableObject generates a model class called ExampleModelInlineNullableObjectType0. It should be just ExampleModelInlineNullableObject, since there are no other named types for this property.
  • Similarly, the MyEnum schema, which is a nullable enum, generates the class MyEnumType1 even though there is no Type0.
  • The MyEnumWithExplicitType schema is the same as MyEnum except it specifically indicates type, and the result is a bit wilder: it generates enum classes MyEnumWithExplicitTypeType1, MyEnumWithExplicitType2Type1, and MyEnumWithExplicitType3Type1, all of which are exactly the same.

only applicable to 3.1:

  • ExampleModel.nullableObjectWithExplicitTypes, which is the same as nullable_object_with_one_of except that it also (unnecessarily, but validly) specifies type: ["object", "null"], also works correctly.
  • ExampleModel.one_of_enums_with_explicit_types, which combines a string enum with an int enum and specifies `type: ["string", "integer"], has the problem where three classes are created for each enum.

OpenAPI Spec Files
3.0: https://gist.github.com/eli-bl/7bea406525fb3b1452a71781ada5c1c0
3.1: https://gist.github.com/eli-bl/c03c88eb69312053e0122d1d8a06c2a0

Desktop (please complete the following information):

  • OS: macOS 14.5
  • Python Version: 3.8.15
  • openapi-python-client version 0.21.5

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

UnionProperty の動作から始め、リンクされた OpenAPI 3.0 および 3.1 の仕様ファイルからクライアントを生成します。生成されたクラス名と重複するモデルを、issue で説明されている例と比較します。nullable スキーマと union スキーマに不要なサフィックスが付加されず、同一の追加クラスも作成されなくなれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
openapi, python
領域
devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。