Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,9 @@ function remap<S extends object = JSONSchema, O extends ParserOptions<S> = 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<string> | undefined;
const recursiveEntries: InventoryEntry[] = [];
for (const entry of inventory) {
// console.log('Re-mapping $ref pointer "%s" at %s', entry.$ref.$ref, entry.pathFromRoot);
Expand Down Expand Up @@ -460,7 +463,7 @@ function remap<S extends object = JSONSchema, O extends ParserOptions<S> = 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);
Expand All @@ -472,6 +475,10 @@ function remap<S extends object = JSONSchema, O extends ParserOptions<S> = 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);
Expand All @@ -482,6 +489,7 @@ function remap<S extends object = JSONSchema, O extends ParserOptions<S> = 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
Expand Down
93 changes: 93 additions & 0 deletions test/specs/bundle-regressions/extended-ref-siblings.spec.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>) {
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<string, unknown>) {
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);
});
});