Skip to content

Split a request-response model that re-declares the flagged property of an allOf ancestor - #5221

Merged
Koudai Aono (koxudaxi) merged 1 commit into
fix-5097-rw-mapsfrom
fix-5132-rw-redeclared
Oct 23, 2026
Merged

Koudai Aono (koxudaxi) merged 1 commit into
fix-5097-rw-mapsfrom
fix-5132-rw-redeclared

Conversation

@koxudaxi

@koxudaxi Koudai Aono (koxudaxi) commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Fixes: #5132

Summary by CodeRabbit

  • Bug Fixes

    • Improved request/response model generation for inherited read-only and write-only fields, including cases where child schemas redeclare those fields.
    • Generated request and response models now preserve the correct field behavior across inherited schemas and references.
  • Tests

    • Added coverage for inline and referenced schemas, with deduplication enabled and disabled.

@coderabbitai

coderabbitai Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
📥 Commits

Reviewing files that changed from the base of the PR and between 33d6b4e4f0d6f1272735b76b332c2b35e5ab870d and 0d3e071444150d53df91f3ea3a174ac0667576a8.

⛔ Files ignored due to path filters: 2
  • tests/data/jsonschema/read_only_write_only_redeclared_inherited_flag.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/jsonschema/read_only_write_only_redeclared_inherited_flag_inline.json is excluded by !tests/data/**/*.json and included by none
📒 Files selected for processing: 6
  • src/datamodel_code_generator/parser/jsonschema.py
  • tests/data/expected/main/jsonschema/read_only_write_only_redeclared_inherited_flag.py
  • tests/data/expected/main/jsonschema/read_only_write_only_redeclared_inherited_flag~main_read_only_write_only_redeclared_inherited_flag[dedupe-ref].py
  • tests/main/jsonschema/test_main_jsonschema.py
  • tests/main/jsonschema/test_main_jsonschema.refs
  • tests/main/test_payload_validation.refs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.

📝 Walkthrough

The parser now detects request-response splits when an allOf ancestor’s effective fields require variants, even if a child redeclares an inherited read-only property. Tests cover referenced and inline schemas, deduplication modes, generated models, and payload validation.

Changes: Inherited read/write flag handling

Layer / File(s) Summary
Detect splits from inherited fields
src/datamodel_code_generator/parser/jsonschema.py
A shared predicate checks effective fields for read-only, write-only, and request-variant references. In RequestResponse mode, the parser also checks linearized allOf ancestors.
Verify generated request-response variants
tests/main/jsonschema/test_main_jsonschema.py, tests/data/expected/main/jsonschema/read_only_write_only_redeclared_inherited_flag*.py, tests/main/jsonschema/test_main_jsonschema.refs, tests/main/test_payload_validation.refs
Tests cover $ref and inline re-declarations with deduplication enabled and disabled. Generated-model expectations and payload-validation references include the resulting variants.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

Merge Risk: ⚪ Minimal · up to 0d3e0

