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 references between two (or more) chemas cause a dead loop #199

Open
KonstantinSviridov opened this issue Jan 24, 2018 · 0 comments · Fixed by raml-org/raml-js-parser-2#855

Comments

@KonstantinSviridov
Copy link

Hi!

While circular references within a single schema are supported, the situation is different for circular references between several schemas. The code is

var ZSchema = require("z-schema");

var ref1 = "http://www.example.org/schema1/#";
var ref2 = "http://www.example.org/schema2/#";

var schema1 = {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "id": ref1, 
    "properties": {
        "prop1": {
           "$ref":  ref2
        }
    }
};

var schema2 = {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "id": ref2,
    "properties": {
        "prop1": {
           "$ref":  ref1
        }
    }
};

var validator = new ZSchema();
validator.setRemoteReference(ref1, schema1);
validator.setRemoteReference(ref2, schema2);

var valid = validator.validateSchema(schema1);
console.log("valid: " + JSON.stringify(valid))
console.log("errors: " + JSON.stringify(validator.getLastErrors(),null,2))

And the output is

C:\work\circular-refs\node_modules\z-schema\src\Utils.js:4
    return /^https?:\/\//.test(uri);
                          ^

RangeError: Maximum call stack size exceeded
    at RegExp.test (native)
    at Object.exports.isAbsoluteUri (C:\work\circular-refs\node_modules\z-schema\src\Utils.js:4:27)
    at mergeReference (C:\work\circular-refs\node_modules\z-schema\src\SchemaCompilation.js:8:15)
    at collectReferences (C:\work\circular-refs\node_modules\z-schema\src\SchemaCompilation.js:52:18)
    at collectReferences (C:\work\circular-refs\node_modules\z-schema\src\SchemaCompilation.js:82:13)
    at ZSchema.collectReferences (C:\work\circular-refs\node_modules\z-schema\src\SchemaCompilation.js:82:13)
    at ZSchema.exports.compileSchema (C:\work\circular-refs\node_modules\z-schema\src\SchemaCompilation.js:219:34)
    at ZSchema.exports.getSchemaByUri (C:\work\circular-refs\node_modules\z-schema\src\SchemaCache.js:123:49)
    at ZSchema.exports.compileSchema (C:\work\circular-refs\node_modules\z-schema\src\SchemaCompilation.js:224:51)
    at ZSchema.exports.getSchemaByUri (C:\work\circular-refs\node_modules\z-schema\src\SchemaCache.js:123:49)

Trying to pass both schemas in array

var ZSchema = require("z-schema");

var ref1 = "http://www.example.org/schema1/#";
var ref2 = "http://www.example.org/schema2/#";

var schema1 = {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "id": ref1, 
    "properties": {
        "prop1": {
           "$ref":  ref2
        }
    }
};

var schema2 = {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "id": ref2,
    "properties": {
        "prop1": {
           "$ref":  ref1
        }
    }
};

var validator = new ZSchema();
//validator.setRemoteReference(ref1, schema1);
//validator.setRemoteReference(ref2, schema2);

var valid = validator.validateSchema([schema1,schema2]);
console.log("valid: " + JSON.stringify(valid))
console.log("errors: " + JSON.stringify(validator.getLastErrors(),null,2))

produces invalid errors:

valid: false
errors: [
  {
    "code": "UNRESOLVABLE_REFERENCE",
    "params": [
      "http://www.example.org/schema1/#"
    ],
    "message": "Reference could not be resolved: http://www.example.org/schema1/#",
    "path": "#/properties/prop1",
    "schemaId": "http://www.example.org/schema2/#"
  },
  {
    "code": "UNRESOLVABLE_REFERENCE",
    "params": [
      "http://www.example.org/schema2/#"
    ],
    "message": "Reference could not be resolved: http://www.example.org/schema2/#",
    "path": "#/properties/prop1",
    "schemaId": "http://www.example.org/schema1/#"
  }
]

In the last case validation passes fine after the cycle is broken in one place, so, the references values themselves are not the reason.

Regards,
Konstantin

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

Successfully merging a pull request may close this issue.

2 participants