Skip to content

Generate request-response models when only map keywords of a model reach a readOnly model - #5220

Merged
Koudai Aono (koxudaxi) merged 1 commit into
fix-5031-extra-itemsfrom
fix-5097-rw-maps
Oct 23, 2026
Merged

Koudai Aono (koxudaxi) merged 1 commit into
fix-5031-extra-itemsfrom
fix-5097-rw-maps

Conversation

@koxudaxi

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

Copy link
Copy Markdown
Member

Fixes: #5097

Summary by CodeRabbit

  • Bug Fixes

    • Improved JSON Schema handling for read-only and write-only fields in schemas combining maps, patterned properties, named keys, and inheritance. Generated models now better preserve request and response validation behavior.
  • Tests

    • Expanded coverage for these schema combinations, including validation against Draft 2020-12 behavior and cases with 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 57e70d8c4e8c9d2e44deac44356f6d7bbba06298 and 33d6b4e4f0d6f1272735b76b332c2b35e5ab870d.

⛔ Files ignored due to path filters: 4
  • 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_map_keywords_beside_allof.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/jsonschema/read_only_write_only_pattern_properties_ref_sibling.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/jsonschema/read_only_write_only_property_names_base.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_of_models.py
  • tests/data/expected/main/jsonschema/read_only_write_only_map_keywords_of_models~main_read_only_write_only_map_keywords_of_models[dedupe-pattern_properties].py
  • tests/data/expected/main/jsonschema/read_only_write_only_map_keywords_of_models~main_read_only_write_only_map_keywords_of_models[dedupe-property_names].py
  • tests/main/jsonschema/test_main_jsonschema.py
  • tests/main/jsonschema/test_main_jsonschema.refs
  • tests/parser/test_jsonschema.py

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

📝 Walkthrough

The JSON Schema read/write model traversal now carries merged-schema state and selects map-related schemas according to schema shape. Tests cover generated request/response variants across patternProperties, propertyNames, and allOf cases.

Changes: Request-response map schema traversal

Layer / File(s) Summary
Track map and merge context during traversal
src/datamodel_code_generator/parser/jsonschema.py
Child-schema traversal now tracks whether allOf members are merged. Map keywords are considered only for unmerged schemas without properties or allOf; propertyNames is skipped when patternProperties exists.
Validate request-response variants for map schemas
tests/main/jsonschema/test_main_jsonschema.py, tests/main/jsonschema/test_main_jsonschema.refs, tests/parser/test_jsonschema.py, tests/data/expected/main/jsonschema/read_only_write_only_map_keywords_of_models*
Parameterized tests and generated snapshots cover patternProperties, propertyNames, and allOf cases with deduplication enabled and disabled. The schema-graph check now uses a fixture with a patternProperties reference beside a $ref; the tuple-items test enables types_map.

Priority: ➖ Normal

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

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • 5208 — Reports unresolved request-response references for patternProperties beside a $ref or under allOf; this change updates traversal for those map-keyword paths.
  • 5210 — Reports a request-response reference problem involving patternProperties and an unsplit read-only model; this change updates map-keyword handling in model-variant traversal.

Possibly related PRs

Suggested labels: breaking-change-analyzed

Merge Risk: ⚪ Minimal · up to 33d6b

