Skip to content

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

Open
Roman3349 wants to merge 1 commit into
jsonrainbow:mainfrom
Roman3349:fix/ref-identifier-resolution
Open

Roman3349 wants to merge 1 commit into
jsonrainbow:mainfrom
Roman3349:fix/ref-identifier-resolution

Conversation

@Roman3349

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. The Draft-06/07/2019-09 RefConstraints swallow the resulting exception, so such a $ref passed silently (any instance validated), while Draft-04 threw UnresolvableJsonPointerException.

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.

Related Issue

Related to #916 (an unresolvable $ref passing silently). Most of the cases in that issue were refs this library could not resolve; they now resolve. This PR does not change how a genuinely unresolvable $ref is reported; that is what #944 addresses.

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

@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 from 72afb14 to 68b1eb0 Compare October 17, 2026 11:23
…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"). The Draft-06/07/2019-09 constraints
swallowed the resulting exception, so such a $ref passed silently, while
Draft-04 threw.

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

Signed-off-by: Roman Ondráček <mail@romanondracek.cz>
@Roman3349
Roman3349 force-pushed the fix/ref-identifier-resolution branch from 68b1eb0 to c357098 Compare October 18, 2026 00:37
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