From 1034be5d61ccc070c94afba3f58945c42259fb74 Mon Sep 17 00:00:00 2001 From: azyl20 <96654138+azyl20@users.noreply.github.com> Date: Thu, 12 Nov 2026 12:25:15 +1100 Subject: [PATCH 1/3] fix(bundle): don't write the root's file path into refs under a nested `$id` When the root has no `$id`, repeat refs under an external file's `$id` were qualified with the root's local file path, so the bundle only resolved on the machine that made it. Rewrite those refs, and fragment refs under the same `$id`, as pointers from the document root and remove that `$id`. Groups that use a URI or anchor ref, or whose `$id` a URI ref may target, are left as is. Co-Authored-By: Claude Opus 5.5 --- lib/bundle.ts | 103 ++++++++++++++++++ .../bundle-id-refs-no-root-id.spec.ts | 35 ++++++ .../root.schema.json | 7 ++ .../schemas/requests/search-uri.schema.json | 12 ++ .../schemas/requests/search.schema.json | 12 ++ .../schemas/requests/test.json | 6 + .../uri-ref.schema.json | 7 ++ 7 files changed, 182 insertions(+) create mode 100644 test/specs/bundle-id-refs-no-root-id/bundle-id-refs-no-root-id.spec.ts create mode 100644 test/specs/bundle-id-refs-no-root-id/root.schema.json create mode 100644 test/specs/bundle-id-refs-no-root-id/schemas/requests/search-uri.schema.json create mode 100644 test/specs/bundle-id-refs-no-root-id/schemas/requests/search.schema.json create mode 100644 test/specs/bundle-id-refs-no-root-id/schemas/requests/test.json create mode 100644 test/specs/bundle-id-refs-no-root-id/uri-ref.schema.json diff --git a/lib/bundle.ts b/lib/bundle.ts index 01450496..29b4ddc2 100644 --- a/lib/bundle.ts +++ b/lib/bundle.ts @@ -72,6 +72,12 @@ function bundle = Pars // Remap all $ref pointers remap(inventory, options, rootId, rootScopeBase); + // Without a root $id, refs qualified from the root carry its local path, which only resolves + // where the bundle was made. Point them from the document root instead where that is safe. + if (!rootId && parser.$refs._root$Ref.dynamicIdScope) { + collapseLocalRootScopes(parser.schema, rootScopeBase, parser.$refs._root$Ref.legacyIdScope); + } + // Fix any $ref paths that traverse through other $refs (which is invalid per JSON Schema spec) const bundleOptions = (options.bundle || {}) as BundleOptions; if (bundleOptions.optimizeInternalRefs !== false) { @@ -758,6 +764,103 @@ function qualifyPathFromRoot( return url.stripHash(rootResource) + pathFromRoot; } +interface IdScope { + key: "$id" | "id"; + id: string; + // The outermost $id schema above this one, or this schema when it is the outermost + outer: object; + pointer: string; + schema: Record; +} + +interface ScopedRef { + holder: { $ref: string }; + // The $id schemas above this ref, outermost first + scopes: IdScope[]; +} + +// Rewrites refs that start with the root's local path, and fragment-only refs under the same +// nested $id, as pointers from the document root, then removes those $ids. +function collapseLocalRootScopes(schema: unknown, rootScopeBase: string, legacyIdScope: boolean) { + const rootPrefix = url.stripHash(rootScopeBase) + "#"; + const refs: ScopedRef[] = []; + const idScopes: IdScope[] = []; + const seen = new Set(); + + const visit = (node: unknown, pointer: string, scopes: IdScope[], inheritedLegacyId: boolean) => { + if (!node || typeof node !== "object" || ArrayBuffer.isView(node) || seen.has(node)) { + return; + } + seen.add(node); + + const obj = node as Record; + const legacyId = getSchemaIdMode(obj, inheritedLegacyId); + const id = getSchemaId(obj, legacyId); + let innerScopes = scopes; + if (id !== undefined) { + const scope: IdScope = { + key: legacyId ? "id" : "$id", + id, + outer: scopes[0]?.schema ?? obj, + pointer, + schema: obj, + }; + idScopes.push(scope); + innerScopes = [...scopes, scope]; + } + + for (const key of Object.keys(obj)) { + const value = obj[key]; + if (key === "$ref" && typeof value === "string") { + refs.push({ holder: obj as ScopedRef["holder"], scopes: innerScopes }); + } else { + visit(value, Pointer.join(pointer, key), innerScopes, legacyId); + } + } + }; + + // The root has no $id here, so only nested schemas open a scope. + visit(schema, "#", [], legacyIdScope); + + const isRootPrefixed = (ref: ScopedRef) => ref.holder.$ref.startsWith(rootPrefix); + const isFragment = (ref: string) => ref === "#" || ref.startsWith("#/"); + // A ref may target a $id when it contains it, unless it is a pointer from the document root. + const mayTargetId = (ref: string, id: string) => ref !== "#" && !isInternalPointer(ref) && ref.includes(id); + const otherRefs = refs.filter((ref) => !isRootPrefixed(ref)).map((ref) => ref.holder.$ref); + + // Grouped by outermost $id, since removing it re-scopes every ref beneath it. + const groups = new Map(); + for (const ref of refs) { + const outer = ref.scopes[0]?.schema; + if (outer) { + groups.set(outer, [...(groups.get(outer) ?? []), ref]); + } + } + + for (const [outer, group] of groups) { + const scopes = idScopes.filter((scope) => scope.outer === outer); + // URI and anchor refs can't be rewritten as root pointers, so a group using one, or one whose + // $id a URI ref may target, stays as the bundler wrote it. + const canCollapse = + group.some(isRootPrefixed) && + group.every((ref) => isRootPrefixed(ref) || isFragment(ref.holder.$ref)) && + !otherRefs.some((ref) => scopes.some(({ id }) => mayTargetId(ref, id))); + if (!canCollapse) { + continue; + } + + const scopesToCollapse = new Set(); + for (const ref of group) { + const innermost = ref.scopes[ref.scopes.length - 1]!; + ref.holder.$ref = isRootPrefixed(ref) + ? ref.holder.$ref.slice(rootPrefix.length - 1) + : innermost.pointer + ref.holder.$ref.slice(1); + ref.scopes.forEach((scope) => scopesToCollapse.add(scope)); + } + scopesToCollapse.forEach((scope) => delete scope.schema[scope.key]); + } +} + function referenceToResource(entry: InventoryEntry): string { let resourceReference = url.stripHash(entry.targetResourceBase); if ( diff --git a/test/specs/bundle-id-refs-no-root-id/bundle-id-refs-no-root-id.spec.ts b/test/specs/bundle-id-refs-no-root-id/bundle-id-refs-no-root-id.spec.ts new file mode 100644 index 00000000..5917a1fb --- /dev/null +++ b/test/specs/bundle-id-refs-no-root-id/bundle-id-refs-no-root-id.spec.ts @@ -0,0 +1,35 @@ +import { describe, it, expect } from "vitest"; +import $RefParser from "../../../lib/index.js"; +import path from "../../utils/path.js"; + +describe("Bundle with $id in sub-schemas and no root $id", () => { + it("should point repeat refs from the document root instead of the root's file path", async () => { + const parser = new $RefParser(); + const bundled = await parser.bundle(path.rel("test/specs/bundle-id-refs-no-root-id/root.schema.json")); + const search = bundled.properties.search; + + // With no root $id there is no portable URI to point back to, so the nested $id is removed + // and refs beneath it become plain pointers from the document root. + expect(search).not.toHaveProperty("$id"); + expect(search.properties.test2.$ref).toBe("#/properties/search/properties/test1"); + expect(search.properties.page.$ref).toBe("#/properties/search/$defs/page"); + expect(JSON.stringify(bundled)).not.toContain(path.abs("test/specs")); + + // The bundle resolves on its own, without the source files. + const dereferenced = await $RefParser.dereference(structuredClone(bundled)); + expect(dereferenced.properties.search.properties.test2).toEqual({ + type: "object", + properties: { name: { type: "string" } }, + }); + expect(dereferenced.properties.search.properties.page).toEqual({ type: "integer" }); + }); + + it("should keep the nested $id when a URI ref beneath it relies on it", async () => { + const parser = new $RefParser(); + const bundled = await parser.bundle(path.rel("test/specs/bundle-id-refs-no-root-id/uri-ref.schema.json")); + const search = bundled.properties.search; + + expect(search.$id).toBe("requests/search-uri.schema.json"); + expect(search.properties.page.$ref).toBe("search-uri.schema.json#/$defs/page"); + }); +}); diff --git a/test/specs/bundle-id-refs-no-root-id/root.schema.json b/test/specs/bundle-id-refs-no-root-id/root.schema.json new file mode 100644 index 00000000..fe88c6a3 --- /dev/null +++ b/test/specs/bundle-id-refs-no-root-id/root.schema.json @@ -0,0 +1,7 @@ +{ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "type": "object", + "properties": { + "search": { "$ref": "schemas/requests/search.schema.json" } + } +} diff --git a/test/specs/bundle-id-refs-no-root-id/schemas/requests/search-uri.schema.json b/test/specs/bundle-id-refs-no-root-id/schemas/requests/search-uri.schema.json new file mode 100644 index 00000000..1243e2b0 --- /dev/null +++ b/test/specs/bundle-id-refs-no-root-id/schemas/requests/search-uri.schema.json @@ -0,0 +1,12 @@ +{ + "$id": "requests/search-uri.schema.json", + "type": "object", + "properties": { + "test1": { "$ref": "../test.json" }, + "test2": { "$ref": "../test.json" }, + "page": { "$ref": "search-uri.schema.json#/$defs/page" } + }, + "$defs": { + "page": { "type": "integer" } + } +} diff --git a/test/specs/bundle-id-refs-no-root-id/schemas/requests/search.schema.json b/test/specs/bundle-id-refs-no-root-id/schemas/requests/search.schema.json new file mode 100644 index 00000000..c1a9b468 --- /dev/null +++ b/test/specs/bundle-id-refs-no-root-id/schemas/requests/search.schema.json @@ -0,0 +1,12 @@ +{ + "$id": "requests/search.schema.json", + "type": "object", + "properties": { + "test1": { "$ref": "../test.json" }, + "test2": { "$ref": "../test.json" }, + "page": { "$ref": "#/$defs/page" } + }, + "$defs": { + "page": { "type": "integer" } + } +} diff --git a/test/specs/bundle-id-refs-no-root-id/schemas/requests/test.json b/test/specs/bundle-id-refs-no-root-id/schemas/requests/test.json new file mode 100644 index 00000000..6a3251bb --- /dev/null +++ b/test/specs/bundle-id-refs-no-root-id/schemas/requests/test.json @@ -0,0 +1,6 @@ +{ + "type": "object", + "properties": { + "name": { "type": "string" } + } +} diff --git a/test/specs/bundle-id-refs-no-root-id/uri-ref.schema.json b/test/specs/bundle-id-refs-no-root-id/uri-ref.schema.json new file mode 100644 index 00000000..62167a64 --- /dev/null +++ b/test/specs/bundle-id-refs-no-root-id/uri-ref.schema.json @@ -0,0 +1,7 @@ +{ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "type": "object", + "properties": { + "search": { "$ref": "schemas/requests/search-uri.schema.json" } + } +} From 556c9f42b400e3df49d18aaaad22b5c0825290a5 Mon Sep 17 00:00:00 2001 From: azyl20 <96654138+azyl20@users.noreply.github.com> Date: Thu, 12 Nov 2026 12:54:32 +1100 Subject: [PATCH 2/3] fix: point repeat refs from the nested resource they sit in Instead of rewriting refs and removing the nested $id after bundling, record where each nested $id resource sits and, when a repeat ref's target is inside that resource, write it as a pointer from the resource. That needs no URI, so the bundle no longer carries the root's local file path and keeps its $ids. Targets outside the resource are still qualified as before. Co-Authored-By: Claude Opus 5.5 --- lib/bundle.ts | 126 +++--------------- .../bundle-id-refs-no-root-id.spec.ts | 60 ++++++--- .../nested.schema.json | 7 + .../outside.schema.json | 8 ++ .../requests/nested-search.schema.json | 14 ++ 5 files changed, 91 insertions(+), 124 deletions(-) create mode 100644 test/specs/bundle-id-refs-no-root-id/nested.schema.json create mode 100644 test/specs/bundle-id-refs-no-root-id/outside.schema.json create mode 100644 test/specs/bundle-id-refs-no-root-id/schemas/requests/nested-search.schema.json diff --git a/lib/bundle.ts b/lib/bundle.ts index 29b4ddc2..9bb224b8 100644 --- a/lib/bundle.ts +++ b/lib/bundle.ts @@ -25,6 +25,8 @@ export interface InventoryEntry { scopeBase: string; dynamicIdScope: boolean; nestedIdScope: boolean; + // Where the innermost non-root $id resource around this $ref sits in the bundle + scopePathFromRoot?: string; targetResourceBase: string; targetResourceId?: string; rootReference?: string; @@ -72,12 +74,6 @@ function bundle = Pars // Remap all $ref pointers remap(inventory, options, rootId, rootScopeBase); - // Without a root $id, refs qualified from the root carry its local path, which only resolves - // where the bundle was made. Point them from the document root instead where that is safe. - if (!rootId && parser.$refs._root$Ref.dynamicIdScope) { - collapseLocalRootScopes(parser.schema, rootScopeBase, parser.$refs._root$Ref.legacyIdScope); - } - // Fix any $ref paths that traverse through other $refs (which is invalid per JSON Schema spec) const bundleOptions = (options.bundle || {}) as BundleOptions; if (bundleOptions.optimizeInternalRefs !== false) { @@ -113,6 +109,7 @@ function crawl = Parse embeddedResourcePaths: Set, seen: Set, matcherAlreadyChecked = false, + scopePathFromRoot?: string, ) { const obj = key === null ? parent : parent[key as keyof typeof parent]; const bundleOptions = (options.bundle || {}) as BundleOptions; @@ -147,6 +144,7 @@ function crawl = Parse options, embeddedResourcePaths, seen, + scopePathFromRoot, ); } else { // Crawl the object in a specific order that's optimized for bundling. @@ -186,6 +184,7 @@ function crawl = Parse : currentScopeBase; const childHasSchemaIdentifier = dynamicIdScope && hasSchemaIdentifier(value, childLegacyIdScope); const childNestedIdScope = nestedIdScope || childHasSchemaIdentifier; + const childScopePathFromRoot = childHasSchemaIdentifier ? keyPathFromRoot : scopePathFromRoot; let childEmbeddedResourcePaths = embeddedResourcePaths; if (childHasSchemaIdentifier) { childEmbeddedResourcePaths = new Set(embeddedResourcePaths); @@ -207,6 +206,7 @@ function crawl = Parse options, childEmbeddedResourcePaths, seen, + childScopePathFromRoot, ); } else { crawl( @@ -225,6 +225,7 @@ function crawl = Parse childEmbeddedResourcePaths, seen, true, + childScopePathFromRoot, ); } @@ -267,6 +268,7 @@ function inventory$Ref options: O, embeddedResourcePaths: Set, seen: Set, + scopePathFromRoot?: string, ) { const $ref = $refKey === null ? $refParent : $refParent[$refKey]; const shouldResolveOnCwd = $Ref.isExternal$Ref($ref) && options.dereference?.externalReferenceResolution === "root"; @@ -328,13 +330,15 @@ function inventory$Ref targetResourceBase, // The canonical URI of the resource containing the target ...(targetResourceId === undefined ? {} : { targetResourceId }), ...(rootReference === undefined ? {} : { rootReference }), + ...(scopePathFromRoot === undefined ? {} : { scopePathFromRoot }), }); // Recursively crawl the resolved value if (!existingEntry || external) { const resolvedScopeBase = pointer.$ref.dynamicIdScope ? pointer.scopeBase : pointer.$ref.path!; - const resolvedNestedIdScope = - nestedIdScope || (pointer.$ref.dynamicIdScope && hasSchemaIdentifier(pointer.value, pointer.legacyIdScope)); + const resolvedHasSchemaIdentifier = + pointer.$ref.dynamicIdScope && hasSchemaIdentifier(pointer.value, pointer.legacyIdScope); + const resolvedNestedIdScope = nestedIdScope || resolvedHasSchemaIdentifier; const resolvedEmbeddedResourcePaths = new Set(embeddedResourcePaths); if (pointer.value === pointer.$ref.value && hasSchemaIdentifier(pointer.$ref.value, pointer.$ref.legacyIdScope)) { resolvedEmbeddedResourcePaths.add(url.stripHash(pointer.$ref.path!)); @@ -355,6 +359,8 @@ function inventory$Ref options, resolvedEmbeddedResourcePaths, seen, + false, + resolvedHasSchemaIdentifier ? pathFromRoot : scopePathFromRoot, ); } } @@ -556,6 +562,7 @@ function preserveRecursiveTargets( scopeBase: getSchemaBasePath(scope.scopeBase, value, legacyIdScope), dynamicIdScope: true, nestedIdScope: true, + scopePathFromRoot: path, }; } if (targets.has(value)) { @@ -752,6 +759,12 @@ function qualifyPathFromRoot( return pathFromRoot; } + // A target inside the same resource can be pointed to from that resource, which needs no URI. + const scopePath = entry.scopePathFromRoot; + if (scopePath !== undefined && (pathFromRoot === scopePath || pathFromRoot.startsWith(scopePath + "/"))) { + return "#" + pathFromRoot.slice(scopePath.length); + } + // A fragment-only reference inside a nested schema resource resolves against // that resource's $id. Point back to the root resource explicitly instead. // Prefer the root's declared $id when it resolves correctly from this scope; @@ -764,103 +777,6 @@ function qualifyPathFromRoot( return url.stripHash(rootResource) + pathFromRoot; } -interface IdScope { - key: "$id" | "id"; - id: string; - // The outermost $id schema above this one, or this schema when it is the outermost - outer: object; - pointer: string; - schema: Record; -} - -interface ScopedRef { - holder: { $ref: string }; - // The $id schemas above this ref, outermost first - scopes: IdScope[]; -} - -// Rewrites refs that start with the root's local path, and fragment-only refs under the same -// nested $id, as pointers from the document root, then removes those $ids. -function collapseLocalRootScopes(schema: unknown, rootScopeBase: string, legacyIdScope: boolean) { - const rootPrefix = url.stripHash(rootScopeBase) + "#"; - const refs: ScopedRef[] = []; - const idScopes: IdScope[] = []; - const seen = new Set(); - - const visit = (node: unknown, pointer: string, scopes: IdScope[], inheritedLegacyId: boolean) => { - if (!node || typeof node !== "object" || ArrayBuffer.isView(node) || seen.has(node)) { - return; - } - seen.add(node); - - const obj = node as Record; - const legacyId = getSchemaIdMode(obj, inheritedLegacyId); - const id = getSchemaId(obj, legacyId); - let innerScopes = scopes; - if (id !== undefined) { - const scope: IdScope = { - key: legacyId ? "id" : "$id", - id, - outer: scopes[0]?.schema ?? obj, - pointer, - schema: obj, - }; - idScopes.push(scope); - innerScopes = [...scopes, scope]; - } - - for (const key of Object.keys(obj)) { - const value = obj[key]; - if (key === "$ref" && typeof value === "string") { - refs.push({ holder: obj as ScopedRef["holder"], scopes: innerScopes }); - } else { - visit(value, Pointer.join(pointer, key), innerScopes, legacyId); - } - } - }; - - // The root has no $id here, so only nested schemas open a scope. - visit(schema, "#", [], legacyIdScope); - - const isRootPrefixed = (ref: ScopedRef) => ref.holder.$ref.startsWith(rootPrefix); - const isFragment = (ref: string) => ref === "#" || ref.startsWith("#/"); - // A ref may target a $id when it contains it, unless it is a pointer from the document root. - const mayTargetId = (ref: string, id: string) => ref !== "#" && !isInternalPointer(ref) && ref.includes(id); - const otherRefs = refs.filter((ref) => !isRootPrefixed(ref)).map((ref) => ref.holder.$ref); - - // Grouped by outermost $id, since removing it re-scopes every ref beneath it. - const groups = new Map(); - for (const ref of refs) { - const outer = ref.scopes[0]?.schema; - if (outer) { - groups.set(outer, [...(groups.get(outer) ?? []), ref]); - } - } - - for (const [outer, group] of groups) { - const scopes = idScopes.filter((scope) => scope.outer === outer); - // URI and anchor refs can't be rewritten as root pointers, so a group using one, or one whose - // $id a URI ref may target, stays as the bundler wrote it. - const canCollapse = - group.some(isRootPrefixed) && - group.every((ref) => isRootPrefixed(ref) || isFragment(ref.holder.$ref)) && - !otherRefs.some((ref) => scopes.some(({ id }) => mayTargetId(ref, id))); - if (!canCollapse) { - continue; - } - - const scopesToCollapse = new Set(); - for (const ref of group) { - const innermost = ref.scopes[ref.scopes.length - 1]!; - ref.holder.$ref = isRootPrefixed(ref) - ? ref.holder.$ref.slice(rootPrefix.length - 1) - : innermost.pointer + ref.holder.$ref.slice(1); - ref.scopes.forEach((scope) => scopesToCollapse.add(scope)); - } - scopesToCollapse.forEach((scope) => delete scope.schema[scope.key]); - } -} - function referenceToResource(entry: InventoryEntry): string { let resourceReference = url.stripHash(entry.targetResourceBase); if ( diff --git a/test/specs/bundle-id-refs-no-root-id/bundle-id-refs-no-root-id.spec.ts b/test/specs/bundle-id-refs-no-root-id/bundle-id-refs-no-root-id.spec.ts index 5917a1fb..eee6fa08 100644 --- a/test/specs/bundle-id-refs-no-root-id/bundle-id-refs-no-root-id.spec.ts +++ b/test/specs/bundle-id-refs-no-root-id/bundle-id-refs-no-root-id.spec.ts @@ -2,34 +2,56 @@ import { describe, it, expect } from "vitest"; import $RefParser from "../../../lib/index.js"; import path from "../../utils/path.js"; +const fixture = (file: string) => path.rel(`test/specs/bundle-id-refs-no-root-id/${file}`); + +/** Bundles a fixture and checks the bundle carries no local file paths and dereferences on its own. */ +async function bundlePortably(file: string) { + const bundled = await new $RefParser().bundle(fixture(file)); + expect(JSON.stringify(bundled)).not.toContain(path.abs("test/specs")); + + const dereferenced = await $RefParser.dereference(structuredClone(bundled)); + return { bundled, dereferenced }; +} + +const testSchema = { type: "object", properties: { name: { type: "string" } } }; + describe("Bundle with $id in sub-schemas and no root $id", () => { - it("should point repeat refs from the document root instead of the root's file path", async () => { - const parser = new $RefParser(); - const bundled = await parser.bundle(path.rel("test/specs/bundle-id-refs-no-root-id/root.schema.json")); + it("should point repeat refs from the nested resource instead of the root's file path", async () => { + const { bundled, dereferenced } = await bundlePortably("root.schema.json"); const search = bundled.properties.search; - // With no root $id there is no portable URI to point back to, so the nested $id is removed - // and refs beneath it become plain pointers from the document root. - expect(search).not.toHaveProperty("$id"); - expect(search.properties.test2.$ref).toBe("#/properties/search/properties/test1"); - expect(search.properties.page.$ref).toBe("#/properties/search/$defs/page"); - expect(JSON.stringify(bundled)).not.toContain(path.abs("test/specs")); - - // The bundle resolves on its own, without the source files. - const dereferenced = await $RefParser.dereference(structuredClone(bundled)); - expect(dereferenced.properties.search.properties.test2).toEqual({ - type: "object", - properties: { name: { type: "string" } }, - }); + expect(search.$id).toBe("requests/search.schema.json"); + expect(search.properties.test2.$ref).toBe("#/properties/test1"); + expect(search.properties.page.$ref).toBe("#/$defs/page"); + expect(dereferenced.properties.search.properties.test2).toEqual(testSchema); expect(dereferenced.properties.search.properties.page).toEqual({ type: "integer" }); }); - it("should keep the nested $id when a URI ref beneath it relies on it", async () => { - const parser = new $RefParser(); - const bundled = await parser.bundle(path.rel("test/specs/bundle-id-refs-no-root-id/uri-ref.schema.json")); + it("should keep a URI ref to the nested resource as written", async () => { + const { bundled, dereferenced } = await bundlePortably("uri-ref.schema.json"); const search = bundled.properties.search; expect(search.$id).toBe("requests/search-uri.schema.json"); + expect(search.properties.test2.$ref).toBe("#/properties/test1"); expect(search.properties.page.$ref).toBe("search-uri.schema.json#/$defs/page"); + expect(dereferenced.properties.search.properties.page).toEqual({ type: "integer" }); + }); + + it("should point repeat refs from the innermost nested resource", async () => { + const { bundled, dereferenced } = await bundlePortably("nested.schema.json"); + const inner = bundled.properties.search.properties.inner; + + expect(inner.$id).toBe("inner/inner.schema.json"); + expect(inner.properties.test2.$ref).toBe("#/properties/test1"); + expect(dereferenced.properties.search.properties.inner.properties.test2).toEqual(testSchema); + }); + + it("should still qualify refs whose target is outside the nested resource", async () => { + const bundled = await new $RefParser().bundle(fixture("outside.schema.json")); + const search = bundled.properties.search; + + // No ref written inside the resource can reach the document root without a root $id. + expect(search.$id).toBe("requests/search.schema.json"); + expect(search.properties.test1.$ref).toMatch(/outside\.schema\.json#\/properties\/a$/); }); }); diff --git a/test/specs/bundle-id-refs-no-root-id/nested.schema.json b/test/specs/bundle-id-refs-no-root-id/nested.schema.json new file mode 100644 index 00000000..3066149c --- /dev/null +++ b/test/specs/bundle-id-refs-no-root-id/nested.schema.json @@ -0,0 +1,7 @@ +{ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "type": "object", + "properties": { + "search": { "$ref": "schemas/requests/nested-search.schema.json" } + } +} diff --git a/test/specs/bundle-id-refs-no-root-id/outside.schema.json b/test/specs/bundle-id-refs-no-root-id/outside.schema.json new file mode 100644 index 00000000..466d30c9 --- /dev/null +++ b/test/specs/bundle-id-refs-no-root-id/outside.schema.json @@ -0,0 +1,8 @@ +{ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "type": "object", + "properties": { + "a": { "$ref": "schemas/requests/test.json" }, + "search": { "$ref": "schemas/requests/search.schema.json" } + } +} diff --git a/test/specs/bundle-id-refs-no-root-id/schemas/requests/nested-search.schema.json b/test/specs/bundle-id-refs-no-root-id/schemas/requests/nested-search.schema.json new file mode 100644 index 00000000..d87529a1 --- /dev/null +++ b/test/specs/bundle-id-refs-no-root-id/schemas/requests/nested-search.schema.json @@ -0,0 +1,14 @@ +{ + "$id": "requests/nested-search.schema.json", + "type": "object", + "properties": { + "inner": { + "$id": "inner/inner.schema.json", + "type": "object", + "properties": { + "test1": { "$ref": "../../test.json" }, + "test2": { "$ref": "../../test.json" } + } + } + } +} From 68948d87129116cd3ee1c70d64fde8bc6254486f Mon Sep 17 00:00:00 2001 From: azyl20 <96654138+azyl20@users.noreply.github.com> Date: Thu, 12 Nov 2026 13:45:37 +1100 Subject: [PATCH 3/3] clean up comments --- lib/bundle.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/bundle.ts b/lib/bundle.ts index 9bb224b8..0f6425d0 100644 --- a/lib/bundle.ts +++ b/lib/bundle.ts @@ -25,7 +25,6 @@ export interface InventoryEntry { scopeBase: string; dynamicIdScope: boolean; nestedIdScope: boolean; - // Where the innermost non-root $id resource around this $ref sits in the bundle scopePathFromRoot?: string; targetResourceBase: string; targetResourceId?: string;