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

Enum properties with nullable: true don't handle null values

Aperta
#1,405 0 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Python
Stelle
2k
Fork
293
Merge medio
34m
PR unite (30g)
1

Descrizione

Description

When an OpenAPI schema defines an enum property with nullable: true as a property attribute (rather than including null in the enum values), the generated Python code raises TypeError when deserializing null from the API.

OpenAPI Schema

conditionable_type:
  type: string
  enum:
    - AlertField
  nullable: true

Expected Behavior

Per OpenAPI v3.0.3 spec:

"If nullable is true, then null is allowed as a value regardless of any other constraints."

The generated code should accept None as a valid value.

Actual Behavior

The generated from_dict() method passes None directly to the enum check function, which raises:

TypeError: Unexpected value None. Expected one of {'AlertField'}

Generated Code (v0.28.2)

_conditionable_type = d.pop("conditionable_type", UNSET)
conditionable_type: SomeEnumType | Unset  # Missing `| None`
if isinstance(_conditionable_type, Unset):
    conditionable_type = UNSET
else:
    # Passes None directly to check function - FAILS
    conditionable_type = check_some_enum_type(_conditionable_type)

Expected Generated Code

Should be similar to how nullable string properties are handled:

def _parse_conditionable_type(data: object) -> SomeEnumType | None | Unset:
    if data is None:
        return data
    if isinstance(data, Unset):
        return data
    return check_some_enum_type(data)

conditionable_type = _parse_conditionable_type(d.pop("conditionable_type", UNSET))

Root Cause

The EnumProperty class only checks for null in the enum values list:

unchecked_value_list = [value for value in enum if value is not None]

It does not check for nullable: true as a separate property attribute on the schema.

Related Issues

  • #504, #512, #516 - Fixed null being in the enum values (enum: [value, null])
  • This issue is about nullable: true as a property attribute (enum: [value] + nullable: true)

Both are valid per OpenAPI spec, but only the first case is currently handled.

Reproduction

Minimal OpenAPI spec:

openapi: "3.0.3"
info:
  title: Test
  version: "1.0"
paths: {}
components:
  schemas:
    TestModel:
      type: object
      properties:
        my_enum:
          type: string
          enum:
            - value1
          nullable: true

Generate client, then:

from my_client.models import TestModel
TestModel.from_dict({"my_enum": None})  # Raises TypeError

Version

  • openapi-python-client: 0.28.2

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dall’implementazione di EnumProperty e dal percorso from_dict() del modello generato descritto nell’issue. Traccia il modo in cui le proprietà string nullable preservano None, quindi aggiungi una gestione equivalente per le proprietà enum nullable mantenendo la validazione dell’enum. Verifica che la riproduzione accetti None senza sollevare TypeError e continui a rifiutare i valori non-None imprevisti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
api
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
58/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.