Fix crash when parsing enum containing only null value#2674
Conversation
… add tests for null-only enum generation
WalkthroughThis change adds support for empty enum schemas in OpenAPI/JSON Schema by modifying the enum parsing logic to generate either a null type reference or a synthetic model with a null-type root field, preventing the generation of invalid empty Enum classes that cause Black parsing failures. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~15 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2674 +/- ##
=======================================
Coverage 99.51% 99.51%
=======================================
Files 79 79
Lines 10986 11001 +15
Branches 1316 1318 +2
=======================================
+ Hits 10933 10948 +15
Misses 32 32
Partials 21 21
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
CodSpeed Performance ReportMerging #2674 will not alter performanceComparing Summary
Footnotes
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
tests/parser/test_jsonschema.py (1)
926-931: Good test coverage for the non-nullable empty enum case.The test correctly validates that an empty enum with no nullable flag returns a
Nonetype. Consider adding a complementary test for the nullable case to ensure both branches are unit-tested:def test_parse_enum_empty_enum_nullable() -> None: """Test parse_enum creates root model when enum_fields is empty and nullable.""" parser = JsonSchemaParser("") obj = JsonSchemaObject.parse_obj({"type": "string", "enum": [None], "nullable": True}) result = parser.parse_enum("NullEnum", obj, ["NullEnum"]) assert result.reference is not None assert len(parser.results) == 1
📜 Review details
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
src/datamodel_code_generator/parser/jsonschema.py(1 hunks)tests/data/expected/main/openapi/null_only_enum.py(1 hunks)tests/data/openapi/null_only_enum.yaml(1 hunks)tests/main/openapi/test_main_openapi.py(1 hunks)tests/parser/test_jsonschema.py(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
tests/parser/test_jsonschema.py (1)
src/datamodel_code_generator/parser/jsonschema.py (2)
JsonSchemaObject(186-450)parse_enum(2507-2689)
src/datamodel_code_generator/parser/jsonschema.py (3)
src/datamodel_code_generator/parser/openapi.py (1)
get_data_type(381-390)src/datamodel_code_generator/model/types.py (1)
get_data_type(100-106)src/datamodel_code_generator/reference.py (2)
reference(77-79)add(787-841)
🪛 Checkov (3.2.334)
tests/data/openapi/null_only_enum.yaml
[high] 1-14: Ensure that the global security field has rules defined
(CKV_OPENAPI_4)
🔇 Additional comments (4)
tests/data/expected/main/openapi/null_only_enum.py (1)
1-11: LGTM!The expected output correctly generates a
RootModelwith__root__: None = Nonefor the null-only enum case. This avoids the Black parsing error that occurred with empty Enum classes and properly represents a type that only acceptsNone.src/datamodel_code_generator/parser/jsonschema.py (1)
2570-2608: Well-structured fix for the null-only enum crash.The implementation correctly handles two cases:
- Empty enum that is not nullable → returns a simple
Nonetype- Empty enum that is nullable → creates a synthetic root model with a null-type field
This prevents the generation of empty Enum classes that caused Black parsing failures.
tests/data/openapi/null_only_enum.yaml (1)
1-12: LGTM!This test fixture correctly reproduces the minimal case from issue #1969 - a schema with
type: string,enum: [null], andnullable: true. The Checkov warning about missing security rules is irrelevant for a test fixture focused on schema parsing.tests/main/openapi/test_main_openapi.py (1)
4177-4185: LGTM! Well-structured regression test for null-only enum handling.The test appropriately validates the fix for issue #1969 by ensuring that OpenAPI schemas with
enum: [null]andnullable: trueno longer cause a crash and generate the expected output (RootModel[None]instead of an empty Enum class).
Summary
black.parsing.InvalidInputcrash when parsing OpenAPI schema withenum: [null]andnullable: trueRootModel[None]instead of empty Enum class for null-only enumsFixes #1969
Summary by CodeRabbit
New Features
Tests
✏️ Tip: You can customize this high-level summary in your review settings.