Skip to content

Commit

Permalink
fix(recursion): resolve infinite loop with recursive links
Browse files Browse the repository at this point in the history
  • Loading branch information
Kostya Polenkov committed Dec 5, 2024
1 parent 600f02d commit 0c448c3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/dereferencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export default class Dereferencer {
const refProm = referenceResolver.resolve(ref, this.options.rootSchema);
proms.push(refProm);
fetched = await refProm as JSONSchema;
this.refCache[ref] = fetched
}

if (this.options.recursive === true && fetched !== true && fetched !== false && ref !== "#") {
Expand Down
65 changes: 64 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {inspect} from 'util';
import Dereferencer, { NonStringRefError } from "./index";
import { Properties, JSONSchemaObject, JSONSchema } from "@json-schema-tools/meta-schema";

Expand Down Expand Up @@ -169,6 +168,70 @@ describe("Dereferencer", () => {
expect(oneOfs[0].properties.contains).toBe(dereffed);
});

it("does not get stuck on a recursive ", async () => {
expect.assertions(1);
const dereferencer = new Dereferencer({
definitions: {
node: {
type: "object",
properties: {
name: {
type: "string"
},
children: {
type: "array",
items: {
$ref: "#/definitions/node"
}
}
}
}
},
type: "object",
properties: {
tree: {
title: "Recursive references",
$ref: "#/definitions/node"
}
}
});

const dereffed = await dereferencer.resolve();


const recursiveChildren = {
type: 'array',
items: {
type: 'object',
properties: {
name: {
type: 'string'
},
children: {}
}
},
};
recursiveChildren.items.properties.children = recursiveChildren

const expected = {
type: "object",
properties: {
tree: {
title: "Recursive references",
type: "object",
properties: {
name: {
type: 'string'
},
children: recursiveChildren
}
},
}
}

expect(dereffed).toStrictEqual(expected);
});

it("can deal with root refs-to-ref as url, metaschema on master", async () => {
expect.assertions(8);
const dereferencer = new Dereferencer({
Expand Down

0 comments on commit 0c448c3

Please sign in to comment.