Skip to content

Generate request-response models of dataclass, msgspec and TypedDict where additionalProperties sit beside properties - #5243

Merged
Koudai Aono (koxudaxi) merged 1 commit into
fix-5208-rw-merged-mapsfrom
fix-5209-rw-additional
Oct 23, 2026
Merged

Koudai Aono (koxudaxi) merged 1 commit into
fix-5208-rw-merged-mapsfrom
fix-5209-rw-additional

Conversation

@koxudaxi

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

Copy link
Copy Markdown
Member

Fixes: #5209

Summary by CodeRabbit

  • Bug Fixes
    • Improved request and response model generation when schemas combine read-only or write-only fields with additional properties, including inherited and referenced schemas.
    • Additional properties are now included in generated models when they contribute to the schema, with more accurate types across supported model formats.

@coderabbitai

coderabbitai Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

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 99795c9 and 8485884.

⛔ Files ignored due to path filters: 1
  • tests/data/jsonschema/read_only_write_only_additional_properties_beside_properties.json is excluded by !tests/data/**/*.json and included by none
📒 Files selected for processing: 10
  • src/datamodel_code_generator/parser/jsonschema.py
  • tests/data/expected/main/jsonschema/read_only_write_only_additional_properties_beside_properties.py
  • tests/data/expected/main/jsonschema/read_only_write_only_additional_properties_beside_properties~main_read_only_write_only_additional_properties_beside_properties[dedupe-msgspec].py
  • tests/data/expected/main/jsonschema/read_only_write_only_additional_properties_beside_properties~main_read_only_write_only_additional_properties_beside_properties[dedupe-pydantic_v2].py
  • tests/data/expected/main/jsonschema/read_only_write_only_additional_properties_beside_properties~main_read_only_write_only_additional_properties_beside_properties[dedupe-pydantic_v2_dataclass].py
  • tests/data/expected/main/jsonschema/read_only_write_only_additional_properties_beside_properties~main_read_only_write_only_additional_properties_beside_properties[dedupe-typed_dict-open].py
  • tests/data/expected/main/jsonschema/read_only_write_only_additional_properties_beside_properties~main_read_only_write_only_additional_properties_beside_properties[dedupe-typed_dict].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; 6 remain after this review.

📝 Walkthrough

The parser now considers typed additionalProperties when it detects Request/Response variants and walks schemas. New regression tests and snapshots cover these schemas across multiple model backends and deduplication modes.

Changes: Additional-properties variant generation

Layer / File(s) Summary
Schema traversal for additional properties
src/datamodel_code_generator/parser/jsonschema.py, tests/parser/test_jsonschema.py
The schema walk includes additionalProperties when the output model writes its type or when the merged schema has no declared properties. Schema-valued additionalProperties siblings are also merged into $ref schemas.
Request/Response variant detection
src/datamodel_code_generator/parser/jsonschema.py
Variant detection checks inherited schemas, types written beside fields, nested type references, and bases that generate Request variants.
Backend regression coverage
tests/main/jsonschema/test_main_jsonschema.py, tests/main/jsonschema/test_main_jsonschema.refs, tests/data/expected/main/jsonschema/read_only_write_only_additional_properties_beside_properties*
A parameterized test checks schema acceptance and rejection and validates generated models. Snapshots cover dataclass, msgspec, Pydantic, and TypedDict outputs, including open TypedDicts and deduplicated output.

Priority: ➖ Normal

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

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

Possibly related PRs

Suggested labels: breaking-change-analyzed

Merge Risk: ⚪ Minimal · up to 84858

