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

Impossible to bundle a circular schema after dereferencing it #219

Closed
paztis opened this issue Mar 10, 2021 · 5 comments
Closed

Impossible to bundle a circular schema after dereferencing it #219

paztis opened this issue Mar 10, 2021 · 5 comments

Comments

@paztis
Copy link
Contributor

paztis commented Mar 10, 2021

If a schema has circular ancestor, it is possible to dereference it
but if we want to re-reference it (for stringification for example), it is impossible to bundle it.

Tested in the unit tests with specs/circular/circular-indirect-ancestor.yaml

let parser = new $RefParser();
const schemaDeref = await parser.dereference(path.rel("specs/circular/circular-indirect-ancestor.yaml"));

const schema = await parser.bundle(schemaDeref);

This crashed with Maximum call stack in 2 places:

  • in resolve-externals.js crawl function
    easily fixable with this code:
function crawl (obj, path, $refs, options, ancestors = []) {
  let promises = [];

  if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj)) {
    if ($Ref.isExternal$Ref(obj)) {
      promises.push(resolve$Ref(obj, path, $refs, options));
    }
    else if (ancestors.indexOf(obj) === -1) {
      const nextAncestors = [...ancestors, obj];
      for (let key of Object.keys(obj)) {
        let keyPath = Pointer.join(path, key);
        let value = obj[key];

        if ($Ref.isExternal$Ref(value)) {
          promises.push(resolve$Ref(value, keyPath, $refs, options));
        }
        else {
          promises = promises.concat(crawl(value, keyPath, $refs, options, nextAncestors));
        }
      }
    }
  }

  return promises;
}
  • then in bundle.js in crawl function
    here I don't know how to create new references in case of circular detection
@paztis
Copy link
Contributor Author

paztis commented Mar 13, 2021

I've create a PR with a new method rereference that exactly do this job in sync: #221

Cab you look at it ?

@paztis
Copy link
Contributor Author

paztis commented Mar 29, 2021

Hello

Any new about it ? Have you found any time to look at the PR ?

@philsturgeon
Copy link
Member

Why would you dereference AND bundle? I don't understand the use case so its hard to imagine a solution.

@paztis
Copy link
Contributor Author

paztis commented May 6, 2021 via email

@philsturgeon
Copy link
Member

@paztis can you see if this works in 9.0.9? We've done a lot of work on making various circular scenarios work better.

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

No branches or pull requests

2 participants