Skip to content

Fix merged result in parse_item not passed back to parse_object_fields#2997

Merged
koxudaxi merged 3 commits intomainfrom
fix/ref-merge-field-metadata
Feb 14, 2026
Merged

Fix merged result in parse_item not passed back to parse_object_fields#2997
koxudaxi merged 3 commits intomainfrom
fix/ref-merge-field-metadata

Conversation

@koxudaxi
Copy link
Owner

@koxudaxi koxudaxi commented Feb 14, 2026

Fixes: #2995

Summary by CodeRabbit

  • Bug Fixes

    • Corrected merging of referenced schemas with inline keywords so generated models honor combined metadata (readonly/writeOnly, constraints, defaults, descriptions/titles, nullability) for fields and additionalProperties.
    • Ensured parameter schemas in OpenAPI merges reflect combined constraints and defaults when references include extra keywords.
  • Tests

    • Added tests and expected outputs covering ref+metadata merging for JSON Schema and OpenAPI parameter scenarios.

@coderabbitai
Copy link

coderabbitai bot commented Feb 14, 2026

📝 Walkthrough

Walkthrough

Referenced schemas ($ref) combined with additional schema keywords are now merged before parsing; the merged schema is used for object properties, additionalProperties, and OpenAPI parameter parsing so field types and metadata reflect the merged result.

Changes

Cohort / File(s) Summary
JSON Schema Parser
src/datamodel_code_generator/parser/jsonschema.py
When an object property or additionalProperties contains a $ref plus schema-affecting keywords (and the ref is not nullable-only), merge the referenced schema with the local schema via _merge_ref_with_schema and use the merged schema for subsequent parsing.
OpenAPI Parser
src/datamodel_code_generator/parser/openapi.py
parse_all_parameters signature updated to accept path: list[str] and return `DataType
Generated Expected Outputs
tests/data/expected/main/jsonschema/ids/ContactPoint.py, tests/data/expected/main/jsonschema/ids/__init__.py, tests/data/expected/main/jsonschema/ref_merge_field_metadata.py, tests/data/expected/main/jsonschema/ref_merge_additional_properties.py, tests/data/expected/main/openapi/ref_merge_parameters.py
Added/updated expected model files to reflect merged metadata (added Field imports and Field(...) metadata: descriptions, titles, frozen/readOnly, constraints, defaults, nullable, and new RootModel/BaseModel shapes).
Tests
tests/main/jsonschema/test_main_jsonschema.py, tests/main/openapi/test_main_openapi.py
Added tests test_ref_merge_field_metadata, test_ref_merge_additional_properties, and test_ref_merge_parameters to assert that $ref merged with const/other keywords produces models with merged metadata.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

Suggested labels

breaking-change-analyzed

Poem

🐰 I nibbled at refs in the schema glade,
Merged bits and metadata, tidy and brave.
Fields now wear titles, constraints, and more,
Hopping through models—fresh code to adore.
🥕✨

🚥 Pre-merge checks | ✅ 6
✅ Passed checks (6 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title directly aligns with the main objective: fixing the merged result propagation from parse_item back to parse_object_fields, which is the core change described in issue #2995.
Linked Issues check ✅ Passed The PR implements all required objectives from issue #2995: merging $ref with schema keywords in parse_item, propagating merged results to parse_object_fields and related functions, and ensuring metadata reflects the merged schema.
Out of Scope Changes check ✅ Passed All changes directly support the primary objective of propagating merged schema results. Updates to jsonschema.py and openapi.py implement the core merge logic, test files validate the fix with appropriate test cases for both scenarios.
Docstring Coverage ✅ Passed Docstring coverage is 88.89% which is sufficient. The required threshold is 80.00%.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into main

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

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/ref-merge-field-metadata

No actionable comments were generated in the recent review. 🎉


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
Contributor

github-actions bot commented Feb 14, 2026

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

@codecov
Copy link

codecov bot commented Feb 14, 2026

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #2997   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           94        94           
  Lines        18081     18095   +14     
  Branches      2090      2093    +3     
=========================================
+ Hits         18081     18095   +14     
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 force-pushed the fix/ref-merge-field-metadata branch from 20c2d06 to a665752 Compare February 14, 2026 15:20
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/datamodel_code_generator/parser/openapi.py (1)

511-516: ⚠️ Potential issue | 🟡 Minor

Remove unused noqa on Line 511.
Ruff reports this as unused; keep the signature clean.

🧹 Suggested fix
-def parse_all_parameters(  # noqa: PLR0912, PLR0914, PLR0915
+def parse_all_parameters(

@koxudaxi koxudaxi merged commit f3ef9c6 into main Feb 14, 2026
36 of 37 checks passed
@koxudaxi koxudaxi deleted the fix/ref-merge-field-metadata branch February 14, 2026 15:49
@github-actions
Copy link
Contributor

Breaking Change Analysis

Result: Breaking changes detected

Reasoning: This PR fixes a bug where metadata from $ref-referenced schemas was not being merged when inline keywords were present. The fix changes generated code output - fields that previously had no Field() annotations now include Field(...) with description, title, constraints, etc. from the referenced schema. While this is a bug fix making output more accurate, it changes the generated code that users may depend on, affecting comparison tests, regenerated code, and potentially custom templates.

Content for Release Notes

Code Generation Changes

  • Fields using $ref with inline keywords now include merged metadata - When a schema property uses $ref alongside additional keywords (e.g., const, enum, readOnly, constraints), the generator now correctly merges metadata (description, title, constraints, defaults, readonly/writeOnly) from the referenced schema into the field definition. Previously, this metadata was lost. For example, a field like type: Type may now become type: Type = Field(..., description='Type of this object.', title='type') when the referenced schema includes those attributes. This also affects additionalProperties and OpenAPI parameter schemas. (Fix merged result in parse_item not passed back to parse_object_fields #2997)

This analysis was performed by Claude Code Action

@github-actions
Copy link
Contributor

🎉 Released in 0.54.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.

Merged result in parse_item not passed back to parse_object_fields

1 participant