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

[Circular schema] max call stack in case de-referenced circular schema #217

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

Comments

@paztis
Copy link
Contributor

paztis commented Mar 10, 2021

In case of a schema with circular references, that is re-referenced with @apidevtools/json-schema-ref-parser for example
Circularity is not supported

version: 0.5.0-rcv.33


jsf.option({
    alwaysFakeOptionals: true
}, undefined);
const jsf = require('json-schema-faker');

const schema2 = {
    type: 'object',
    properties: {
        prop3: {type: 'number'},
        prop4: {type: 'number'},
        pointer: null
    }};

const schemaRoot = {
    type: 'object',
    properties: {
        prop1: {type: 'string'},
        prop2: {type: 'string'},
        pointer: schema2
    }
}

schema2.properties.pointer = schemaRoot;

const fake = jsf.generate(schemaRoot);
console.log(fake);

result

const jsf = require('json-schema-faker');

const schema2 = {
    type: 'object',
    properties: {
        prop3: {type: 'number'},
        prop4: {type: 'number'},
        pointer: null
    }};

const schemaRoot = {
    type: 'object',
    properties: {
        prop1: {type: 'string'},
        prop2: {type: 'string'},
        pointer: schema2
    }
}

schema2.properties.pointer = schemaRoot;

const fake = jsf.generate(schemaRoot);
console.log(fake);

I crashed in getRefs function
I;ve test a local fix that resolves it;

function getRefs(refs, schema) {
    var $refs = {};

    if (Array.isArray(refs)) {
      refs.forEach(function (_schema) {
        $refs[_schema.$id || _schema.id] = _schema;
      });
    } else {
      $refs = refs || {};
    }

    function walk(obj, ancestors = []) {
      if (!obj || typeof obj !== 'object') { return; }
      if (ancestors.includes(obj)) {return;}
      if (Array.isArray(obj)) { return obj.forEach((item) => {return walk(item, [...ancestors, obj])}); }

      var _id = obj.$id || obj.id;

      if (typeof _id === 'string' && !$refs[_id]) {
        $refs[_id] = obj;
      }

      Object.keys(obj).forEach(function (key) {
        walk(obj[key], [...ancestors, obj]);
      });
    }

    walk(refs);
    walk(schema);

    return $refs;
  }

but it crashes after in traverse function

Testable in sandbox https://codesandbox.io/s/stoic-northcutt-zzukl?file=/package.json (just run node start in terminal)

@P0lip
Copy link
Member

P0lip commented Mar 10, 2021

I believe you meant to open the issue in json-schema-fake repo? 😃

@paztis
Copy link
Contributor Author

paztis commented Mar 10, 2021

oh sorry my bad

closing it

@paztis paztis closed this as completed Mar 10, 2021
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