Skip to content

Fix relative URL refs with path-only root ids#3085

Merged
koxudaxi merged 2 commits intomainfrom
fix-relative-url-ref-3080
Apr 15, 2026
Merged

Fix relative URL refs with path-only root ids#3085
koxudaxi merged 2 commits intomainfrom
fix-relative-url-ref-3080

Conversation

@koxudaxi
Copy link
Copy Markdown
Owner

@koxudaxi koxudaxi commented Apr 14, 2026

Summary

  • resolve relative $id values against the retrieval URL before using them as the base for relative $ref resolution
  • add resolver coverage for relative root $id plus base_url
  • add a CLI regression test for --url input with path-only $id and relative external $ref

Root Cause

When a schema was fetched via --url and declared a path-only root $id like /schemas/v1/main.schema.json, that $id replaced the retrieval URL as the reference base without first being normalized to an absolute URL. The next relative $ref was therefore resolved as a local filesystem path instead of remaining remote.

Validation

  • tox -e type
  • tox -e readme
  • tox -e cli-docs -- --check
  • tox -e config-types -- --check
  • tox -e py314-parallel -- tests/test_reference.py tests/main/jsonschema/test_main_jsonschema.py -k 'relative_root_id or root_id_differs_from_base_url or url_with_relative_root_id_resolves_relative_refs or root_id_jsonschema_with_remote_file or remote_ref'
  • tox -e py312-parallel -- tests/test_reference.py tests/main/jsonschema/test_main_jsonschema.py -k 'relative_root_id or root_id_differs_from_base_url or url_with_relative_root_id_resolves_relative_refs or root_id_jsonschema_with_remote_file or remote_ref'

Fixes: #3080

Summary by CodeRabbit

Bug Fixes

  • Fixed URL resolution logic for relative JSON Schema $ref references when using remote schemas with relative root $id paths. Relative references now correctly resolve against the provided base URL.

Tests

  • Added end-to-end and unit tests to verify correct resolution of relative references in schemas with relative root identifiers.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 14, 2026

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: 5fd68a75-144d-4236-a08f-b5f988952369

📥 Commits

Reviewing files that changed from the base of the PR and between f0960e9 and d8d928d.

📒 Files selected for processing (3)
  • src/datamodel_code_generator/reference.py
  • tests/main/jsonschema/test_main_jsonschema.py
  • tests/test_reference.py

📝 Walkthrough

Walkthrough

Modified ModelResolver.resolve_ref method to fix relative $ref resolution when using the --url option. The logic now correctly uses base_url as the effective base when set, incorporating root_id appropriately via join_url, rather than treating root_id as a fallback. Added unit and E2E tests validating the fix.

Changes

Cohort / File(s) Summary
Reference Resolution Logic
src/datamodel_code_generator/reference.py
Updated ModelResolver.resolve_ref to prioritize base_url when determining the effective base URL. When root_id is present and not a full URL, it is joined with base_url via join_url instead of being used as a fallback. Added PLR0915 noqa directive to method signature.
Unit Test
tests/test_reference.py
Added test test_resolve_ref_with_relative_root_id_and_base_url to validate that relative $ref resolves correctly against an HTTP base_url when root_id is path-only.
E2E Test
tests/main/jsonschema/test_main_jsonschema.py
Added test test_main_url_with_relative_root_id_resolves_relative_refs to verify CLI behavior with --url option. Mocks HTTP requests and confirms relative references are fetched from the correct derived URL and generated output is correct.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A URL so grand, a path so small,
The ref once lost, now answers the call,
Base and root join hands so bright,
External schemas resolved just right! 🌐✨

🚥 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 clearly summarizes the main change: fixing relative URL refs when root ids are path-only, which directly addresses the core bug described in issue #3080.
Linked Issues check ✅ Passed The PR fully addresses issue #3080: it fixes relative $ref resolution when schemas have path-only root $id values by normalizing them against the retrieval URL, preventing incorrect filesystem path resolution.
Out of Scope Changes check ✅ Passed All changes directly support the linked issue fix: core logic updates to ModelResolver.resolve_ref, a unit test for the resolver behavior, and an E2E CLI test validating the --url scenario with relative root $id.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ 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 fix-relative-url-ref-3080

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

github-actions Bot commented Apr 14, 2026

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 14, 2026

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 fix-relative-url-ref-3080 (d8d928d) with main (f0960e9)

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.

@koxudaxi koxudaxi marked this pull request as ready for review April 14, 2026 06:55
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (f0960e9) to head (d8d928d).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #3085   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           87        87           
  Lines        18274     18300   +26     
  Branches      2089      2090    +1     
=========================================
+ Hits         18274     18300   +26     
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 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.

@Raekkeri
Copy link
Copy Markdown

Hi @koxudaxi

Thanks for the quick response! This works on my machine.

B.R. the author of #3080

@koxudaxi koxudaxi merged commit d7a1f71 into main Apr 15, 2026
38 checks passed
@koxudaxi koxudaxi deleted the fix-relative-url-ref-3080 branch April 15, 2026 15:50
@github-actions
Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR is a bug fix for URL resolution when fetching schemas via --url with a path-only root $id. It corrects incorrect behavior (relative $id was being treated as a filesystem path instead of being normalized against the retrieval URL). No CLI options, Python API, default behavior, or generated code output formats are changed. The fix only affects previously-broken cases, making them work correctly, so existing correct usage is unaffected.


This analysis was performed by Claude Code Action

@github-actions
Copy link
Copy Markdown
Contributor

🎉 Released in 0.56.1

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.

Relative $ref does not resolve externally when using --url option

2 participants