No actionable issue remains from the reviewed changes; the PR is mergeable 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 names the main change: request/response model generation for dataclass, msgspec, and TypedDict backends when additionalProperties sits beside properties. The raw summary and objectives confi…
Linked Issues check Passed Issue [#5209] requires successful request-response generation for dataclass, msgspec, and TypedDict backends when additionalProperties sits beside properties and reaches a readOnly model. The parser…
Out of Scope Changes check Passed The changes are within issue [#5209]. The parser changes implement request/response variant handling for typed additionalProperties. The expected model files and regression tests verify the affected…
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 7 files. (3 skipped: 1 …
Full details: Title check

Explanation

The title names the main change: request/response model generation for dataclass, msgspec, and TypedDict backends when additionalProperties sits beside properties. The raw summary and objectives confirm this matches the changes to jsonschema.py, the new regression test, and the expected output files. The title is long (117 characters) but it is specific and related to the changeset.

Full details: Linked Issues check

Explanation

Issue [#5209] requires successful request-response generation for dataclass, msgspec, and TypedDict backends when additionalProperties sits beside properties and reaches a readOnly model. The parser changes include typed additionalProperties in request/response variant detection and schema traversal when the type contributes to generated output. The added parameterized regression test covers standard dataclass, msgspec, closed TypedDict, open TypedDict, and Pydantic variants, with and without deduplication. The generated expected files provide output evidence for these cases.

Full details: Out of Scope Changes check

Explanation

The changes are within issue [#5209]. The parser changes implement request/response variant handling for typed additionalProperties. The expected model files and regression tests verify the affected backends. The schema-graph test update supports the changed child-schema collection behavior. No unrelated product behavior is identified in the supplied whole-PR 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 7 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-23T16:43:24.219822Z d40af8f 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: d40af8f4f0

ℹ️ 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 31.57%

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_recommend_options_full_kubernetes_style 14.5 s 26.7 s -45.9%
❌ test_perf_client_calls[sync] 23.9 ms 42.1 ms -43.12%
❌ test_perf_client_calls[async] 26.8 ms 46.4 ms -42.31%
❌ test_perf_load_pyproject_config[mapping-None] 229.9 µs 371.2 µs -38.08%
❌ test_perf_load_pyproject_config[config-None] 227 µs 361.3 µs -37.17%
❌ test_perf_load_pyproject_config[mapping-multiple] 239.2 µs 378.7 µs -36.84%
❌ test_perf_load_pyproject_config[config-multiple] 239.3 µs 375.1 µs -36.2%
❌ test_perf_large_models_pydantic_v2_noformat 1 s 1.6 s -35.08%
❌ test_perf_target_render[fastapi-large] 4.5 s 6.9 s -34.73%
❌ test_perf_graphql_style_pydantic_v2 642.6 ms 984 ms -34.7%
❌ test_perf_aws_style_openapi_pydantic_v2 1.7 s 2.5 s -34.5%
❌ test_perf_complex_refs 1.7 s 2.5 s -34.03%
❌ test_perf_multiple_files_input 2.9 s 4.4 s -33.9%
❌ test_perf_inherited_required_fields[partial-derived-first] 813.6 ms 1,230.1 ms -33.86%
❌ test_perf_false_reference_validation 253.2 ms 382.5 ms -33.8%
❌ test_perf_extreme_large_schema 12.2 s 18.5 s -33.79%
❌ test_perf_large_models_pydantic_v2_builtin_double_quotes 1.1 s 1.6 s -33.65%
❌ test_perf_all_options_enabled 5 s 7.5 s -33.62%
❌ test_perf_deep_nested 4.4 s 6.6 s -33.5%
❌ test_perf_large_models_pydantic_v2_builtin 1.1 s 1.6 s -33.36%
... ... ... ... ...

ℹ️ 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-5209-rw-additional (8485884) with fix-5208-rw-merged-maps (99795c9)

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!

…where additionalProperties sit beside properties

Under --read-only-write-only-model-type request-response the schema graph read
the additionalProperties beside properties as a reference of the model, which
holds for pydantic v2 BaseModel, whose typed extra field names their type. A
dataclass, a pydantic dataclass, a msgspec Struct and a TypedDict under
--no-use-closed-typed-dict accept extra keys of any type, so 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 additionalProperties only where the model writes their type, also where
the properties come from the $ref beside them: the other models keep one class,
as without the option, and only the readOnly model splits.

A TypedDict writes their type as extra_items, which named the readOnly model
while its model did not split. Such a model now splits where the type it writes
as extra_items names a model with variants, and each variant names the matching
variant there.

Fixes: #5209
@koxudaxi
Koudai Aono (koxudaxi) merged commit 7b0a6b0 into main Oct 23, 2026
25 of 26 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the fix-5209-rw-additional branch October 23, 2026 19:11
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This is a narrow bug fix to the opt-in request-response read-only/write-only model splitting in the JSON Schema parser. It only matters when a model has both properties and a schema-valued additionalProperties that points to a model with readOnly or writeOnly fields. The parser now uses the additionalProperties type to decide the split only for output types that actually write that type: pydantic v2 BaseModel through its typed extra field, and TypedDict through extra_items when closed TypedDicts are enabled. For those, the holder model now splits into Request and Response variants, so its extras point to the matching variant instead of to a base model that is never generated in request-response mode. For dataclass, pydantic dataclass, msgspec and open TypedDict output, additionalProperties beside properties no longer count as a type reference, because those models accept extra keys of any type. This keeps the predicted split and the generated split consistent. The same handling now also covers additionalProperties placed beside a $ref. Before this change, this schema pattern gave inconsistent or invalid output, so the new behavior corrects broken output rather than changing working output. The diff adds only new test fixtures and expected outputs, and no existing expected output was modified. No CLI options, public Python APIs, defaults, templates or supported Python versions change. The only signature changes are to private underscore-prefixed parser helpers, namely _splits_into_rw_model_variants, which now takes a reference, and _iter_rw_model_schema_children, which now takes a types_extras keyword. These are internal and not part of the public API.


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