Skip to content

Split pydantic request-response models only where their typed extra field or schema validator rules type additionalProperties - #5297

Merged
Koudai Aono (koxudaxi) merged 1 commit into
fix-5293-moved-rule-aliasfrom
fix-5286-typed-extra-allof-false
Oct 24, 2026
Merged

Koudai Aono (koxudaxi) merged 1 commit into
fix-5293-moved-rule-aliasfrom
fix-5286-typed-extra-allof-false

Conversation

@koxudaxi

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

Copy link
Copy Markdown
Member

Fixes: #5286

Summary by CodeRabbit

  • Bug Fixes
    • Corrected request and response model generation for schemas with typed additional fields, pattern-based properties, or references to other models.
    • Generated models now keep the appropriate request- and response-specific types for extra fields, including in TypedDict output.
    • Improved validation of extra fields governed by pattern-property rules.

…ield or schema validator rules type additionalProperties

The request-response split walk followed the additionalProperties of every pydantic model, while the builder
writes no typed extra field for an allOf holding a false schema, so the holder was asked for variants nobody
generated. The walk now reads the builder's own decisions: the typed extra field's schema
(_typed_extra_field_schema) and, under --generate-schema-validators, the additionalProperties the patternProperties
rules type (_pattern_rule_additional_properties), including those of an inline allOf member.

Fixes: #5286
@coderabbitai

coderabbitai Bot commented Oct 24, 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 7851208 and df173ab.

Files ignored due to path filters: 1
  • tests/data/jsonschema/read_only_write_only_typed_extras.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_typed_extras.py
  • tests/data/expected/main/jsonschema/read_only_write_only_typed_extras~main_read_only_write_only_typed_extras[dedupe-validators].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

Request/response reference discovery now follows only additional-property schemas used by generated typed fields, pattern-property validators, or TypedDict extra items. Shared helpers select those schemas and handle eligible $ref siblings. New tests cover model splitting and generated validation.

Changes: Request/response reference discovery

Layer / File(s) Summary
Select schemas for reference traversal
src/datamodel_code_generator/parser/jsonschema.py
Shared helpers select schema-valued additional properties for pattern rules and typed extra fields. Reference traversal uses those selections for model fields, map models, inline allOf members, and TypedDict extra items.
Verify request/response variants
tests/data/expected/main/jsonschema/read_only_write_only_typed_extras.py, tests/data/expected/main/jsonschema/read_only_write_only_typed_extras~main_read_only_write_only_typed_extras[dedupe-validators].py, tests/main/jsonschema/test_main_jsonschema.py, tests/main/jsonschema/test_main_jsonschema.refs, tests/parser/test_jsonschema.py
Generated outputs and tests cover typed and untyped extras, pattern-property validation, request/response deduplication, and valid and invalid payloads.

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 reviewers: ysheikh2

Merge Risk: Minimal · up to df173

No concrete issue remains that would prevent merging after normal checks.

