Fix optional discriminator literals#3608
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: ⛔ Files ignored due to path filters (2)
📒 Files selected for processing (7)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughChangesThe parser now assigns literal or enum-member defaults to forced-optional Pydantic v2 discriminator fields while preserving non-nullable Optional discriminator handling
Estimated code review effort: 3 (Moderate) | ~25 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 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 |
|
📚 Docs Preview: https://pr-3608.datamodel-code-generator.pages.dev |
Merging this PR will not alter performance
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/datamodel_code_generator/parser/base.py (1)
2045-2052: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueUpdate the type hint to accept Enum members.
To cleanly support using Enum values as the discriminator default when
use_enum_values_in_discriminator=True,literalshould also acceptMember.♻️ Proposed refactor
def __set_force_optional_discriminator_literal_default( self, model: DataModel, discriminator_field: DataModelFieldBase, - literal: DiscriminatorValue, + literal: DiscriminatorValue | Member, *, can_retain_cache: bool, ) -> None:🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/datamodel_code_generator/parser/base.py` around lines 2045 - 2052, Update the literal parameter type hint in __set_force_optional_discriminator_literal_default to accept Enum Member values in addition to DiscriminatorValue, preserving the existing behavior and other parameter types.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/datamodel_code_generator/parser/base.py`:
- Around line 2152-2171: Update the discriminator resolution flow around
_is_msgspec_struct and replace_field_type: only take the early-break path for
msgspec structs, preserving replace_field_type and discriminator_field.required
updates for Pydantic v2 models. Move
__set_force_optional_discriminator_literal_default to after the
replace_field_type block so it runs for every resolved Pydantic discriminator
field, including fields whose literals were inferred from a non-literal schema.
---
Nitpick comments:
In `@src/datamodel_code_generator/parser/base.py`:
- Around line 2045-2052: Update the literal parameter type hint in
__set_force_optional_discriminator_literal_default to accept Enum Member values
in addition to DiscriminatorValue, preserving the existing behavior and other
parameter types.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: f0b500ac-7e2c-4ef9-b39e-6a8cd7a7cf63
⛔ Files ignored due to path filters (2)
tests/data/expected/dynamic_models/force_optional_discriminator_literals.jsonis excluded by!tests/data/**/*.jsonand included by nonetests/data/openapi/discriminator_force_optional.yamlis excluded by!tests/data/**/*.yamland included by none
📒 Files selected for processing (7)
src/datamodel_code_generator/parser/base.pytests/data/expected/main/openapi/discriminator/force_optional_pydantic_v2.pytests/data/expected/main/openapi/discriminator/force_optional_pydantic_v2_dataclass.pytests/data/expected/main/openapi/discriminator/required_pydantic_v2.pytests/main/openapi/test_main_openapi.pytests/main/test_dynamic_models.pytox.ini
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3608 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 164 164
Lines 35647 35725 +78
Branches 4106 4121 +15
=========================================
+ Hits 35647 35725 +78
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Fixes: #3606
Summary by CodeRabbit
Bug Fixes
Tests