Skip to content

Fix --reuse-model --collapse-reuse-models to deduplicate identical inline definitions#2903

Merged
koxudaxi merged 1 commit intomainfrom
fix/reuse-model-inline-definitions
Jan 2, 2026
Merged

Fix --reuse-model --collapse-reuse-models to deduplicate identical inline definitions#2903
koxudaxi merged 1 commit intomainfrom
fix/reuse-model-inline-definitions

Conversation

@koxudaxi
Copy link
Copy Markdown
Owner

@koxudaxi koxudaxi commented Jan 2, 2026

Fixes: #1792

Summary by CodeRabbit

  • Tests

    • Added test coverage for the --reuse-model combined with --collapse-reuse-models feature, validating model deduplication and correct output generation.
  • Chores

    • Minor code quality improvements.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jan 2, 2026

Warning

Rate limit exceeded

@koxudaxi has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 9 minutes and 44 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 2484a59 and 3c9fd85.

⛔ Files ignored due to path filters (3)
  • tests/data/jsonschema/reuse_model_collapse_nested.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/jsonschema/reuse_model_collapse_with_root.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/jsonschema/reuse_model_inline_definitions.json is excluded by !tests/data/**/*.json and included by none
📒 Files selected for processing (5)
  • src/datamodel_code_generator/parser/base.py
  • tests/data/expected/main/jsonschema/reuse_model_collapse_inline_definitions.py
  • tests/data/expected/main/jsonschema/reuse_model_collapse_nested.py
  • tests/data/expected/main/jsonschema/reuse_model_collapse_with_root.py
  • tests/main/jsonschema/test_main_jsonschema.py
📝 Walkthrough

Walkthrough

This PR adds test cases and expected output files to verify the --reuse-model and --collapse-reuse-models flag combination for JSON schema parsing with Pydantic v2 models, plus a minor noqa comment adjustment in the parser.

Changes

Cohort / File(s) Summary
Parser Comment Update
src/datamodel_code_generator/parser/base.py
Minor noqa comment amendment on the __delete_duplicate_models method; no functional changes.
Test Data: Model Reuse Collapse
tests/data/expected/main/jsonschema/reuse_model_collapse_inline_definitions.py, tests/data/expected/main/jsonschema/reuse_model_collapse_with_root.py
Two new expected output files containing Pydantic v2 BaseModel definitions demonstrating collapsed/reused model behavior. First file defines Pos, Node1, and Model with shared Pos references; second adds StringType (RootModel) and Nested1 alongside Pos and Model.
Test Functions
tests/main/jsonschema/test_main_jsonschema.py
Two new test functions: test_main_reuse_model_collapse_inline_definitions and test_main_reuse_model_collapse_with_root. Both invoke the generator with --reuse-model --collapse-reuse-models --output-model-type pydantic_v2.BaseModel flags and verify output against expected files.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • #2853: Addresses the same reuse-model/collapse behavior, converting aliases to class inheritance for Pydantic v2 models.
  • #2731: Implements the --collapse-reuse-models option and related parser logic that these tests verify.

Suggested labels

breaking-change-analyzed

Poem

🐰 Hop along through tests so neat,
Models reuse in collapse so sweet,
Duplicates fold, definitions align,
Pydantic schemas now perfectly design!

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and clearly summarizes the main change: fixing the --reuse-model --collapse-reuse-models flags to properly deduplicate identical inline definitions, which matches the code changes adding tests and expected output files.
Docstring Coverage ✅ Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%.

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 Jan 2, 2026

📚 Docs Preview: https://pr-2903.datamodel-code-generator.pages.dev

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Jan 2, 2026

CodSpeed Performance Report

Merging #2903 will not alter performance

Comparing fix/reuse-model-inline-definitions (3c9fd85) with main (a310b6f)

⚠️ 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.

Summary

✅ 11 untouched
⏩ 98 skipped1

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 Jan 2, 2026

Codecov Report

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

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2903   +/-   ##
=======================================
  Coverage   99.38%   99.38%           
=======================================
  Files          92       92           
  Lines       16342    16362   +20     
  Branches     1934     1939    +5     
=======================================
+ Hits        16241    16261   +20     
  Misses         52       52           
  Partials       49       49           
Flag Coverage Δ
unittests 99.38% <100.00%> (+<0.01%) ⬆️

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 force-pushed the fix/reuse-model-inline-definitions branch 3 times, most recently from 1042a36 to cf5989e Compare January 2, 2026 17:34
@koxudaxi koxudaxi force-pushed the fix/reuse-model-inline-definitions branch from cf5989e to 3c9fd85 Compare January 2, 2026 17:35
@koxudaxi koxudaxi enabled auto-merge (squash) January 2, 2026 17:37
@koxudaxi koxudaxi merged commit 8b7c441 into main Jan 2, 2026
35 checks passed
@koxudaxi koxudaxi deleted the fix/reuse-model-inline-definitions branch January 2, 2026 17:38
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 2, 2026

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR is a bug fix, not a breaking change. It fixes issue #1792 where --reuse-model --collapse-reuse-models was not properly deduplicating identical inline model definitions. The documentation already stated that --collapse-reuse-models should "collapse duplicate models by replacing references" - this PR makes that work correctly for inline definitions. The change only affects output when both flags are explicitly used together, and it now produces the documented/expected behavior. Users who were relying on the broken behavior (getting duplicate models when they should have been deduplicated) will see improved output that matches the intended feature design.


This analysis was performed by Claude Code Action

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 2, 2026

🎉 Released in 0.52.0

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.

1 participant