Conversation
📝 WalkthroughWalkthroughRefines JSON Schema parsing: excludes "title" from base-object merges, broadens title-driven inline type aliasing (including primitives and pattern/propertyName cases), and fixes path resolution for nested Changes
Sequence Diagram(s)(omitted — changes are internal parser logic without multi-component runtime interactions requiring a sequence diagram) Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Possibly related PRs
Suggested labels
Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
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 |
|
📚 Docs Preview: https://pr-2891.datamodel-code-generator.pages.dev |
CodSpeed Performance ReportMerging #2891 will not alter performanceComparing
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/datamodel_code_generator/parser/jsonschema.py (1)
2639-2686: Clean up unused noqa directive.The
# noqa: PLR0911directive on line 2639 is unused since PLR0911 (too-many-return-statements) is not enabled in the Ruff configuration.🔎 Suggested fix
- def _should_create_type_alias_for_title( # noqa: PLR0911 - self, item: JsonSchemaObject, name: str - ) -> bool: + def _should_create_type_alias_for_title( + self, item: JsonSchemaObject, name: str + ) -> bool:
Expanded type alias logic looks good.
The additions correctly identify primitive types (lines 2676-2686) and enum literals (lines 2670-2675) as candidates for type alias generation when they have titles. This aligns well with the PR objective to create type aliases for nested titled elements.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
tests/data/jsonschema/use_title_as_name_nested_titles.jsonis excluded by!tests/data/**/*.jsonand included by none
📒 Files selected for processing (4)
src/datamodel_code_generator/parser/jsonschema.pytests/data/expected/main/jsonschema/use_title_as_name_nested_titles.pytests/data/expected/main/jsonschema/use_title_as_name_nested_titles_pydantic.pytests/main/jsonschema/test_main_jsonschema.py
🧰 Additional context used
🧬 Code graph analysis (3)
tests/data/expected/main/jsonschema/use_title_as_name_nested_titles_pydantic.py (1)
tests/data/expected/main/jsonschema/use_title_as_name_nested_titles.py (1)
Foo(42-47)
tests/data/expected/main/jsonschema/use_title_as_name_nested_titles.py (1)
src/datamodel_code_generator/model/typed_dict.py (1)
TypedDict(49-114)
src/datamodel_code_generator/parser/jsonschema.py (2)
src/datamodel_code_generator/util.py (1)
model_dump(254-258)src/datamodel_code_generator/parser/base.py (1)
get_special_path(293-295)
🪛 Ruff (0.14.10)
tests/data/expected/main/jsonschema/use_title_as_name_nested_titles.py
9-9: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
12-12: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
15-15: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
18-18: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
21-21: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
24-24: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
27-27: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
30-30: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
33-33: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
36-36: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
39-39: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
src/datamodel_code_generator/parser/jsonschema.py
2639-2639: Unused noqa directive (non-enabled: PLR0911)
Remove unused noqa directive
(RUF100)
⏰ 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)
- GitHub Check: py312-isort7 on Ubuntu
- GitHub Check: 3.12 on Windows
- GitHub Check: 3.10 on Windows
- GitHub Check: 3.11 on Windows
- GitHub Check: 3.13 on Windows
- GitHub Check: 3.11 on macOS
- GitHub Check: 3.14 on Windows
- GitHub Check: 3.10 on macOS
- GitHub Check: benchmarks
- GitHub Check: Analyze (python)
🔇 Additional comments (6)
src/datamodel_code_generator/parser/jsonschema.py (3)
1894-1894: LGTM! Excluding "title" enables proper type alias generation for nested elements.Excluding "title" from the base_object merge prevents the parent schema's title from being propagated to child schemas in combined schemas (anyOf/oneOf). This ensures that each branch can use its own title for type alias generation when
use_title_as_nameis enabled.
2780-2786: LGTM! Improved path resolution for additionalProperties.Using
get_special_path("additionalProperties", object_path)ensures consistent path marking for internal reference tracking, which improves the correctness of nested type resolution for dict types with additionalProperties.
3002-3004: LGTM! Consistent path handling.This mirrors the improvement made in
parse_item, ensuring thatadditionalPropertiespaths are consistently marked usingget_special_pathacross different parsing methods.tests/main/jsonschema/test_main_jsonschema.py (1)
3107-3154: Nested use_title_as_name tests correctly mirror existing inline-types coverageBoth new tests reuse the same CLI pattern and BLACK_PY313_SKIP gating as the existing
use_title_as_name_inline_typestests, but point at the nested-titles schema and goldens. They exercise TypedDict and Pydantic v2 outputs with the right flags (--use-title-as-name, union operator, standard collections, skip-root-model where appropriate), so they should reliably lock in the intended behavior for titled nested items/additionalProperties/oneOf-anyOf branches.tests/data/expected/main/jsonschema/use_title_as_name_nested_titles.py (1)
1-47: TypedDict golden cleanly models nested titled aliases and Foo referencesThe alias ladder (
MyArrayItem→MyArray,MyObjectProp→MyObject,MyOneOfBranch/MyAnyOfBranch→ unions, nested array aliases, etc.) is consistent and keeps Foo’s fields pointing only at named types, which matches the new test’s intent for nested titled elements. UsingNotRequired[...]and modernlist[...]/dict[...]forms is appropriate for the 3.13-targeted output this golden represents; just ensure your lint configuration either targets ≥3.12 or excludes these expected files if Ruff is run with an older Python target.tests/data/expected/main/jsonschema/use_title_as_name_nested_titles_pydantic.py (1)
1-65: Pydantic v2 golden correctly promotes nested titled schemas to named RootModelsEach titled nested schema is represented as its own
RootModel[...]with a titledrootfield, andFoo’s optional fields reference those models rather than inlined types, mirroring the TypedDict golden and satisfying the nested use-title-as-name behavior under test. The unions and container shapes align with the issue description and existing pydantic_v2 expectations.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2891 +/- ##
=======================================
Coverage 99.37% 99.37%
=======================================
Files 92 92
Lines 16152 16165 +13
Branches 1906 1909 +3
=======================================
+ Hits 16051 16064 +13
Misses 52 52
Partials 49 49
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: 0
🧹 Nitpick comments (1)
src/datamodel_code_generator/parser/jsonschema.py (1)
2639-2690: Expanded aliasing logic is correct, but clean up unused noqa directive.The expanded
_should_create_type_alias_for_titlecorrectly handles:
- patternProperties and propertyNames cases
- Primitive types with titles
- Enum as literal scenarios
However, the
# noqa: PLR0911comment is flagged as unused by Ruff (PLR0911 is not enabled in your configuration).🔎 Proposed cleanup
- def _should_create_type_alias_for_title( # noqa: PLR0911 - self, item: JsonSchemaObject, name: str - ) -> bool: + def _should_create_type_alias_for_title( + self, item: JsonSchemaObject, name: str + ) -> bool:
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
tests/data/jsonschema/use_title_as_name_nested_titles.jsonis excluded by!tests/data/**/*.jsonand included by none
📒 Files selected for processing (3)
src/datamodel_code_generator/parser/jsonschema.pytests/data/expected/main/jsonschema/use_title_as_name_nested_titles.pytests/data/expected/main/jsonschema/use_title_as_name_nested_titles_pydantic.py
🧰 Additional context used
🧬 Code graph analysis (2)
tests/data/expected/main/jsonschema/use_title_as_name_nested_titles_pydantic.py (2)
tests/data/expected/parser/openapi/openapi_parser_parse_modular/bar.py (1)
Field(6-7)tests/data/expected/main/jsonschema/use_title_as_name_nested_titles.py (1)
Foo(54-61)
tests/data/expected/main/jsonschema/use_title_as_name_nested_titles.py (1)
src/datamodel_code_generator/model/typed_dict.py (1)
TypedDict(49-114)
🪛 Ruff (0.14.10)
src/datamodel_code_generator/parser/jsonschema.py
2639-2639: Unused noqa directive (non-enabled: PLR0911)
Remove unused noqa directive
(RUF100)
tests/data/expected/main/jsonschema/use_title_as_name_nested_titles.py
9-9: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
12-12: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
15-15: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
18-18: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
21-21: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
24-24: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
27-27: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
30-30: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
33-33: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
36-36: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
39-39: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
42-42: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
45-45: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
48-48: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
51-51: Cannot use type alias statement on Python 3.10 (syntax was added in Python 3.12)
(invalid-syntax)
⏰ 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). (16)
- GitHub Check: py312-isort7 on Ubuntu
- GitHub Check: 3.10 on macOS
- GitHub Check: py312-pydantic1 on Ubuntu
- GitHub Check: 3.12 on Windows
- GitHub Check: 3.13 on macOS
- GitHub Check: 3.14 on Ubuntu
- GitHub Check: 3.12 on macOS
- GitHub Check: 3.10 on Windows
- GitHub Check: 3.14 on Windows
- GitHub Check: 3.11 on Windows
- GitHub Check: 3.14 on macOS
- GitHub Check: 3.11 on Ubuntu
- GitHub Check: 3.13 on Windows
- GitHub Check: 3.13 on Ubuntu
- GitHub Check: benchmarks
- GitHub Check: Analyze (python)
🔇 Additional comments (5)
tests/data/expected/main/jsonschema/use_title_as_name_nested_titles_pydantic.py (1)
1-87: LGTM! Test expectation file structure is correct.The Pydantic model definitions follow the expected patterns:
- RootModel usage for type aliases is appropriate
- Field titles are consistently applied
- Pattern constraints using
constrare correctly specified- The Foo model properly references all nested type aliases
This correctly validates the expanded use-title-as-name functionality for patternProperties and propertyNames.
src/datamodel_code_generator/parser/jsonschema.py (3)
1894-1894: Good refinement to exclude title from base object merges.Excluding "title" when creating the base_object for combined schemas prevents the parent's title from leaking into child schemas during anyOf/oneOf/allOf merging. This ensures titles are only used for their intended purpose in the aliasing logic.
2784-2790: Improved path resolution for nested additionalProperties.Using
get_special_path("additionalProperties", object_path)ensures consistent special path formatting and proper nesting hierarchy for dict types derived from additionalProperties. This aligns with how other special paths like "array" and "enum" are handled throughout the parser.
3006-3010: Consistent path resolution applied to root-level additionalProperties.This change mirrors the fix at lines 2784-2790, applying the same improved path resolution using
get_special_path("additionalProperties", path)for root-level dict types. Maintains consistency across the parser's handling of additionalProperties.tests/data/expected/main/jsonschema/use_title_as_name_nested_titles.py (1)
1-61: LGTM! Test expectation file correctly uses modern Python syntax.The type aliases and TypedDict structure correctly validate the expanded use-title-as-name functionality:
- Modern
typestatement syntax (PEP 695, Python 3.12+)- Proper TypedDict with NotRequired fields
- Consistent naming from schema titles
The Ruff warnings about Python 3.12 syntax are expected for this generated test expectation file—the generator respects
target_python_versionconfiguration, and this output represents the expected result when targeting Python 3.12+.
Breaking Change AnalysisResult: Breaking changes detected Reasoning: This PR changes code generation behavior when Content for Release NotesCode Generation Changes
This analysis was performed by Claude Code Action |
|
🎉 Released in 0.52.0 This PR is now available in the latest release. See the release notes for details. |
Fixes: #2887
Summary by CodeRabbit
Bug Fixes
New Features
Tests
✏️ Tip: You can customize this high-level summary in your review settings.