From 6113eb058e61ced3b27d94e876b228fbb085ed50 Mon Sep 17 00:00:00 2001 From: Mradul Singh Date: Fri, 13 Nov 2026 16:37:28 +0530 Subject: [PATCH] fix: don't remap refs onto an extended ref's sibling keys when bundling An extended $ref is inlined merged with its siblings, so another $ref to the same target (or to a path under one of those sibling keys) can't be pointed at it. Inline those refs instead. Fixes #440 --- lib/bundle.ts | 10 +- .../extended-ref-siblings.spec.ts | 93 +++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 test/specs/bundle-regressions/extended-ref-siblings.spec.ts diff --git a/lib/bundle.ts b/lib/bundle.ts index 4d4e214e..5bd7e20a 100644 --- a/lib/bundle.ts +++ b/lib/bundle.ts @@ -431,6 +431,9 @@ function remap = Parse }); let file, hash, pathFromRoot; + // Sibling keys of the inlined $ref that other $refs are remapped to. An extended $ref is + // inlined merged with its siblings, so those keys no longer hold the target's own values. + let siblingKeys: Set | undefined; const recursiveEntries: InventoryEntry[] = []; for (const entry of inventory) { // console.log('Re-mapping $ref pointer "%s" at %s', entry.$ref.$ref, entry.pathFromRoot); @@ -460,7 +463,7 @@ function remap = Parse entry.$ref.$ref = entry.hash; } } - } else if (entry.file === file && entry.hash === hash) { + } else if (entry.file === file && entry.hash === hash && !siblingKeys) { // This $ref points to the same value as the previous $ref, so remap it to the same path entry.$ref.$ref = qualifyPathFromRoot(entry, pathFromRoot, rootId, rootScopeBase); generatedRefs.set(entry.$ref, pathFromRoot); @@ -472,6 +475,10 @@ function remap = Parse // location into that ancestor would remove the very path it points to. entry.$ref = entry.parent[entry.key] = $Ref.dereference(entry.$ref, entry.value, options); recursiveEntries.push(entry); + } else if (siblingKeys?.has(tokens[0])) { + // The previous $ref overrides this key with its own sibling, so the path beneath + // it no longer holds the target's value. Inline the value here instead. + entry.$ref = entry.parent[entry.key] = $Ref.dereference(entry.$ref, entry.value, options); } else { const subPath = Pointer.join(pathFromRoot, tokens); entry.$ref.$ref = qualifyPathFromRoot(entry, subPath, rootId, rootScopeBase); @@ -482,6 +489,7 @@ function remap = Parse file = entry.file; hash = entry.hash; pathFromRoot = entry.pathFromRoot; + siblingKeys = entry.extended ? new Set(Object.keys(entry.$ref).filter((key) => key !== "$ref")) : undefined; // This is the first $ref to point to this value, so dereference the value. // Any other $refs that point to the same value will point to this $ref instead diff --git a/test/specs/bundle-regressions/extended-ref-siblings.spec.ts b/test/specs/bundle-regressions/extended-ref-siblings.spec.ts new file mode 100644 index 00000000..e23e29fb --- /dev/null +++ b/test/specs/bundle-regressions/extended-ref-siblings.spec.ts @@ -0,0 +1,93 @@ +import { describe, expect, it } from "vitest"; +import $RefParser from "../../../lib/index.js"; + +const rootUrl = "mem://schemas/root.json"; + +function resolverFor(documents: Record) { + return { + file: false as const, + http: false as const, + mem: { + canRead: /^mem:/, + read(file: { url: string }) { + const document = documents[file.url]; + if (document === undefined) { + throw new Error(`Unexpected memory URL: ${file.url}`); + } + return JSON.stringify(document); + }, + }, + }; +} + +async function bundleAndDereference(documents: Record) { + const resolve = resolverFor(documents); + const bundled: any = await new $RefParser().bundle(rootUrl, { resolve }); + const fromBundle: any = await new $RefParser().dereference(structuredClone(bundled)); + const fromFiles: any = await new $RefParser().dereference(rootUrl, { resolve }); + return { bundled, fromBundle, fromFiles }; +} + +describe("bundling extended $refs to the same target (issue #440)", () => { + it("does not point an extended $ref at another extended $ref", async () => { + const { bundled, fromBundle, fromFiles } = await bundleAndDereference({ + "mem://schemas/status.json": { type: "string" }, + "mem://schemas/a.json": { + type: "object", + properties: { status: { $ref: "status.json", description: "A status" } }, + }, + "mem://schemas/b.json": { + type: "object", + properties: { status: { $ref: "status.json", nullable: true } }, + }, + [rootUrl]: { + $defs: { + A: { $ref: "a.json" }, + B: { $ref: "b.json" }, + }, + }, + }); + + expect(bundled.$defs.A.properties.status).toEqual({ type: "string", description: "A status" }); + expect(bundled.$defs.B.properties.status).toEqual({ type: "string", nullable: true }); + expect(fromBundle.$defs.B.properties.status).toEqual({ type: "string", nullable: true }); + expect(fromBundle).toEqual(fromFiles); + }); + + it("does not point a sub-path $ref beneath a key that an extended $ref overrides", async () => { + const { bundled, fromBundle, fromFiles } = await bundleAndDereference({ + "mem://schemas/person.json": { + type: "object", + properties: { name: { type: "string" } }, + }, + [rootUrl]: { + $defs: { + A: { $ref: "person.json", properties: { name: { description: "A name" } } }, + B: { $ref: "person.json#/properties/name" }, + }, + }, + }); + + expect(bundled.$defs.B).toEqual({ type: "string" }); + expect(fromBundle.$defs.B).toEqual({ type: "string" }); + expect(fromBundle).toEqual(fromFiles); + }); + + it("still remaps a sub-path $ref beneath keys that an extended $ref leaves alone", async () => { + const { bundled, fromBundle, fromFiles } = await bundleAndDereference({ + "mem://schemas/person.json": { + type: "object", + properties: { name: { type: "string" } }, + }, + [rootUrl]: { + $defs: { + A: { $ref: "person.json", description: "A person" }, + B: { $ref: "person.json#/properties/name" }, + }, + }, + }); + + expect(bundled.$defs.B).toEqual({ $ref: "#/$defs/A/properties/name" }); + expect(fromBundle).toEqual(fromFiles); + }); +});