json-ref-resolver icon indicating copy to clipboard operation
json-ref-resolver copied to clipboard

Maximum callstack issue

Open JaiPe opened this issue 3 years ago • 1 comments

Describe the bug

I have a case where a schema may return a circular dependency (even to itself) and this seems to cause a maximum callstack and/or out of memory exception. But only if a multi-level baseUri is specified.

To Reproduce

This causes an infinite loop:

  const resolver = new Resolver({
    resolvers: {
      file: {
        resolve(p) {
          return { $ref: p.toString() };
        }
      }
    }
  });
  await resolver.resolve([{ $ref: './a' }], {
    baseUri: '/root1/root2' // Change this to /root1 and it's fine
  });

Expected behavior

baseUri should support any number of nested folders.

Additional context

Just a guess, but I assume this is something related to the normalised key that's being stored as a visited node - but am unfamiliar with the code and haven't been able to dig into it yet.

Environment (remove any that are not applicable):

  • Library version: v3.1.3

JaiPe avatar Dec 03 '21 11:12 JaiPe

Just a followup, I get a similar issue if I use the transformRef property, even if the baseUri is only 1 level deep:

  const resolver = new Resolver({
    transformRef: uri => {
      return uri.uri;
    },
    resolvers: {
      file: {
        resolve(p) {
          return { $ref: p.toString() };
        }
      }
    }
  });
  await resolver.resolve([{ $ref: './a' }], {
    baseUri: '/root1'
  });

JaiPe avatar Dec 03 '21 11:12 JaiPe