Skip to content

Exclude OpenAPI/JSON Schema extension fields (x-*)#2801

Merged
koxudaxi merged 5 commits intokoxudaxi:mainfrom
ahmetveburak:fix/exclude-extension-fields
Dec 31, 2025
Merged

Exclude OpenAPI/JSON Schema extension fields (x-*)#2801
koxudaxi merged 5 commits intokoxudaxi:mainfrom
ahmetveburak:fix/exclude-extension-fields

Conversation

@ahmetveburak
Copy link
Copy Markdown
Contributor

@ahmetveburak ahmetveburak commented Dec 25, 2025

I'm not sure if this approach meets the design. I had to proceed, and I found this solution.

Fixes: #2800

Summary by CodeRabbit

  • Bug Fixes

    • Improved JSON Schema extras handling: vendor extension fields (x-*) are now ignored when detecting schema keywords, preventing unintended schema merging and preserving intended $ref semantics.
  • Tests

    • Added a test to verify that vendor extension keys do not trigger schema merging, ensuring parsing continues to treat $ref-only schemas correctly.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Dec 25, 2025

Warning

Rate limit exceeded

@koxudaxi has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 7 minutes and 50 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 53e7613 and aef5ac5.

📒 Files selected for processing (2)
  • src/datamodel_code_generator/parser/jsonschema.py
  • tests/parser/test_jsonschema.py
📝 Walkthrough

Walkthrough

Ignore OpenAPI/JSON Schema vendor extension keys (prefixed with x-) when deciding if a $ref also contains schema-affecting keywords; has_ref_with_schema_keywords now filters out metadata-only keys and x-* extras from obj.extras before checking for schema keywords. (27 words)

Changes

Cohort / File(s) Summary
Schema detection logic
src/datamodel_code_generator/parser/jsonschema.py
Update has_ref_with_schema_keywords to exclude metadata-only keys and vendor extension keys (x-*) from obj.extras before evaluating presence of schema-affecting keywords.
Tests
tests/parser/test_jsonschema.py
Add test_has_ref_with_schema_keywords_extras_with_extension_keys() asserting that extras containing only x-* extension keys do not trigger schema merging for a $ref.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

🐇
I nibbled extras, tossed the x- away,
Kept refs lean for a brighter day.
No needless merges, no looping fright,
I hopped along, the schema’s light.

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title directly and clearly describes the main change: excluding OpenAPI/JSON Schema extension fields with the x-* prefix, which aligns with the core objective of fixing the recursion issue.
Linked Issues check ✅ Passed The code changes properly address issue #2800 by filtering out x-* extension fields in has_ref_with_schema_keywords to prevent recursion, and the added test validates this behavior.
Out of Scope Changes check ✅ Passed All changes are directly related to the linked issue: the code modification filters x-* extensions, and the test validates this filtering behavior.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8c7550c and 43eb240.

📒 Files selected for processing (2)
  • src/datamodel_code_generator/parser/jsonschema.py
  • tests/parser/test_jsonschema.py
🧰 Additional context used
🧬 Code graph analysis (2)
src/datamodel_code_generator/parser/jsonschema.py (1)
src/datamodel_code_generator/util.py (1)
  • get_fields_set (208-212)
tests/parser/test_jsonschema.py (1)
src/datamodel_code_generator/parser/jsonschema.py (3)
  • JsonSchemaObject (202-472)
  • parse_obj (3121-3162)
  • has_ref_with_schema_keywords (453-472)
🔇 Additional comments (1)
src/datamodel_code_generator/parser/jsonschema.py (1)

453-472: LGTM! Clear fix for recursion issue with OpenAPI extension fields.

The implementation correctly filters out OpenAPI/JSON Schema extension fields (x-*) when determining if a schema has $ref combined with schema-affecting keywords. This prevents unnecessary schema merging that was causing recursion on large schemas with many extension fields.

The docstring update clearly explains the rationale, and the filter logic not k.startswith("x-") is efficient and aligns with the OpenAPI specification requirement that extension fields use lowercase "x-" prefix.

Comment thread tests/parser/test_jsonschema.py
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Dec 25, 2025

CodSpeed Performance Report

Merging #2801 will not alter performance

Comparing ahmetveburak:fix/exclude-extension-fields (aef5ac5) with main (9dc9dd4)

⚠️ 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.

Summary

✅ 11 untouched
⏩ 98 skipped1

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.

@koxudaxi
Copy link
Copy Markdown
Owner

@ahmetveburak
Thank you for creating the PR.
Can you fix the CI errors?
I expect this.

# tests/parser/test_jsonschema.py:925
- obj = JsonSchemaObject.parse_obj({
+ obj = model_validate(
+     JsonSchemaObject,
+     {
          "$ref": "#/$defs/Base",
          "deprecated": False,
          "x-internalAPI": False,
          "x-custom-field": "value",
-     })
+     },
+ )

Copy link
Copy Markdown
Collaborator

@ilovelinux ilovelinux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure to understand how this PR affects current behavior and how it fixes the issue. A rationale is missing.

Also see #2800 (comment). Maybe this is a workaround for just that case? Maybe RecursionLimit may still be hit but this edit keep recursion under the limit for this particular case?

Comment thread src/datamodel_code_generator/parser/jsonschema.py
Comment thread tests/parser/test_jsonschema.py
@ahmetveburak ahmetveburak force-pushed the fix/exclude-extension-fields branch from 9680861 to f7598fa Compare December 26, 2025 17:22
@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 26, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.37%. Comparing base (9dc9dd4) to head (aef5ac5).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2801   +/-   ##
=======================================
  Coverage   99.37%   99.37%           
=======================================
  Files          92       92           
  Lines       16040    16046    +6     
  Branches     1893     1893           
=======================================
+ Hits        15939    15945    +6     
  Misses         52       52           
  Partials       49       49           
Flag Coverage Δ
unittests 99.37% <100.00%> (+<0.01%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 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 enabled auto-merge (squash) December 26, 2025 17:57
@Nekidev
Copy link
Copy Markdown

Nekidev commented Dec 28, 2025

I tried this to get moving and the issue didn't go away when using the fixed branch:

image

@koxudaxi
Copy link
Copy Markdown
Owner

@Nekidev Can you provide the input schema? I want to test it.

@Nekidev
Copy link
Copy Markdown

Nekidev commented Dec 29, 2025

@Nekidev
Copy link
Copy Markdown

Nekidev commented Dec 29, 2025

My temporary fix was to install the library as a library and import it from a script where I had previously set the recursion limit to 5000. It works, but it's not the best solution and neither is it a one-size-fits-all solution. The same repository has a generate.py file with it.

@koxudaxi
Copy link
Copy Markdown
Owner

@Nekidev
Thank you for reporting the issue. I believe the loop bug had a different cause. I've fixed it.
#2852

@Nekidev
Copy link
Copy Markdown

Nekidev commented Dec 30, 2025

That's great! You're welcome.

@koxudaxi koxudaxi merged commit 294acb6 into koxudaxi:main Dec 31, 2025
35 checks passed
@ahmetveburak ahmetveburak deleted the fix/exclude-extension-fields branch December 31, 2025 18:59
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 1, 2026

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenApi extension fields causing recursion on big schemas

4 participants