openapi-generators / openapi-generators/openapi-python-client
Enum properties with nullable: true don't handle null values
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 2k
- Fork
- 293
- Merge trung bình
- 34 phút
- Pull request đã merge (30 ngày)
- 1
Mô tả
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
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với phần triển khai EnumProperty và đường đi from_dict() của model được sinh ra như mô tả trong issue. Theo dõi cách các thuộc tính string nullable giữ nguyên None, sau đó thêm cách xử lý tương đương cho các thuộc tính enum nullable trong khi vẫn giữ lại việc validation enum. Xác nhận rằng bản tái hiện chấp nhận None mà không phát sinh TypeError và vẫn từ chối các giá trị non-null không mong đợi.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- api
- Loại issue
- Lỗi
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 58/100