Skip to content

Type users of unmodelled mapped and x-python-import refs by import - #6791

Merged
Koudai Aono (koxudaxi) merged 1 commit into
regex-quote-literal-5355from
fix-mapped-allof-postpass
Nov 13, 2026
Merged

Koudai Aono (koxudaxi) merged 1 commit into
regex-quote-literal-5355from
fix-mapped-allof-postpass

Conversation

@koxudaxi

@koxudaxi Koudai Aono (koxudaxi) commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Fixes: #6772
Fixes: #6786

Summary by CodeRabbit

  • Bug Fixes
    • Improved generated models that use externally mapped or imported types, including in inherited models, fields, collections, unions, and OpenAPI operations.
    • Missing schemas referenced by used external mappings now produce a clear type-resolution error.
  • Documentation
    • Clarified how mapped types behave in allOf when sibling keywords do not add fields; those keywords are not retained.

@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The parser now resolves used, unmodelled references from allOf schemas through mapped imports or x-python-import declarations. Regression tests cover JSON Schema and OpenAPI cases, generated models, and payload validation.

Changes: allOf reference typing

Layer / File(s) Summary
Reference type factories
src/datamodel_code_generator/parser/jsonschema.py
The parser factors reference type creation and reference-user checks into helpers.
Unmodelled reference resolution
src/datamodel_code_generator/parser/jsonschema.py, docs/architecture.md, docs/cli-reference/base-options.md
End-of-parsing logic types used, unmodelled mapped and x-python-import references. Stand-in parsing uses an isolated import context. The documentation describes this flow and the handling of fieldless keywords in mapped allOf references.
Mapped-reference regressions
tests/main/openapi/test_external_ref_mapping_all_of.py, tests/data/expected/main/openapi/external_ref_mapping_all_of/value_keywords[missing].py, tests/data/expected/main/openapi/external_ref_mapping_all_of/value_keywords_modules/*, tests/data/expected/main/openapi/external_ref_mapping_all_of/value_keywords_openapi.py, tests/data/expected/main/openapi/external_ref_mapping_all_of/value_keywords_union.py, tests/main/openapi/test_external_ref_mapping_all_of.refs, tests/main/test_main_openapi.py, tests/main/test_payload_validation.refs, tests/test_yaml_fast_constructor.refs
Tests and generated snapshots cover mapped references across JSON Schema and OpenAPI uses, including missing documents, modules, unions, and payload validation.
Python-import regressions
tests/main/openapi/test_external_ref_mapping_all_of.py, tests/data/expected/main/openapi/external_ref_mapping_all_of/python_import_all_of_value_keywords.py, tests/main/openapi/test_external_ref_mapping_all_of.refs
Tests and snapshots cover imported classes and type expressions in allOf fields, arrays, and additional properties.

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

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

Merge Risk: Minimal · up to 1bf98

The change makes allOf combined with a mapped or x-python-import $ref and a value keyword use the imported type, as a direct $ref does. No actionable merge-blocking risk remains. Nullable x-python-import targets still produce non-optional types, but that behavior is unchanged from before this PR.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the core change: references to unmodelled mapped and x-python-import types are now typed by their imports. The raw summary and objectives confirm this is the main change in jsonschema.…
Linked Issues check Passed Stack objectives, citing contributing PRs:

[#6772, #6786] Generation succeeds for an allOf that combines a $ref with a value keyword (maxLength, description). The fix is in the current PR (#6791). Th…
Out of Scope Changes check Passed All changed files relate to #6772 or #6786. jsonschema.py implements the typing and import fix. Expected snapshot files and test fixture entries (test_external_ref_mapping_all_of.py, .refs files, test…
Full details: Title check

Explanation

The title names the core change: references to unmodelled mapped and x-python-import types are now typed by their imports. The raw summary and objectives confirm this is the main change in jsonschema.py, with tests and docs supporting it. It is specific enough for a reviewer scanning history, though it uses the term 'Type users of' without saying which bug it fixes.

Full details: Linked Issues check

Explanation

Stack objectives, citing contributing PRs:

[#6772, #6786] Generation succeeds for an allOf that combines a $ref with a value keyword (maxLength, description). The fix is in the current PR (#6791). The jsonschema.py change in _type_imported_references_without_model types users of x-python-import and mapped references without models, and creates their imports. Expected snapshots show the intended output for both issues:

  • #6772: value_keywords[missing].py types code as Code, imported from mycompany.shared. value_keywords_modules/order.py types code as Code | None.
  • #6786: python_import_all_of_value_keywords.py types amount as Decimal | None.
    Status: Addressed (contributing PR: #6791). The stack members below the current PR are uncertain and were not reviewed. The current PR diff is taken from merge base fd3b52a, which is the head of #6790, so the base code is present but not independently verified. The summary shows no dependence on that code.

Remaining uncertainty: the summary does not show a dedicated test for the qualname form of x-python-import (#6786). The qualname form goes through the same _python_import_type_maker path, but this is not confirmed by a test. Test results were not supplied.

Full details: Out of Scope Changes check

Explanation

All changed files relate to #6772 or #6786. jsonschema.py implements the typing and import fix. Expected snapshot files and test fixture entries (test_external_ref_mapping_all_of.py, .refs files, test_main_openapi.py, test_payload_validation.refs, test_yaml_fast_constructor.refs) exercise the allOf value-keyword and x-python-import cases. Documentation updates (architecture.md, base-options.md, test_main_openapi.py docstring) describe the changed behavior. No unrelated behavior change is shown in the summary. Excluded files (for example tests/data/**/*.json and docs/llms-full.txt) were not inspected.