The request variant retains the re-declared field in the reviewed cases. No issue identified here prevents merging after normal checks.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and specifically describes the main change: splitting request-response models when an allOf ancestor has a re-declared flagged property.
Linked Issues check Passed [#5132] Requirement: with --read-only-write-only-model-type request-response, an allOf child that re-declares an inherited readOnly property without readOnly must generate request and response models.…
Out of Scope Changes check Passed All changes relate to #5132.
- parser/jsonschema.py: the split logic change and the helper extraction (_rw_model_fields_split, _splits_into_rw_model_variants) implement the ancestor-based split. The h…
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
Full details: Linked Issues check

Explanation

[#5132] Requirement: with --read-only-write-only-model-type request-response, an allOf child that re-declares an inherited readOnly property without readOnly must generate request and response models. The child's re-declared field must stay in the request variant, and the holder's child field must reference the request variant.

Evidence from the PR summary:

  • parser/jsonschema.py: split selection now checks the linearized allOf ancestors under RequestResponse mode. A model splits when an ancestor's effective fields split, even when the model's own declarations remove the inherited read/write flags. This addresses the root cause in the reported 'A Parser can not resolve classes' failure.
  • Expected output tests/data/expected/main/jsonschema/read_only_write_only_redeclared_inherited_flag.py: ChildRequest has 'id: int | None' and 'name: str | None'. ModelRequest has 'child: ChildRequest | None'. This matches the expected behavior in the issue.
  • Dedupe snapshot output (dedupe-ref variant) generates ChildRequest with 'id: Int | None', so the $ref re-declaration is retained.
  • tests/main/jsonschema/test_main_jsonschema.py adds a parametrized test covering $ref and inline re-declarations (inline with --allof-merge-mode none), with and without deduplication. It validates payloads against Draft 202012 and checks ModelRequest validation.

Uncertainty: the issue reports $ref failures under every --allof-merge-mode. The summary describes the test as covering the $ref case under the default merge mode, and the inline case under none. It does not state that every merge mode is exercised for the $ref case. The parser change is not merge-mode specific, so the fix should apply, but per-mode behavior was not directly verified. The two fixture files were excluded from review, so their exact content was not inspected.

Full details: Out of Scope Changes check

Explanation

All changes relate to #5132.

  • parser/jsonschema.py: the split logic change and the helper extraction (_rw_model_fields_split, _splits_into_rw_model_variants) implement the ancestor-based split. The helper extraction supports the same logic and does not add new behavior.
  • Expected output files (.py, dedupe and non-dedupe variants): these are generated results for the new fixtures. They show the parent, child, and holder variant shapes that the issue describes. Additional models such as GrandChild, Linked, and Relinked are fixture coverage for the same split rules.
  • test_main_jsonschema.py and the .refs snapshot entries: test coverage for the new behavior.
  • test_payload_validation.refs: adds two fixtures for the same read/write inheritance case.

No change was found that is unrelated to the request-response split of re-declared flagged properties.

Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2 files. (4 skipped: 2 unsupported, 2 too large.)

✨ Finishing Touches: 💡 1
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review 🔄 Running since 2026-10-23T14:07:51.408645Z 9af87ef PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

Copy link
Copy Markdown
Contributor

@codspeed

codspeed Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ Walltime results may be unreliable

Some benchmarks used the Walltime instrument outside CodSpeed Macro Runners, which can make the results inconsistent.

Learn more about Macro Runners

See affected benchmarks

✅ 50 untouched benchmarks
⏩ 98 skipped benchmarks1


Comparing fix-5132-rw-redeclared (038cdd0) with fix-5097-rw-maps (b261839)

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. ↩

…of an allOf ancestor

Under --read-only-write-only-model-type request-response a model split only
where its effective fields were flagged or referenced a model with variants. A
child that re-declares the inherited readOnly property (through a $ref, or
inline under --allof-merge-mode none), or the inherited reference to a model
with variants, has no such field, so it was written as a subclass of an
ancestor whose model the variants replace, while the fields referencing it read
variants that were never generated, and generation failed with "A Parser can
not resolve classes". Such a model now also splits where the effective fields
of an allOf ancestor split it; its variants keep the re-declared property, and
the fields referencing it read them.

Fixes: #5132
@codecov

codecov Bot commented Oct 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@koxudaxi
Koudai Aono (koxudaxi) merged commit b1cbd9f into main Oct 23, 2026
132 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the fix-5132-rw-redeclared branch October 23, 2026 16:07
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: The only source change is a refactor and bug fix in the JSON Schema parser's _get_separate_model_fields. The repeated split check is moved into two new private helpers, _rw_model_fields_split and _splits_into_rw_model_variants. With the request-response read-only/write-only model type, a model now also splits into Request and Response variants when one of its allOf ancestors splits, even if the model re-declares the inherited flagged property. Before this fix, such a model could not inherit from the ancestor, because the variants replace the ancestor's model, so the old output for these schemas was broken. The change only applies when the opt-in read-only-write-only-model-type option is set to request-response. No CLI options, public Python APIs, template variables, defaults or supported Python versions are changed or removed. The deleted lines are only the inline condition logic that moved into the helpers. Everything else in the PR is new test fixtures, expected outputs and test registrations. So this is a bug fix that makes previously invalid output correct, not a breaking change.


This analysis was produced by the release draft workflow

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.

--read-only-write-only-model-type request-response fails when an allOf child re-declares an inherited readOnly property

1 participant