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

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

Offen
#1,120 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
Python
Sterne
2k
Forks
293
Ø Merge
34 Min.
Gemergte PRs (30 T.)
1

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem Verhalten von UnionProperty und generiere Clients aus den verknüpften OpenAPI 3.0- und 3.1-Spezifikationsdateien. Vergleiche die generierten Klassennamen und doppelten Modelle mit den im Issue beschriebenen Beispielen. Als abgeschlossen gilt die Aufgabe, wenn nullable- und Union-Schemas keine unnötigen Suffixe mehr erhalten oder identische zusätzliche Klassen erstellen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
openapi, python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.