Finishing Touches

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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between fd3b52a and 1bf982c.

Files ignored due to path filters: 9

  • docs/llms-full.txt is excluded by none and included by none
  • tests/data/jsonschema/external_ref_mapping_all_of_keywords/order.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/jsonschema/external_ref_mapping_all_of_keywords/shared.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/jsonschema/external_ref_mapping_all_of_keywords/union.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/jsonschema/external_ref_mapping_all_of_keywords_modules/order.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/jsonschema/external_ref_mapping_all_of_keywords_modules/other.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/jsonschema/python_import_all_of_keywords.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/openapi/external_ref_mapping_all_of_keywords/api.yaml is excluded by !tests/data/**/*.yaml and included by none
  • tests/data/openapi/external_ref_mapping_all_of_keywords/shared.yaml is excluded by !tests/data/**/*.yaml and included by none
Files selected for processing: 15

  • docs/architecture.md
  • docs/cli-reference/base-options.md
  • src/datamodel_code_generator/parser/jsonschema.py
  • tests/data/expected/main/openapi/external_ref_mapping_all_of/python_import_all_of_value_keywords.py
  • tests/data/expected/main/openapi/external_ref_mapping_all_of/value_keywords[missing].py
  • tests/data/expected/main/openapi/external_ref_mapping_all_of/value_keywords_modules/__init__.py
  • tests/data/expected/main/openapi/external_ref_mapping_all_of/value_keywords_modules/order.py
  • tests/data/expected/main/openapi/external_ref_mapping_all_of/value_keywords_modules/other.py
  • tests/data/expected/main/openapi/external_ref_mapping_all_of/value_keywords_openapi.py
  • tests/data/expected/main/openapi/external_ref_mapping_all_of/value_keywords_union.py
  • tests/main/openapi/test_external_ref_mapping_all_of.py
  • tests/main/openapi/test_external_ref_mapping_all_of.refs
  • tests/main/openapi/test_main_openapi.py
  • tests/main/test_payload_validation.refs
  • tests/test_yaml_fast_constructor.refs

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

@koxudaxi
Koudai Aono (koxudaxi) marked this pull request as ready for review November 13, 2026 04:09
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T04:11:29.092631Z 1bf982c Draft marked ready
ℹ️ 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 Nov 13, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 16.23%

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