Pre-merge checks: Passed 4 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Docstring Coverage Inconclusive Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 3 files. (3 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and specifically summarizes the main change: limiting request-response model splitting to cases where typed extra-field or schema-validator rules type additionalProperties.
Linked Issues check Passed The changes address #5286. src/datamodel_code_generator/parser/jsonschema.py makes request/response reference discovery follow the same typed additionalProperties decisions as model generation. Th…
Out of Scope Changes check Passed The source change is limited to request/response reference discovery and shared additionalProperties eligibility and schema-selection logic. The generated expected files, snapshot registration, and …
Full details: Linked Issues check

Explanation

The changes address #5286. src/datamodel_code_generator/parser/jsonschema.py makes request/response reference discovery follow the same typed additionalProperties decisions as model generation. The change covers schema-valued additionalProperties, pattern-property validation sources, inline allOf handling, typed extra fields, and TypedDict extra_items. The added test_main_read_only_write_only_typed_extras test covers closed models beside a false schema, typed extras that require request/response variants, and validator-based output. The expected outputs cover both default and deduplicated validator cases.

Full details: Out of Scope Changes check

Explanation

The source change is limited to request/response reference discovery and shared additionalProperties eligibility and schema-selection logic. The generated expected files, snapshot registration, and parser test update support the new behavior. No unrelated production behavior or unrelated test area is shown in the whole-PR summary.

Full details: Docstring Coverage

Explanation

Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 3 files. (3 skipped: 1 unsupported, 2 too large.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

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
  • 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 24, 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-24T08:10:36.692736Z df173ab 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 24, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 12.23%

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

⚡ 12 improved benchmarks
✅ 38 untouched benchmarks
⏩ 98 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_perf_large_models_pydantic_v2_noformat 884.3 ms 757.1 ms +16.79%
⚡ test_perf_large_models_typed_dict[legacy-requiredness] 776.5 ms 667.4 ms +16.35%
⚡ test_perf_large_models_single_module_stdout 990.3 ms 869.2 ms +13.93%
⚡ test_perf_large_models_pydantic_v2_builtin_double_quotes 887.7 ms 794.4 ms +11.74%
⚡ test_perf_kubernetes_style_pydantic_v2 1.6 s 1.4 s +11.65%
⚡ test_perf_aws_style_openapi_pydantic_v2 1.3 s 1.2 s +11.54%
⚡ test_perf_simple_msgspec_unset_field_rendering 84.2 ms 75.6 ms +11.36%
⚡ test_perf_multiple_files_input 2.3 s 2.1 s +11.34%
⚡ test_perf_graphql_style_pydantic_v2 510.6 ms 459.4 ms +11.15%
⚡ test_perf_large_models_typed_dict[total-false-requiredness] 790.6 ms 714.3 ms +10.68%
⚡ test_perf_large_models_pydantic_v2 2.1 s 1.9 s +10.46%
⚡ test_perf_simple_pydantic_v2_field_construction 6.4 ms 5.8 ms +10.04%

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-5286-typed-extra-allof-false (df173ab) with fix-5293-moved-rule-alias (7851208)

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!

@koxudaxi
Koudai Aono (koxudaxi) merged commit c35e0d0 into main Oct 24, 2026
132 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the fix-5286-typed-extra-allof-false branch October 24, 2026 09:10
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: Breaking changes detected

Reasoning: The diff changes how the JSON Schema parser decides which children of a model can cause read-only and write-only splitting. The old boolean types_extras (true for any pydantic model with schema-valued additionalProperties) is replaced by the new helper _rw_typed_additional_properties. It returns only the additionalProperties that the typed extra field or the patternProperties validator rules actually type. The new expected output shows the effect on generated code. Closed and ClosedBesideRef, whose additionalProperties are an allOf with a false schema, now stay single classes where the old logic would have split them into Request and Response variants. Member, under generate-schema-validators, now splits into MemberRequest and MemberResponse because its allOf member additionalProperties point to a split model. These class name changes in request-response output can break user code that imports the old names. No CLI options, public API, templates, defaults or Python version support change. The only signature change is a renamed keyword on a private helper method.

Content for Release Notes

Code Generation Changes

  • Request-response splitting now follows the typed extra field of pydantic models - With --read-only-write-only-model-type request-response and pydantic output, a model with fields now splits into Request and Response variants only when the type actually written for its additionalProperties refers to a split model. Before, any schema-valued additionalProperties counted whenever the output model type supported a typed extra field. So models whose additionalProperties get no typed extra field, for example an allOf that contains a false schema, are now generated as one class (e.g. Closed) instead of the old ClosedRequest and ClosedResponse pair. With --generate-schema-validators, a model whose patternProperties rules type additionalProperties taken from an allOf member that refers to a split model now splits into Request and Response variants (e.g. MemberRequest and MemberResponse instead of Member). Code that imports or refers to the old class names must be updated (Split pydantic request-response models only where their typed extra field or schema validator rules type additionalProperties #5297)

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 models crash on additionalProperties allOf with false next to a model reference

1 participant