Skip to content

fix: Resolve $ref to anchors, URN base URIs and ids beside $ref in Draft 2019-09 - #970

Merged
DannyvdSluijs merged 1 commit into
jsonrainbow:mainfrom
Roman3349:fix/ref-identifier-resolution
Nov 24, 2026
Merged

DannyvdSluijs merged 1 commit into
jsonrainbow:mainfrom
Roman3349:fix/ref-identifier-resolution

Conversation

@Roman3349

@Roman3349 Roman3349 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Description

A $ref whose fragment is a plain name (an anchor) rather than a JSON pointer could not be resolved. Fragment-only ids ("id": "#foo" / "$id": "#foo" in Draft-04 to Draft-07) and $anchor (Draft 2019-09) were never registered. On top of that, expandRefs() rewrote the fragment into a JSON pointer, so #foo became #/foo, and resolving it threw UnresolvableJsonPointerException, even for valid data. Up to 6.13.1, the Draft-06/07/2019-09 RefConstraints swallowed that exception in strict mode, so such a $ref passed silently; since #944 it surfaces.

URN base URIs were broken as well. UriResolver::resolve() took urn:uuid:… for a local file path and prefixed it with file://<cwd>/, and generate() would have produced urn://uuid:… anyway.

Changes:

  • SchemaStorage
    • Registers anchors and resolves a plain-name fragment against them. An unknown anchor throws UnresolvableJsonPointerException.
    • A fragment-only id names a schema without changing the base URI of its members.
    • From Draft 2019-09 on, a $ref is resolved against the $id next to it. The dialect is taken from $schema and inherited by subschemas. Up to Draft-07 the sibling $id stays ignored. Without $schema the previous behavior is kept.
  • UriResolver
    • A fragment-only reference is resolved as the base without its fragment plus the new fragment (RFC 3986 §5.2.2).
    • A base with a scheme but no authority (urn:) is no longer treated as a local file path. A single-letter scheme still is, to keep Windows drive paths (C:\…) working.
  • JsonSchemaTestSuiteTest

Related Issue

Related to #916 (an unresolvable $ref passing silently). Follow-up to #944 (released in 6.14.0), which made an unresolvable $ref throw. With this PR, refs to anchors, fragment-only ids and URN-based ids resolve instead of throwing, which restores the Draft-06/07 strict results that dropped in 6.14.0.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code refactoring
  • Other (please describe):

Checklist

  • I have read the CONTRIBUTING guidelines
  • My code follows the code style of this project
  • I have added tests that prove my fix is effective or that my feature works
  • All new and existing tests pass
  • I have updated the documentation accordingly
  • My changes generate no new warnings

Additional Notes

Rebased onto 6.14.0. With #944 merged, the "valid" variants of the affected test suite cases started throwing and were skipped; they pass again with this PR, so those skips are removed together with their "invalid" twins.

@Roman3349 Roman3349 changed the title fix: Resolve $ref to anchors, URN base URIs and ids beside $ref in Dr… fix: Resolve $ref to anchors, URN base URIs and ids beside $ref in Draft 2019-09 Oct 14, 2026
@Roman3349
Roman3349 force-pushed the fix/ref-identifier-resolution branch 2 times, most recently from 68b1eb0 to c357098 Compare October 18, 2026 00:37
@Roman3349
Roman3349 force-pushed the fix/ref-identifier-resolution branch from c357098 to 0a4aa67 Compare October 24, 2026 17:40
@Roman3349
Roman3349 force-pushed the fix/ref-identifier-resolution branch from 0a4aa67 to d1fb2f0 Compare November 23, 2026 23:12
@Roman3349

Copy link
Copy Markdown
Contributor Author

Rebased onto the current main to resolve the conflict with #951 in UriResolverTest. As #951 now resolves an empty reference against the base instead of returning the base, the fragment-only shortcut checks strpos($uri, '#') === 0 instead of $uri[0] === '#', which failed on an empty reference.

…aft 2019-09

A $ref whose fragment is a plain name rather than a JSON pointer could not
be resolved: fragment-only ids (draft-04 to draft-07) and $anchor (draft
2019-09) were never registered, and the fragment was rewritten to a JSON
pointer ("#foo" became "#/foo"). Resolving such a $ref therefore threw
UnresolvableJsonPointerException, even for valid data. Up to 6.13.1 the
Draft-06/07/2019-09 constraints swallowed that exception in strict mode,
so the $ref passed silently; since jsonrainbow#944 it surfaces.

- SchemaStorage registers anchors and resolves a plain-name fragment
  against them. A fragment-only id no longer changes the base uri.
- From Draft 2019-09 on, a $ref is resolved against the $id next to it,
  as the dialect declared by $schema says. Up to draft-07 the sibling
  $id stays ignored.
- UriResolver resolves a fragment-only reference against the base without
  its fragment, and no longer takes a base without an authority (urn:)
  for a local file path.
- Removes the test suite skips that now pass, including those added in
  jsonrainbow#944 for refs that could not be resolved.

Signed-off-by: Roman Ondráček <mail@romanondracek.cz>
@DannyvdSluijs
DannyvdSluijs merged commit be61915 into jsonrainbow:main Nov 24, 2026
18 checks passed
github-actions Bot added a commit that referenced this pull request Nov 24, 2026
@Roman3349
Roman3349 deleted the fix/ref-identifier-resolution branch November 24, 2026 20:10
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.

2 participants