⚠️ 44 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

❌ 44 regressed benchmarks
✅ 9 untouched benchmarks
⏩ 98 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_perf_simple_msgspec_unset_field_rendering 333.3 ms 485.2 ms -31.3%
❌ test_perf_large_models_msgspec 1.5 s 2 s -22.99%
❌ test_perf_large_models_typed_dict[total-false-requiredness] 901.5 ms 1,143.9 ms -21.19%
❌ test_perf_large_models_typed_dict[legacy-requiredness] 911.1 ms 1,154.6 ms -21.09%
❌ test_perf_ordinary_array_constraint_generation 1.6 s 2 s -20.68%
❌ test_perf_client_calls[sync] 25.6 ms 32.2 ms -20.57%
❌ test_perf_large_models_dataclass_builtin 1.1 s 1.3 s -20.28%
❌ test_perf_large_models_single_module_stdout 1.3 s 1.6 s -19.97%
❌ test_perf_large_models_pydantic_v2_builtin 1.1 s 1.3 s -19.78%
❌ test_perf_scalar_root_collapsed_builtin 145.6 ms 181.5 ms -19.75%
❌ test_perf_client_calls[async] 28.6 ms 35.7 ms -19.7%
❌ test_perf_target_render[fastapi-small] 17.5 ms 21.4 ms -18.4%
❌ test_perf_large_models_pydantic_v2_builtin_double_quotes 1.1 s 1.4 s -18.36%
❌ test_perf_openapi_large_pydantic_v2_builtin 1.2 s 1.5 s -18.24%
❌ test_perf_inherited_required_fields[required-base-first] 563.6 ms 688.2 ms -18.1%
❌ test_perf_large_models_pydantic_v2_noformat 1.1 s 1.3 s -17.94%
❌ test_perf_local_reference_file_resolution 103.2 ms 124.5 ms -17.14%
❌ test_perf_inherited_required_fields[partial-base-first] 963.6 ms 1,161.8 ms -17.06%
❌ test_perf_graphql_style_pydantic_v2 632.9 ms 754.8 ms -16.15%
❌ test_perf_duplicate_names 783.7 ms 931.2 ms -15.84%
... ... ... ... ...

ℹ️ 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-mapped-allof-postpass (1bf982c) with regex-quote-literal-5355 (fd3b52a)

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 Nov 13, 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 1d8de06 into main Nov 13, 2026
117 of 118 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the fix-mapped-allof-postpass branch November 13, 2026 05:05
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: The PR fixes a crash. Before, an allOf that combines a mapped external ref (from --external-ref-mapping) or an x-python-import definition with keywords that add no field failed with the error A Parser can not resolve classes; now it is typed by the import. The source changes are all private methods of JsonSchemaParser. One new hook, _type_imported_references_without_model, replaces _reject_mapped_references_without_schema and keeps the same SchemaParseError for a mapped ref whose fragment names no schema. The other new helpers are _python_import_type_maker, _type_users_by, _has_reference_users and the _unshared_imports context manager, which the existing stand-in parse now reuses. No public Python name, CLI option, default value or template is removed or changed, and the release baseline lists no new surfaces. For the inputs this PR touches, the behaviour diff gate classifies the default-output cells as crash-fix, including the new python_import_all_of_keywords input. The accepted cells are about writing patterns that contain a single quote, which this diff does not touch, so they come from other changes since 0.95.0. The docs changes only describe the newly supported allOf case. Output changes only for schemas that used to fail, so there are no breaking changes.


This analysis was produced by the release draft workflow

@github-actions

Copy link
Copy Markdown
Contributor

🎉 Released in 0.96.0

This PR is now available in the latest release. See the release notes for details.

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.

allOf of an x-python-import $ref and maxLength cannot resolve classes allOf of a mapped $ref and maxLength fails with "can not resolve classes"

1 participant