Skip to content

Generate request-response models where patternProperties sit beside a $ref or an allOf wraps a map - #5242

Merged
Koudai Aono (koxudaxi) merged 1 commit into
fix-5203-mapped-fragmentfrom
fix-5208-rw-merged-maps
Oct 23, 2026
Merged

Koudai Aono (koxudaxi) merged 1 commit into
fix-5203-mapped-fragmentfrom
fix-5208-rw-merged-maps

Conversation

@koxudaxi

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

Copy link
Copy Markdown
Member

Fixes: #5208

Summary by CodeRabbit

  • Bug Fixes
    • Improved request and response model generation for schemas that combine references with map-key constraints or inherited schemas.
    • Corrected handling of maps with patterned or named keys alongside references, including additional-property maps within composed schemas.
    • Improved consistency of generated models with and without request/response variant deduplication.

@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 e17b577 and 64ca574d5c45a0fd5372fbb979707797ca438dca.

⛔ 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_additional_properties_allof_member.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/jsonschema/read_only_write_only_pattern_properties_allof_map.json is excluded by !tests/data/**/*.json and included by none
📒 Files selected for processing: 7
  • src/datamodel_code_generator/parser/jsonschema.py
  • tests/data/expected/main/jsonschema/read_only_write_only_map_keywords_beside_ref_or_base.py
  • tests/data/expected/main/jsonschema/read_only_write_only_map_keywords_beside_ref_or_base~main_read_only_write_only_map_keywords_beside_ref_or_base[allof_map-default].py
  • tests/data/expected/main/jsonschema/read_only_write_only_map_keywords_beside_ref_or_base~main_read_only_write_only_map_keywords_beside_ref_or_base[ref_sibling-dedupe].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; 0 remain after this review.

📝 Walkthrough

The parser updates request/response variant detection for allOf bases and reference-graph classification for map keywords beside $ref. Tests cover generated models, schema validation, and payload validation for these cases.

Changes: Request-response map handling

Layer / File(s) Summary
allOf request variant detection
src/datamodel_code_generator/parser/jsonschema.py
Variant detection now checks whether any allOf base reference generates a Request variant.
Merged reference map classification
src/datamodel_code_generator/parser/jsonschema.py
The reference scan uses the merged schema when patternProperties or propertyNames accompanies a $ref, and uses its properties and allOf to classify the schema.
Generated model and validation coverage
tests/main/jsonschema/test_main_jsonschema.py, tests/data/expected/main/jsonschema/read_only_write_only_map_keywords_beside_ref_or_base*, tests/main/jsonschema/test_main_jsonschema.refs, tests/main/test_payload_validation.refs
Tests and snapshots cover request/response models for reference-sibling and allOf map schemas, with deduplication enabled and disabled. Payload-validation snapshots include the additionalProperties allOf-member fixture.

Priority: ➖ Normal

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

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • #5237 — Reports the same class-resolution failure for map keywords beside $ref and in allOf.
  • #5239 — Concerns request/response variants for an allOf model that names a map.
  • #5097 — Reports unresolved references when patternProperties or propertyNames reaches a read-only model through allOf.
  • #5209 — Reports the related class-resolution failure for additionalProperties reaching a read-only model.

Possibly related PRs

Merge Risk: ⚪ Minimal · up to 64ca5

This change lets request-response generation succeed for patternProperties beside a $ref and for allOf-wrapped maps. No new merge-blocking risk was identified.

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 summarizes the main changes: generating request-response models for patternProperties beside a $ref and maps wrapped in allOf.
Linked Issues check Passed The changes address open issue #5208. jsonschema.py now uses the merged schema for map-keyword handling when patternProperties or propertyNames are siblings of a $ref. It also detects request …
Out of Scope Changes check Passed The source change, generated snapshots, test cases, schema-graph regression coverage, and payload-validation references support the request/response generation fix in #5208. The additional `additional…
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 3…
Full details: Linked Issues check

Explanation

The changes address open issue #5208. jsonschema.py now uses the merged schema for map-keyword handling when patternProperties or propertyNames are siblings of a $ref. It also detects request variants when an allOf base reference generates one. The added request/response cases cover the sibling and allOf forms, including deduplication variants. The generated snapshots show that only the read-only model splits while map-reachable models receive valid request/response output.

Full details: Out of Scope Changes check

Explanation

