Fix handling of falsy default values for enums in set-default-enum-member option#2977
Conversation
📝 WalkthroughWalkthroughUpdated enum default handling so falsy defaults (e.g., 0, "") are treated as valid enum members instead of being coerced away; adjusted checks to distinguish Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Suggested labels
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 |
ilovelinux
left a comment
There was a problem hiding this comment.
It looks good but we need a unit test to cover that edge case. It will help us to verify the fix and prevent any future regressions 🙂
|
Thanks for the suggestion. I’ve added a unit test to cover that falsy default enum edge case to verify the fix and help prevent future regressions. |
Merging this PR will not alter performance
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2977 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 94 94
Lines 17780 17782 +2
Branches 2044 2044
=========================================
+ Hits 17780 17782 +2
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:
|
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where falsy default values (0, False, empty string) for enum fields were not being converted to their corresponding enum members when using the --set-default-enum-member flag. The issue occurred because the code was using truthy checks (if not model_field.default) instead of explicit None checks (if model_field.default is None).
Changes:
- Fixed condition in
__set_default_enum_memberto check forNoneexplicitly instead of using truthy evaluation - Fixed string conversion in
find_memberto handle falsy values correctly - Added test coverage for falsy default values (integer 0 and empty string)
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/datamodel_code_generator/parser/base.py | Changed falsy check to explicit None check in __set_default_enum_member |
| src/datamodel_code_generator/model/enum.py | Fixed default value conversion to handle None separately from falsy values in find_member |
| tests/main/jsonschema/test_main_jsonschema.py | Added test for falsy default enum values |
| tests/data/jsonschema/falsy_default_enum_number.json | Test input with integer 0 and empty string defaults |
| tests/data/expected/main/jsonschema/falsy_default_enum_number.py | Expected output showing correct enum member assignment |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Breaking Change AnalysisResult: No breaking changes detected Reasoning: This PR is a bug fix, not a breaking change. The changes fix incorrect handling of falsy default values (0, "", False) when using the --set-default-enum-member option. Previously, falsy defaults were incorrectly treated as absent/None due to Python's truthiness evaluation ( This analysis was performed by Claude Code Action |
|
🎉 Released in 0.54.0 This PR is now available in the latest release. See the release notes for details. |
Fixes: #2976
Summary by CodeRabbit
Bug Fixes
Tests
Samples