Skip to content

fix: resolve canonical $id aliases after concurrent external loads - #444

Open
PStarH wants to merge 1 commit into
APIDevTools:mainfrom
PStarH:fix/external-canonical-id-race
Open

PStarH wants to merge 1 commit into
APIDevTools:mainfrom
PStarH:fix/external-canonical-id-race

Conversation

@PStarH

@PStarH PStarH commented Dec 1, 2026

Copy link
Copy Markdown

Problem

A canonical $id alias registered by an external document can be invisible to a sibling $ref resolved concurrently.

Root schema references the same external document by path and by its canonical $id:

// root
"$id": "mem://schemas/root.json",
"properties": {
  "byPath": { "$ref": "external.json" },
  "byId":   { "$ref": "urn:example:external" }
}
// external.json
"$id": "urn:example:external"

External refs are crawled concurrently. The urn: ref is attempted before external.json has been parsed and its $id registered, so no resolver matches it and resolution throws:

SyntaxError: Unable to resolve $ref pointer "urn:example:external"

Before: 1 pass / 1 fail. A direct probe shows only mem://schemas/ext.json had been read when the failure occurred.

Fix

  • Extract file-info construction into an exported getFileInfo() shared by parse() and the resolver pre-check (lib/parse.ts).
  • In lib/resolve-external.ts, when a ref has zero matching resolvers, defer it instead of failing immediately. After all documents finish loading, retry deferred refs once, when canonical aliases are registered.
  • Refs with a matching resolver still resolve immediately; genuinely unresolvable refs still reject with the original error.

Verification

On the patched worktree, using bun:test plus a temporary vitest shim (project dependencies are not installed):

bun test test/specs/external-canonical-id/external-canonical-id.spec.ts
2 pass, 0 fail, 4 expect() calls

Before-patch same spec logic: 1 pass / 1 fail.

Additional checks:

probe4.ts         resolves; a === b: true; one read only
qa_canonical.ts   8/8 checks
qa_harness_patched.ts  10 cases / 24 checks, 0 failures
tsc --noEmit      pass

The regression test asserts identity (toBe) between the path-ref and $id-ref results, and that the external document is read exactly once. A second case confirms an actually unresolvable urn: still rejects with the original error.

Caveat

vitest is unavailable in this environment, so the spec ran under bun:test with a shim outside the repo. It has not been executed under the project's real Vitest config, and the full suite was not run. CI should re-run the new spec and the full suite.

Duplicate check

Open issues at scan time: #440, #435, #433. Open PRs: none. No issue/PR covers this race between path-based and canonical-$id resolution of the same external document.

This branch has not been deployed

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

1 participant