Conversation
WalkthroughAdds a new CLI/config option Changes
Sequence Diagram(s)sequenceDiagram
participant User as CLI User
participant CLI as datamodel-codegen CLI
participant Gen as generate()
participant Parser as Parser (JsonSchema/OpenAPI/GraphQL)
participant Dedupe as Parser.deduplication logic
participant FS as File output
User->>CLI: run with --reuse-model --collapse-reuse-models
CLI->>Gen: build Config (collapse_reuse_models=True) & call generate(...)
Gen->>Parser: instantiate parser(collapse_reuse_models=True)
Parser->>Dedupe: parse models -> identify duplicates
Dedupe-->>Parser: collapse duplicates into canonical refs (when enabled)
Parser->>Gen: return consolidated model modules
Gen->>FS: write generated code files
FS-->>User: output files created
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🧰 Additional context used🧬 Code graph analysis (1)src/datamodel_code_generator/parser/base.py (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
🔇 Additional comments (2)
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 |
🤖 Generated by GitHub Actions
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2731 +/- ##
=======================================
Coverage 99.36% 99.36%
=======================================
Files 83 83
Lines 12062 12068 +6
Branches 1456 1456
=======================================
+ Hits 11985 11991 +6
Misses 45 45
Partials 32 32
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
🧹 Nitpick comments (2)
src/datamodel_code_generator/__main__.py (1)
442-443: Config plumbing forcollapse_reuse_modelsis consistent; consider UX warningThe new
Config.collapse_reuse_modelsfield and its propagation intorun_generate_from_config()/generate()look consistent with existing flags likecollapse_root_modelsandreuse_model.You might optionally add a CLI-level warning analogous to the
--reuse-scope=treecheck (e.g., whencollapse_reuse_modelsis true butreuse_modelis false) so users understand when the flag is a no-op.Also applies to: 747-748
tests/main/test_main_json.py (1)
108-128: New CLI doc test for--collapse-reuse-modelsis well targetedThe test cleanly exercises
--reuse-model+--collapse-reuse-modelsagainstduplicate_models.jsonwith a dedicated golden file, and thecli_docmetadata ties into the docs pipeline appropriately.If you want parity with
test_main_json_reuse_model_pydantic2, you could later add a similar Pydantic v2 variant for this option, but it’s not strictly required for this PR.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (13)
docs/cli-reference/index.mddocs/cli-reference/model-customization.mddocs/cli-reference/quick-reference.mdsrc/datamodel_code_generator/__init__.pysrc/datamodel_code_generator/__main__.pysrc/datamodel_code_generator/arguments.pysrc/datamodel_code_generator/cli_options.pysrc/datamodel_code_generator/parser/base.pysrc/datamodel_code_generator/parser/graphql.pysrc/datamodel_code_generator/parser/jsonschema.pysrc/datamodel_code_generator/parser/openapi.pytests/data/expected/main/json/json_collapse_reuse_model.pytests/main/test_main_json.py
🧰 Additional context used
🧬 Code graph analysis (2)
src/datamodel_code_generator/parser/base.py (1)
src/datamodel_code_generator/model/enum.py (1)
Enum(39-121)
tests/main/test_main_json.py (2)
tests/main/conftest.py (2)
output_file(98-100)run_main_and_assert(244-408)tests/test_main_kr.py (1)
output_file(44-46)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: combine coverage
- GitHub Check: benchmarks
🔇 Additional comments (13)
src/datamodel_code_generator/cli_options.py (1)
72-83: New CLI metadata entry is consistent
"--collapse-reuse-models"is correctly added as a model customization option alongside--collapse-root-models; naming and categorization look consistent with existing entries.src/datamodel_code_generator/parser/graphql.py (1)
167-196: Correctly threadscollapse_reuse_modelsinto GraphQL parserThe new
collapse_reuse_modelsparameter is added with a safe default and forwarded to the baseParserin the same slot ascollapse_root_models, so existing behavior is preserved while enabling the new feature.Also applies to: 265-293
docs/cli-reference/model-customization.md (1)
11-12:--collapse-reuse-modelsdocumentation is clear and aligned with behaviorThe option is listed in the overview table and documented with a concrete example that accurately illustrates collapsing duplicate models when combined with
--reuse-model; anchors and cross-references are consistent with the rest of the page.Also applies to: 1204-1272
docs/cli-reference/index.md (1)
14-17: Index updated consistently for new optionThe Model Customization option count and alphabetical “C” section correctly include
--collapse-reuse-modelswith a link to its detailed docs; index structure remains coherent.Also applies to: 44-45
src/datamodel_code_generator/parser/openapi.py (1)
253-280: OpenAPI parser correctly wirescollapse_reuse_modelsthrough to base parserThe new
collapse_reuse_modelsflag is added to the constructor with a default and passed through toJsonSchemaParser.__init__, matching the existing pattern aroundcollapse_root_models/skip_root_modeland keeping backward compatibility.Also applies to: 351-378
docs/cli-reference/quick-reference.md (1)
80-81: Quick reference entries for--collapse-reuse-modelsare accurateThe new option is integrated into both the Model Customization table and the alphabetical index with descriptions that match the main documentation and correct anchor links.
Also applies to: 184-185
src/datamodel_code_generator/arguments.py (1)
172-178: New--collapse-reuse-modelsflag wiring looks correctArgument wiring (store_true, default=None, help text, grouping under model customization) is consistent with existing options like
--collapse-root-modelsand will integrate cleanly withConfig.merge_args.src/datamodel_code_generator/__init__.py (1)
455-456: Publicgenerate()API extension is safe and correctly forwardedAdding
collapse_reuse_modelsas a keyword-only parameter (with a default) keeps thegenerate()API backward compatible, and the parser construction now forwards it viacollapse_reuse_models=collapse_reuse_models, so parsers can honor the new behavior.Also applies to: 705-707
tests/data/expected/main/json/json_collapse_reuse_model.py (1)
1-23: Golden output correctly reflects collapsed reuse behaviorThe expected models (reusing
ArmRightfor bothArm_RightandArm_Left, and avoiding empty subclasses) match the intended--collapse-reuse-modelssemantics and follow existing header/style conventions for golden files.src/datamodel_code_generator/parser/jsonschema.py (1)
582-583: JsonSchemaParser correctly forwardscollapse_reuse_modelsto the base parserThe new
collapse_reuse_modelsparameter onJsonSchemaParser.__init__and its forwarding in thesuper().__init__call are consistent with the rest of the constructor, and keep existing direct usages ofJsonSchemaParserbackward compatible.Also applies to: 679-681
src/datamodel_code_generator/parser/base.py (3)
745-745: LGTM! Clean parameter addition.The new
collapse_reuse_modelsparameter is well-positioned, properly typed, and has a sensible default that preserves backward compatibility.
912-912: LGTM! Proper instance variable storage.The parameter is correctly stored as an instance variable following the established pattern.
1579-1582: LGTM! Consistent implementation for tree-scope reuse.The logic correctly extends the collapsing behavior to tree-scope reuse (shared modules), maintaining consistency with the single-module reuse implementation. The three-part condition properly handles Enums, non-inheritable types, and user-requested collapsing.
CodSpeed Performance ReportMerging #2731 will not alter performanceComparing Summary
Footnotes
|
Fixes: #1443
Summary by CodeRabbit
New Features
--collapse-reuse-modelsCLI option to collapse duplicate models by replacing references instead of creating empty inheritance subclasses when used with--reuse-model.Documentation
--collapse-reuse-modelsoption (index counts and alphabetical listing adjusted).Tests
--reuse-modeland--collapse-reuse-modelsare used together.✏️ Tip: You can customize this high-level summary in your review settings.