Skip to content

Reuse validated schema definitions#3467

Merged
koxudaxi merged 1 commit into
mainfrom
optimize-parser-throughput
Jun 21, 2026
Merged

Reuse validated schema definitions#3467
koxudaxi merged 1 commit into
mainfrom
optimize-parser-throughput

Conversation

@koxudaxi

@koxudaxi koxudaxi commented Jun 21, 2026

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • Performance

    • Added caching for validated schema definitions to eliminate redundant re-processing during code generation.
    • Optimized handling of references within the same JSON schema file for faster resolution.
  • Tests

    • Added regression test for same-file reference behavior to ensure consistent behavior.

@coderabbitai

coderabbitai Bot commented Jun 21, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 36188f7e-2fce-4302-ba14-90c83a910ac5

📥 Commits

Reviewing files that changed from the base of the PR and between 2a4a94e and 8e9dd00.

⛔ Files ignored due to path filters (1)
  • tests/data/jsonschema/same_file_ref_fast_path.json is excluded by !tests/data/**/*.json and included by none
📒 Files selected for processing (4)
  • src/datamodel_code_generator/parser/jsonschema.py
  • tests/data/expected/main/jsonschema/same_file_ref_fast_path.py
  • tests/main/jsonschema/test_main_jsonschema.py
  • tests/parser/test_jsonschema.py

📝 Walkthrough

Walkthrough

Adds a _get_ref_raw_doc(file_part) helper to JsonSchemaParser that centralizes same-file $ref raw-document selection (returning self.raw_obj for empty or current-root file parts). Updates _load_ref_schema_object to use it and adds an early _ref_schema_object_cache write after definition validation in _parse_file. New unit and integration tests cover both code paths.

Changes

Same-file $ref fast-path and definition caching

Layer / File(s) Summary
$ref raw-doc helper and definition cache pre-population
src/datamodel_code_generator/parser/jsonschema.py
Adds _get_ref_raw_doc(file_part) that returns self.raw_obj when file_part is empty or matches the current root ref path, otherwise delegates to _get_ref_body. Rewires _load_ref_schema_object to call it. Adds a setdefault cache write keyed by model_resolver.resolve_ref(definition_path) after definition validation in _parse_file.
Unit tests for fast-path and cache reuse
tests/parser/test_jsonschema.py
Adds RefBodyCountingJsonSchemaParser (tracks _get_ref_body call paths). Adds two tests asserting same-root $ref uses self.raw_obj with no body load and non-current-root $ref loads the body. Updates test_parse_file_reuses_validated_definition_objects with nested definitions and stricter exact-count assertions.
Integration test and golden file
tests/data/expected/main/jsonschema/same_file_ref_fast_path.py, tests/main/jsonschema/test_main_jsonschema.py
Golden file defines User and SameFileRefFastPath Pydantic models. Integration test test_main_same_file_ref_fast_path generates code from same_file_ref_fast_path.json and asserts output matches the golden file.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • koxudaxi/datamodel-code-generator#3445: Both PRs modify $ref resolution in jsonschema.py to handle the same-document "$ref": "#" case, with overlapping changes in the _load_ref_schema_object / parse_obj code path.
  • koxudaxi/datamodel-code-generator#3466: Both PRs touch the definition-validation and _ref_schema_object_cache population path in _parse_file, implementing validated $ref caching to avoid re-validating the same referenced definition.

Poem

🐇 A rabbit hops along the schema tree,
No need to fetch the same branch twice, you see!
_get_ref_raw_doc checks the current root first,
Cached definitions quench the re-parse thirst.
With golden tests to prove the path is right,
Same-file refs now zip at rabbit speed! 🌟

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Reuse validated schema definitions' directly aligns with the main change: caching and reusing validated JsonSchemaObject instances in the parser to avoid re-validation.
Docstring Coverage ✅ Passed Docstring coverage is 86.67% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch optimize-parser-throughput

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 and usage tips.

@github-actions

Copy link
Copy Markdown
Contributor

📚 Docs Preview: https://pr-3467.datamodel-code-generator.pages.dev

@codspeed-hq

codspeed-hq Bot commented Jun 21, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

✅ 11 untouched benchmarks
⏩ 98 skipped benchmarks1


Comparing optimize-parser-throughput (8e9dd00) with main (2a4a94e)

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 Jun 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (2a4a94e) to head (8e9dd00).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #3467   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          149       149           
  Lines        32235     32276   +41     
  Branches      3770      3772    +2     
=========================================
+ Hits         32235     32276   +41     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@koxudaxi koxudaxi merged commit d71d633 into main Jun 21, 2026
64 checks passed
@koxudaxi koxudaxi deleted the optimize-parser-throughput branch June 21, 2026 17:39
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: PR #3467 is an internal performance optimization for resolving same-file JSON Schema $refs. It replaces an inline expression in the private _load_ref_schema_object method with a new private helper _get_ref_raw_doc, and seeds _ref_schema_object_cache in _parse_file to reuse already-validated definition objects. The only removed line is an internal one-liner (swapped for an equivalent helper call) plus a docstring update — all within underscore-prefixed private methods. There are no changes to CLI options, the public Python API, Jinja2 templates, default values, Python version support, or error handling. The added test test_main_same_file_ref_fast_path explicitly asserts that generated output remains unchanged, confirming no code-generation behavior change. Therefore there are no breaking changes.


This analysis was performed by Claude Code Action

@github-actions

Copy link
Copy Markdown
Contributor

🎉 Released in 0.65.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.

1 participant