Skip to content

Fix root model reuse collapse#3089

Merged
koxudaxi merged 2 commits intomainfrom
fix/reuse-root-collapse
Apr 16, 2026
Merged

Fix root model reuse collapse#3089
koxudaxi merged 2 commits intomainfrom
fix/reuse-root-collapse

Conversation

@koxudaxi
Copy link
Copy Markdown
Owner

@koxudaxi koxudaxi commented Apr 16, 2026

fixes: #3082

Summary by CodeRabbit

  • Bug Fixes

    • Fixed preservation of field constraints when using model reuse together with root-model collapsing, preventing unintended model deduplication or constraint loss.
  • Tests

    • Added end-to-end test and expected output fixture to verify constraint preservation across reuse and collapse-root-models options.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 16, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: b61be919-8a97-461d-8adf-3aa1922db8b5

📥 Commits

Reviewing files that changed from the base of the PR and between 6a8fe65 and ceb0561.

📒 Files selected for processing (1)
  • tests/main/jsonschema/test_main_jsonschema.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/main/jsonschema/test_main_jsonschema.py

📝 Walkthrough

Walkthrough

Parser now skips root models during reuse/deduplication when collapse_root_models is enabled, preventing root-model merging that could remove fields or misapply constraints. A targeted test fixture and E2E test were added to validate the behavior.

Changes

Cohort / File(s) Summary
Core Parser Fix
src/datamodel_code_generator/parser/base.py
Skip models that are root types from the reuse/deduplication loop when collapse_root_models is enabled, avoiding incorrect merging and constraint propagation.
Test Fixture
tests/data/expected/main/jsonschema/reuse_model_collapse_root_models_constraints.py
Add Pydantic models (Shared, RecordA, RecordB, ReproBugs) demonstrating constrained field and root-model structure for the repro case.
E2E Test
tests/main/jsonschema/test_main_jsonschema.py
Add parametrized test test_main_reuse_model_collapse_root_models_preserves_constraints exercising --reuse-model with --collapse-root-models and variations of --collapse-reuse-models, asserting generated output matches the fixture.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Suggested labels

breaking-change-analyzed

Poem

🐰 I hopped through models, fields in sight,
Two roots once tangled in dedup's bite.
Now roots are skipped when collapse is true,
Constraints kept safe, and logic anew.
🍃🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix root model reuse collapse' directly describes the main code change: skipping root model processing in the reuse deduplication loop when collapse_root_models is enabled.
Linked Issues check ✅ Passed The PR changes skip root model processing in __reuse_model when collapse_root_models is enabled, preventing incorrect merging of constrained root models. A test fixture and E2E test validate that constraints are preserved when reusing/collapsing root models, addressing the core bugs described in issue #3082.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the root model reuse collapse issue: the core parser logic fix, a test fixture demonstrating the fix, and an E2E test validating constraint preservation.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/reuse-root-collapse

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 16, 2026

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 16, 2026

Merging this PR will not alter performance

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

✅ 11 untouched benchmarks
⏩ 98 skipped benchmarks1


Comparing fix/reuse-root-collapse (ceb0561) with main (5137795)

Open in CodSpeed

Footnotes

  1. 98 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (5137795) to head (ceb0561).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #3089   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           87        87           
  Lines        18300     18306    +6     
  Branches      2090      2091    +1     
=========================================
+ Hits         18300     18306    +6     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@koxudaxi koxudaxi merged commit 3e87a9a into main Apr 16, 2026
38 checks passed
@koxudaxi koxudaxi deleted the fix/reuse-root-collapse branch April 16, 2026 06:08
@github-actions
Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR is a pure bug-fix release addressing issue #3082. Three code changes: (1) parser/base.py skips root-type models during deduplication when collapse_root_models is enabled, preserving field constraints that were previously dropped; (2) main.py Config.merge_args clears non-explicit input source fields when input/url/input_model is set via CLI, fixing config-vs-CLI conflicts; (3) reference.py resolve_ref correctly joins a relative root_id with base_url instead of replacing it. No CLI options, Python API signatures, default values, template contracts, Python version support, or error-handling contracts are changed. Generated output may differ only for users who were previously hitting buggy code paths (e.g., combining --reuse-model with --collapse-root-models), and in those cases the new output is the corrected output — this is a fix, not a breaking change.


This analysis was performed by Claude Code Action

@github-actions
Copy link
Copy Markdown
Contributor

🎉 Released in 0.56.1

This PR is now available in the latest release. See the release notes for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bugs when combining reuse_model=True with collapse_root_models=True and use_title_as_name=True

1 participant