The change narrows which map keywords trigger request/response splitting and adds test coverage for the map-keyword cases. No new blocking risk was found; a known limitation with a $ref plus sibling patternProperties remains but was already present.

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: generating request-response models when map keywords reach a readOnly model.
Linked Issues check Passed The changes address the coding requirements in [#5097]. JsonSchemaParser._iter_rw_model_schema_children now follows patternProperties and propertyNames only for object-map typing, and the read/w…
Out of Scope Changes check Passed The changed parser logic, generated snapshots, test fixtures, schema-graph test, and parameterized tests all support [#5097]. The changes provide implementation coverage and regression coverage for th…
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 4 files. (3 skipped: 1 …
Full details: Linked Issues check

Explanation

The changes address the coding requirements in [#5097]. JsonSchemaParser._iter_rw_model_schema_children now follows patternProperties and propertyNames only for object-map typing, and the read/write reference traversal preserves the selected child schemas through allOf. The generated snapshots cover request/response models for the flagged model and successful generation for inherited patternProperties and propertyNames cases. The parameterized tests also cover map keywords beside allOf, deduplication modes, and validation results.

Full details: Out of Scope Changes check

Explanation

The changed parser logic, generated snapshots, test fixtures, schema-graph test, and parameterized tests all support [#5097]. The changes provide implementation coverage and regression coverage for the reported request/response reference-resolution failure. No unrelated change is established by the supplied whole-pull-request summary.

Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 4 files. (3 skipped: 1 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

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-23T14:10:14.736500Z 5ff0d67 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 degrade performance by 47.75%

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 regressed benchmarks
⏩ 98 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_perf_client_calls[async] 20.8 ms 45.5 ms -54.3%
❌ test_perf_client_calls[sync] 18.7 ms 41 ms -54.24%
❌ test_perf_load_pyproject_config[mapping-None] 169.3 µs 368.6 µs -54.06%
❌ test_perf_load_pyproject_config[mapping-multiple] 174.8 µs 379.8 µs -53.98%
❌ test_perf_load_pyproject_config[config-multiple] 176.4 µs 378.2 µs -53.37%
❌ test_perf_load_pyproject_config[config-None] 171.1 µs 363.8 µs -52.98%
❌ test_perf_pydantic_field_name_resolution 969.6 µs 2,007.8 µs -51.71%
❌ test_perf_simple_msgspec_unset_field_rendering 78.6 ms 162.7 ms -51.68%
❌ test_perf_complex_refs 1.2 s 2.4 s -51.14%
❌ test_perf_duplicate_names 571 ms 1,163.1 ms -50.91%
❌ test_perf_extreme_large_schema 8.7 s 17.6 s -50.47%
❌ test_perf_local_reference_file_resolution 86.3 ms 173.7 ms -50.33%
❌ test_perf_scalar_root_collapsed_builtin 112.8 ms 227.1 ms -50.31%
❌ test_perf_false_reference_validation 177.5 ms 357 ms -50.29%
❌ test_perf_graphql_style_pydantic_v2 465.1 ms 925.3 ms -49.74%
❌ test_perf_unique_items_runtime_scalar_validation 1.6 ms 3.1 ms -49.57%
❌ test_perf_multiple_files_input 2.1 s 4.2 s -49.54%
❌ test_perf_deep_nested 3.2 s 6.3 s -49.43%
❌ test_perf_ordinary_array_constraint_generation 1.3 s 2.5 s -49.1%
❌ test_perf_large_models_pydantic_v2 2.1 s 4 s -49.06%
... ... ... ... ...

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

Tip

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


Comparing fix-5097-rw-maps (b261839) with fix-5031-extra-items (22fa43a)

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

…ach a readOnly model

Under --read-only-write-only-model-type request-response the schema graph that
decides which models get Request/Response variants followed patternProperties
and propertyNames everywhere. Beside properties or allOf, and in an inline allOf
member, the object is a model whose fields hold its properties alone, and
propertyNames give no keys where patternProperties do, so those 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 reads those keywords only where they type
an object map: the model and the models inheriting from it or referencing it
keep one class, as without the option, and only the readOnly model splits.

The schema graph check test reads a schema whose patternProperties sit beside a
$ref, which still splits this way.

Fixes: #5097
@koxudaxi
Koudai Aono (koxudaxi) merged commit 9df8caf into main Oct 23, 2026
25 of 26 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the fix-5097-rw-maps 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 internal to the JSON Schema parser in the read-only/write-only request-response schema graph walk. The private helper _iter_rw_model_schema_children gets a new keyword argument types_map and now yields a merged flag. _get_ref_schema_rw_model_reference_facts no longer counts patternProperties or propertyNames as type-contributing when they sit beside properties or allOf, inside an inline allOf member, or, for propertyNames, when patternProperties already give the keys. This is a bug fix. Before, the schema graph said these objects needed Request/Response variants even though their generated models have no field holding those map values. The project's own schema-graph consistency check flagged this as an error: the model does not split, but its references read a variant. The updated check fixture shows that the previously flagged pattern_properties_base case is now consistent. Generated output only changes for users of the request-response read-only/write-only mode with these specific map-keyword shapes, and the old output there was the inconsistency the check was built to catch. No CLI options, public API, templates, defaults, Python version support or error reporting changed. The rest of the diff is new test fixtures, expected outputs and test updates that call the private helper.


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