Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(recursion): resolve infinite loop with recursive links #717

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
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