Repository navigation
Description
When two $refs to the same external file both have sibling keys, bundle() inlines the first one (merged with its siblings) and points the second at it. The second ref then resolves to the target plus the first ref's siblings. dereference() on the source files gives the correct result.
import { mkdirSync, writeFileSync } from "node:fs";
import { $RefParser } from "@apidevtools/json-schema-ref-parser";
mkdirSync("schemas", { recursive: true });
writeFileSync("schemas/status.json", JSON.stringify({ type: "string" }));
writeFileSync("schemas/a.json", JSON.stringify({
type: "object",
properties: { status: { $ref: "status.json", description: "A status" } },
}));
writeFileSync("schemas/b.json", JSON.stringify({
type: "object",
properties: { status: { $ref: "status.json", nullable: true } },
}));
writeFileSync("root.json", JSON.stringify({
$defs: {
A: { $ref: "schemas/a.json" },
B: { $ref: "schemas/b.json" },
},
}));
const bundled = await new $RefParser().bundle("root.json");
console.log("bundle:");
console.log(JSON.stringify(bundled.$defs.B.properties.status));
const fromBundle = await new $RefParser().dereference(bundled);
const fromFiles = await new $RefParser().dereference("root.json");
console.log("dereference(bundle) vs dereference(files):");
console.log(JSON.stringify(fromBundle.$defs.B.properties.status));
console.log(JSON.stringify(fromFiles.$defs.B.properties.status));node t.mjs (in a package with "type": "module")
Actual (16.0.8):
bundle:
{"$ref":"#/$defs/A/properties/status","nullable":true}
dereference(bundle) vs dereference(files):
{"nullable":true,"description":"A status","type":"string"}
{"nullable":true,"type":"string"}
$defs.B.properties.status gets description: "A status", which only $defs.A.properties.status has.
Expected: $defs.B.properties.status resolves to {"nullable":true,"type":"string"}, as dereference() of the source files does.
Tested 16.0.8 on Node.js v22.22.1. 15.5.0 and 11.9.3 give the same result (11.9.3 writes #/%24defs/...). main (735a407) has no changes since v16.0.8.
In remap() in lib/bundle.ts, the sort puts extended refs after the others, but when every ref to status.json is extended, the first one is still inlined with $Ref.dereference(), which merges its siblings into the value. The next entry with the same file and hash is then remapped to that entry's pathFromRoot.
Which ref is inlined first depends on the inventory order. In one of our OpenAPI 3.1 documents that order changed between 15.5.0 and 15.5.1, so a different response schema started to show another schema's nullable and example.
Activity
fix: isolate sibling overrides when bundling shared references