fix: quote forward references in recursive RootModel generic parameters#2720
fix: quote forward references in recursive RootModel generic parameters#2720
Conversation
WalkthroughUpdates the parser to include RootModel in forward reference handling during type alias updates. Adds comprehensive test coverage for recursive type aliases with Pydantic v2, including expected output file and corresponding test function to validate the functionality. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 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 |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/datamodel_code_generator/parser/base.py (1)
392-415: Field-based dependency ordering for RootModel is reasonable, minor duplication onlyUsing field
data_typereferences instead ofbase_classesforpydantic_model_v2.RootModelwhen computingindexesensures recursive RootModels are ordered after their dependencies instead of being treated as independent. The comprehension is clear and matches the existing base-class logic.If more model types gain special dependency rules later, consider extracting this into a small helper like
_unresolved_dep_indexes(model, unresolved_paths)to avoid repeating the index-computation pattern, but it's not necessary for this change.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/datamodel_code_generator/parser/base.py(1 hunks)tests/data/expected/main/openapi/type_alias_recursive_pydantic_v2.py(1 hunks)tests/main/openapi/test_main_openapi.py(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
tests/data/expected/main/openapi/type_alias_recursive_pydantic_v2.py (1)
src/datamodel_code_generator/model/base.py (1)
path(680-682)
src/datamodel_code_generator/parser/base.py (2)
src/datamodel_code_generator/model/type_alias.py (1)
TypeAliasBase(21-33)src/datamodel_code_generator/model/pydantic_v2/root_model.py (1)
RootModel(13-35)
🔇 Additional comments (4)
src/datamodel_code_generator/parser/base.py (2)
129-134: RootModel inclusion in dependency pruning exception looks correctExtending the
use_deferred_annotationsguard to keepfield_refsfor bothTypeAliasBaseandpydantic_model_v2.RootModelkeeps their field-based dependencies visible to_reorder_models_keep_model_order, which is consistent with how forward-ref-sensitive models need to be ordered. No issues from a correctness perspective.
1895-1924: Extending __update_type_aliases to RootModel correctly covers recursive genericsUpdating the docstring and
isinstancecheck so that bothTypeAliasBaseandpydantic_model_v2.RootModelparticipate in forward-ref detection means:
- References in RootModel fields to same-module models defined later get
alias='"Name"'assigned.model.has_forward_referenceis now set for such RootModels, enabling the existing rebuild/forward-ref plumbing to work for them as well.This aligns with the new recursive Pydantic v2 RootModel test and avoids NameError at class definition time for generic parameters. The logic is consistent with the existing type-alias handling.
tests/main/openapi/test_main_openapi.py (1)
3471-3488: New Pydantic v2 recursive RootModel test is well-aligned with existing coverageThe test wires
type_alias_recursive.yamlto the new expected Pydantic v2 output, with arguments and naming consistent with the surrounding type-alias/recursive tests. It clearly documents the RootModel-specific forward-ref behavior and should reliably guard this regression.tests/data/expected/main/openapi/type_alias_recursive_pydantic_v2.py (1)
1-46: Expected Pydantic v2 recursive RootModel output matches the intended behaviorThe generated models correctly:
- Use
RootModel[...]forElementaryTypeandJsonType.- Quote recursive
JsonTypereferences in the generic parameter (list["JsonType"],dict[str, "JsonType"]).- Include
model_rebuild()calls for models that participate in forward references.This aligns with the new test and the parser changes; the file looks syntactically and semantically sound as a golden output.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2720 +/- ##
=======================================
Coverage 99.34% 99.34%
=======================================
Files 82 82
Lines 11615 11658 +43
Branches 1406 1410 +4
=======================================
+ Hits 11539 11582 +43
Misses 45 45
Partials 31 31
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 #2720 will not alter performanceComparing Summary
Footnotes
|
Summary by CodeRabbit
Release Notes
Bug Fixes
Tests
✏️ Tip: You can customize this high-level summary in your review settings.