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

stop infinite recursion w/ crawling external refs #231

Merged
merged 2 commits into from
May 21, 2021
Merged

stop infinite recursion w/ crawling external refs #231

merged 2 commits into from
May 21, 2021

Conversation

cody-greene
Copy link
Contributor

@cody-greene cody-greene commented May 16, 2021

This will fix a few cases of "Uncaught RangeError: Maximum call stack size exceeded"

Specifically if you have a schema with a circular reference like this:

const Node = {
  type: 'object',
  properties: {
    child: {$ref: '#'}
  }
}

And for whatever reason this gets dereferenced twice:

// First call runs fine.
const dereferenced = await $RefParser.dereference(Node)

console.log(dereferenced)
//{
//  type: 'object',
//  properties: { child: [Circular *1] }
//}

// Infinite recursion will occur in resolve-external.js:crawl()
await $RefParser.dereference(dereferenced)

And of course this can happen without calling dereference() twice. In fact, I can construct a dereferenced schema myself like so:

const Node = {
  type: 'object',
  properties: {
    child: null  
  }
}
Node.properties.child = Node

// Infinite recursion will occur 
await $RefParser.resolve(Node)

To resolve this we can modify the recursive function to track seen objects and return early if one is seen second time (or more).

Copy link
Member

@P0lip P0lip left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

lib/resolve-external.js Outdated Show resolved Hide resolved
@philsturgeon philsturgeon enabled auto-merge May 21, 2021 09:10
@philsturgeon philsturgeon disabled auto-merge May 21, 2021 09:10
@philsturgeon philsturgeon merged commit 8b70f18 into APIDevTools:master May 21, 2021
@github-actions
Copy link

github-actions bot commented Jun 7, 2021

🎉 This PR is included in version 9.0.8 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants