Skip to content

Keep one request-response class for models that reach a readOnly model only through map keywords they do not type - #5275

Merged
Koudai Aono (koxudaxi) merged 1 commit into
fix-5238-rw-root-propertiesfrom
fix-5237-rw-untyped-maps
Oct 24, 2026
Merged

Koudai Aono (koxudaxi) merged 1 commit into
fix-5238-rw-root-propertiesfrom
fix-5237-rw-untyped-maps

Conversation

@koxudaxi

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

Copy link
Copy Markdown
Member

Fixes: #5237

Summary by CodeRabbit

  • Bug Fixes
    • Improved JSON Schema generation for read-only and write-only fields referenced through map-related keywords and composed schemas.
    • Corrected request and response model splitting for typed maps while keeping models with ordinary properties appropriately combined.
    • Updated schema-graph diagnostics for models that are not split despite references to their variants.
  • Tests
    • Added cross-backend coverage for generated models and validation of valid and invalid payloads.

@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 b2a6c8907ebca7b5381af7ebb739f0119b578b77 and 05243ec04ba503259cea710a05d84bab6c665e32.

⛔ Files ignored due to path filters: 3
  • tests/data/expected/main/jsonschema/main_jsonschema/schema_graph_check_of_read_write_splits[report].report.txt is excluded by !tests/data/**/*.txt and included by none
  • tests/data/jsonschema/read_only_write_only_custom_type_path.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/jsonschema/read_only_write_only_untyped_map_keywords.json is excluded by !tests/data/**/*.json and included by none
📒 Files selected for processing: 8
  • src/datamodel_code_generator/parser/jsonschema.py
  • tests/data/expected/main/jsonschema/read_only_write_only_untyped_map_keywords.py
  • tests/data/expected/main/jsonschema/read_only_write_only_untyped_map_keywords~main_read_only_write_only_untyped_map_keywords[dedupe-msgspec.Struct].py
  • tests/data/expected/main/jsonschema/read_only_write_only_untyped_map_keywords~main_read_only_write_only_untyped_map_keywords[dedupe-pydantic_v2.BaseModel].py
  • tests/data/expected/main/jsonschema/read_only_write_only_untyped_map_keywords~main_read_only_write_only_untyped_map_keywords[dedupe-pydantic_v2.dataclass].py
  • tests/data/expected/main/jsonschema/read_only_write_only_untyped_map_keywords~main_read_only_write_only_untyped_map_keywords[dedupe-typing.TypedDict].py
  • tests/main/jsonschema/test_main_jsonschema.py
  • tests/main/jsonschema/test_main_jsonschema.refs

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

📝 Walkthrough

The read/write reference walk now distinguishes schemas that generate map types from schemas where map keywords do not type fields. The change also updates allOf mapping classification and adds cross-backend request-response tests and generated output snapshots.

Changes: Read/write map reference handling

Layer / File(s) Summary
Map-shape classification and reference traversal
src/datamodel_code_generator/parser/jsonschema.py
The reference walk uses merged schemas and map-model status to decide which map-keyword references contribute request-response variants. A shared allOf mapping-item predicate is also used in root-schema merging.
Generated request-response models
tests/data/expected/main/jsonschema/read_only_write_only_untyped_map_keywords*
Expected outputs cover patterned and additional-property maps, property-bearing and allOf models, recursive nodes, and aggregate request and response models across backends.
Cross-backend behavior tests
tests/main/jsonschema/test_main_jsonschema.py, tests/main/jsonschema/test_main_jsonschema.refs
Parameterized tests validate schema and generated-model acceptance across five backends, with and without deduplication. Snapshot references cover the generated outputs. The schema-graph fixture now uses a custom type path.

Priority: ➖ Normal

Estimated code review effort: 4 (Complex) | ~40 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • #5239: Concerns request-response variants for an allOf that references a root map, which is related to the allOf and map classification changes.
  • #5268: Concerns request-response split decisions for a $ref with customTypePath; this PR also adjusts reference-graph decisions, though the custom-type-path case remains distinct.
  • #5267: Concerns request-response variants and field construction for an allOf that names a root map, related to the allOf and map analysis changes.

Possibly related PRs

Merge Risk: ⚪ Minimal · up to 05243

No issue introduced by this change remains identified; it is ready for normal merge 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 accurately describes the main change: keeping one request-response class when read-only models are reached only through untyped map keywords.
Linked Issues check Passed Directly linked issue #5237 has only coding requirements. It requires successful generation with --read-only-write-only-model-type request-response for four shapes (properties beside a $ref to a patte…
Out of Scope Changes check Passed All changes connect to the #5237 objective. The jsonschema.py change updates the read/write reference walk and allOf mapping classification, which is the code path that causes the reported failures. T…
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 5…
Full details: Linked Issues check

Explanation

Directly linked issue #5237 has only coding requirements. It requires successful generation with --read-only-write-only-model-type request-response for four shapes (properties beside a $ref to a patternProperties map; properties beside a $ref to an additionalProperties map; additionalProperties beside properties in an inline allOf member; additionalProperties beside an allOf with no properties) across five backends, with and without --dedupe-request-response-variants. It also requires that a model reaching F only through an untyped map keyword stays one class, or gets generated variants, and that only F and typed maps split.

