openapi-generators / openapi-generators/openapi-python-client
Enum properties with nullable: true don't handle null values
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 2k
- フォーク
- 293
- 平均マージ
- 34分
- マージ済み PR(30日)
- 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
nullableis true, thennullis 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
nullbeing in the enum values (enum: [value, null]) - This issue is about
nullable: trueas 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
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
EnumProperty の実装と、issue で説明されている生成モデルの from_dict() パスから始めます。nullable な文字列プロパティが None を保持する方法を追跡し、次に enum の検証を維持したまま nullable な enum プロパティにも同等の処理を追加します。再現ケースが TypeError を発生させずに None を受け入れ、予期しない non-null 値は引き続き拒否することを確認します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- api
- issue の種類
- バグ
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 58/100