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

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

未關閉
#1,405 0 則留言 2 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

主要語言
Python
星號
2k
分支
293
平均合併
34 分鐘
30 天內合併 PR
1

描述

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

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 EnumProperty 的實作以及 issue 中描述的產生模型 from_dict() 路徑開始。追蹤 nullable 字串屬性如何保留 None,然後為 nullable 列舉屬性加入等效處理,同時保留列舉驗證。確認重現案例接受 None 而不會引發 TypeError,並且仍然拒絕意外的非 None 值。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
api
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
58/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。