Evidence from the PR summary: jsonschema.py changes the read/write reference walk so map keywords contribute types only when the merged schema is a map. Shape 3 and shape 4 classification is handled by _is_all_of_mapping_item. New expected outputs for read_only_write_only_untyped_map_keywords cover the five backends (dataclasses, msgspec, pydantic_v2.BaseModel, pydantic_v2.dataclass, typing.TypedDict) in dedupe and default modes. Untyped shapes stay single classes (PatternedWithProperties, MemberExtra, MemberExtraBesideBase, PatternedBesideBase, ExtraBesideBase in dataclasses). Typed maps (PatternedRequest/Response, ExtraRequest/Response, MemberMapRequest/Response, BoundedExtra) split. Split of ExtraWithProperties and ExtraBesideBase in TypedDict and pydantic BaseModel outputs is tied to typed additional properties, which the issue allows.

Limits: no repository inspection was available. The input fixture tests/data/jsonschema/read_only_write_only_untyped_map_keywords.json is in ignored_files, so its content was not verified against the issue schemas. The pass result relies on the generated expected outputs and the test summary. Test execution results were not available.

Full details: Out of Scope Changes check

Explanation

All changes connect to the #5237 objective. The jsonschema.py change updates the read/write reference walk and allOf mapping classification, which is the code path that causes the reported failures. The new expected-output files and the test_main_jsonschema.py test exercise the four issue shapes across the five backends. The test_main_jsonschema.refs snapshot group registers those outputs. The schema-graph test change replaces the additionalProperties-in-inline-allOf split trigger with a customTypePath reference. This follows from shape 3 no longer splitting, and the commit message states that customTypePath splitting is still covered. No unrelated feature, refactor, or documentation change was identified in the summary.

Limits: the ignored report file and fixture JSON files were not inspected, as required by ignored_files. This assessment relies on the summary for them.

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 5 files. (3 skipped: 1 unsupported, 2 too large.)

✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • 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

chatgpt-codex-connector Bot commented Oct 23, 2026 •

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 ✅ Completed 2026-10-23T23:47:05.698356Z 05243ec 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 05243ec04b

ℹ️ About Codex in GitHub

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

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "Codex (@codex) review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "Codex (@codex) address that feedback".

@github-actions

Copy link
Copy Markdown
Contributor

@codspeed

codspeed Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 12.22%

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

❌ 4 regressed benchmarks
✅ 46 untouched benchmarks
⏩ 98 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_perf_load_yaml_openapi_large 33.3 ms 38.8 ms -13.97%
❌ test_perf_unique_items_schema_validators 2.4 s 2.8 s -13.69%
❌ test_perf_multiple_files_input 4.2 s 4.7 s -10.67%
❌ test_perf_openapi_large_pydantic_v2_builtin 1.5 s 1.7 s -10.48%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing fix-5237-rw-untyped-maps (ffa677b) with fix-5238-rw-root-properties (f19ef34)

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

@codecov

codecov Bot commented Oct 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…l only through map keywords they do not type

Under --read-only-write-only-model-type request-response the schema graph
decided that a model needs variants from keywords its model does not type, so
the model kept one class while the references to it read Request/Response
variants that were never generated, and generation failed with "A Parser can not
resolve classes":

- A $ref with properties beside it merges the map it names into a model with
  fields, which types none of the map's patternProperties or
  additionalProperties, but the graph read the reference to the map as a
  reference of the model.
- The additionalProperties of an inline allOf member, or beside an allOf that
  builds a model with fields or bases, type nothing there (beside fields only
  pydantic v2 BaseModel and TypedDict write their type), but the graph read them
  wherever the member or the schema declared no properties of its own, and for
  pydantic v2 BaseModel and TypedDict in every inline member.

The graph now decides with the rule the allOf parse applies (an allOf merges
into one map only where every item is an object map), and where a $ref names a
map that the merged schema does not build, it reads the merged schema in place
of the reference: those models keep one class, as without the option, and only
the readOnly model and the maps that type it split.

The schema graph check test reads a schema whose customTypePath types a
reference, which still splits this way.

Fixes: #5237
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: The PR changes only internal private helpers in the JSON Schema parser. It replaces _merged_ref_model_schema with _merged_ref_model_schemas and adds _schema_builds_map_model, _has_map_keywords and _is_all_of_mapping_item. These are used by the read/write schema graph walk that decides request/response splits under the opt-in --dedupe-request-response-variants flag. The fix stops the walk from treating map keywords as typing anything when the generated model does not type them. That covers patternProperties, propertyNames and additionalProperties beside properties or an allOf that builds a model with fields. As a result, those models keep one class under dedupe, just as they already did without the flag. The new tests check that the dedupe and default runs give identical output for every output model type, so default behavior is unchanged. Moving the allOf mapping-item check into _is_all_of_mapping_item is a pure refactor with the same logic. The schema graph check test now uses a customTypePath fixture only because the old fixture no longer triggers the mismatch, which reflects the bug fix rather than a change in how errors are reported. No CLI options, public API, templates, defaults or Python version support were removed or changed.


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

1 participant