The source change, generated snapshots, test cases, schema-graph regression coverage, and payload-validation references support the request/response generation fix in #5208. The additional additionalProperties case exercises the same reference-graph and inline-allOf handling. No unrelated product behavior is established by the reviewed summary.

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 3 files. (4 skipped: 2 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-23T16:43:48.005900Z 64ca574 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 improve performance by 41.84%

2 warnings

⚠️ 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

⚠️ Base and head ran on different runner types

Some benchmarks with significant performance changes were compared across different runner types, so the changes might not come from the code. Run base and head on the same runner type, or use CodSpeed Macro Runners for more stable results.

Learn more about Macro Runners

Open the report in CodSpeed to investigate

⚡ 50 improved benchmarks
⏩ 98 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_perf_client_calls[sync] 42.3 ms 23.9 ms +76.84%
⚡ test_perf_client_calls[async] 46.9 ms 26.8 ms +75.17%
⚡ test_perf_load_pyproject_config[mapping-None] 369.3 µs 229.9 µs +60.65%
⚡ test_perf_load_pyproject_config[config-None] 362.9 µs 227 µs +59.88%
⚡ test_perf_load_pyproject_config[mapping-multiple] 377.5 µs 239.2 µs +57.82%
⚡ test_perf_load_pyproject_config[config-multiple] 376.3 µs 239.3 µs +57.26%
⚡ test_perf_recommend_options_full_kubernetes_style 21.8 s 14.5 s +51.14%
⚡ test_perf_local_reference_file_resolution 175.4 ms 118.9 ms +47.5%
⚡ test_perf_extreme_large_schema 17.9 s 12.2 s +46.31%
⚡ test_perf_scalar_root_collapsed_builtin 227.9 ms 155.8 ms +46.27%
⚡ test_perf_multiple_files_input 4.2 s 2.9 s +45.95%
⚡ test_perf_graphql_style_pydantic_v2 933.2 ms 642.6 ms +45.21%
⚡ test_perf_complex_refs 2.4 s 1.7 s +44.87%
⚡ test_perf_large_models_typed_dict[total-false-requiredness] 1,311.5 ms 907.9 ms +44.46%
⚡ test_perf_all_options_enabled 7.2 s 5 s +44.31%
⚡ test_perf_large_models_pydantic_v2_noformat 1.5 s 1 s +44.13%
⚡ test_perf_kubernetes_style_pydantic_v2 3 s 2.1 s +44.05%
⚡ test_perf_deep_nested 6.3 s 4.4 s +43.95%
⚡ test_perf_inherited_required_fields[partial-derived-first] 1,168.9 ms 813.6 ms +43.68%
⚡ test_perf_large_models_pydantic_v2_builtin_double_quotes 1.6 s 1.1 s +43.65%
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing fix-5208-rw-merged-maps (99795c9) with fix-5203-mapped-fragment (e17b577)

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 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

… $ref or an allOf wraps a map

Under --read-only-write-only-model-type request-response the schema graph read
the patternProperties and propertyNames beside a $ref by the keywords written
beside them alone. The model of such a schema is built from the $ref merged
with them, and where the target brings properties or allOf its fields hold the
properties alone, so the map keywords type nothing there. Such a model did not
split while the references to it read variants that were never generated, and
generation failed with "A Parser can not resolve classes". The graph now
decides what those keywords type from the merged schema: the model and the
models inheriting from it or referencing it keep one class, as without the
option, and only the readOnly model splits.

A model whose allOf names a map, written as a root model, kept one class while
the map split into variants, so it inherited from a model that was never
generated and generation failed the same way. A model now also splits where the
references to one of its allOf bases read variants; its variants hold the
fields the model holds without the option.

The schema graph check test reads a schema whose additionalProperties sit in an
inline allOf member, which still splits this way.

Fixes: #5208
@koxudaxi
Koudai Aono (koxudaxi) merged commit f08d1d5 into main Oct 23, 2026
26 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the fix-5208-rw-merged-maps branch October 23, 2026 19:11
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: The PR is a narrow bug fix to the read-only/write-only request-response split logic in the JSON Schema parser. It does two things. First, _splits_into_rw_model_variants now also splits a model whose allOf base generates a Request variant, such as a map root model, because those variants replace the base model and nothing could otherwise inherit from it. Second, _get_ref_schema_rw_model_reference_facts now judges patternProperties and propertyNames placed beside a $ref outside allOf against the $ref merged with those keywords, using the new private helper _merged_ref_model_schema. That means map keywords merged into a schema that has properties no longer count as reading variants. Before this change, both cases produced an inconsistent schema graph: a model did not split while references to it read a variant, and the existing check and report test shows these failing with errors such as can not resolve classes. The fix makes them generate valid, consistent models. The existing schema-graph check test was moved to a new fixture, but it still checks the same message format and error behavior. No CLI options, Python API, templates, defaults, or supported Python versions changed. Nothing public was removed: the deleted lines are only docstrings, one internal expression and expected report lines, and the only new code is a private helper. Output only changes for edge-case schemas that previously failed or could not be resolved, so the PR is not breaking.


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.

request-response: patternProperties beside a $ref or under an allOf that reach a readOnly model fail with 'can not resolve classes'

1 participant