Fix type alias template crash with reuse_model on empty fields#3060
Fix type alias template crash with reuse_model on empty fields#3060koxudaxi merged 4 commits intokoxudaxi:mainfrom
Conversation
Guard fields[0] access in TypeStatement, TypeAliasType, TypeAliasAnnotation, and UnionTypeStatement templates. When fields is empty (reuse model), fall back to base_class, producing e.g. `type Foo = Bar`. Fixes koxudaxi#3059 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
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 (1)
📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdded a new expected fixture containing two type aliases and a regression test that verifies code generation with Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
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 |
Merging this PR will not alter performance
|
…AliasType These two union templates had the identical unguarded fields[0] access. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Black 22/23 can't parse Python 3.14 type statement syntax. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3060 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 86 86
Lines 18011 18014 +3
Branches 2075 2075
=========================================
+ Hits 18011 18014 +3
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.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@tests/main/openapi/test_main_openapi.py`:
- Around line 5217-5236: The test test_main_reuse_model_with_type_alias calls
run_main_and_assert but misses the expected_file parameter, causing it to look
for reuse_model_type_alias.py instead of the fixture; update the call to
run_main_and_assert (inside test_main_reuse_model_with_type_alias) to include
expected_file="reuse_model_with_type_alias.py" so the test uses the correct
expected fixture when asserting output.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: d862bc58-c5f1-443e-b85f-2ccdf605eb5c
⛔ Files ignored due to path filters (7)
src/datamodel_code_generator/model/template/TypeAliasAnnotation.jinja2is excluded by none and included by nonesrc/datamodel_code_generator/model/template/TypeAliasType.jinja2is excluded by none and included by nonesrc/datamodel_code_generator/model/template/TypeStatement.jinja2is excluded by none and included by nonesrc/datamodel_code_generator/model/template/UnionTypeAliasAnnotation.jinja2is excluded by none and included by nonesrc/datamodel_code_generator/model/template/UnionTypeAliasType.jinja2is excluded by none and included by nonesrc/datamodel_code_generator/model/template/UnionTypeStatement.jinja2is excluded by none and included by nonetests/data/openapi/reuse_model_type_alias.jsonis excluded by!tests/data/**/*.jsonand included by none
📒 Files selected for processing (2)
tests/data/expected/main/openapi/reuse_model_with_type_alias.pytests/main/openapi/test_main_openapi.py
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Breaking Change AnalysisResult: No breaking changes detected Reasoning: This PR is a pure bug fix. All 6 type alias Jinja2 templates previously crashed with This analysis was performed by Claude Code Action |
|
🎉 Released in 0.56.0 This PR is now available in the latest release. See the release notes for details. |
Fixes #3059
create_reuse_model()creates models withfields=[]andbase_classes=[base_ref]. Class-based templates (BaseModel) handle this naturally (class Foo(Bar): pass), but all six type alias templates unconditionally accessedfields[0], causing a Jinja2UndefinedError. The fix guardsfields[0]access and falls back tobase_classwhen fields is empty.Templates fixed:
TypeStatement.jinja2,TypeAliasType.jinja2,TypeAliasAnnotation.jinja2,UnionTypeStatement.jinja2,UnionTypeAliasAnnotation.jinja2,UnionTypeAliasType.jinja2.Before (bug)
After (fix)
Summary by CodeRabbit