matic icon indicating copy to clipboard operation
matic copied to clipboard

Recursive references cause a RangeError (stack overflow)

Open bascoder opened this issue 9 years ago • 0 comments

When running matic for a JSON file with recursive references ($ref), then a RangeError is caused.

C:\node_modules\matic\lib\merge.js:52
        itterate(val, key, obj, schema);
        ^

RangeError: Maximum call stack size exceeded
    at C:\node_modules\matic\lib\merge.js:52:9
    at Function._.each._.forEach (C:\node_modules\matic\node_modules\underscore\underscore.js:87:22)
    at C:\node_modules\matic\lib\merge.js:14:7
    at C:\node_modules\matic\lib\merge.js:52:9
    at Function._.each._.forEach (C:\node_modules\matic\node_modules\underscore\underscore.js:87:22)
    at C:\node_modules\matic\lib\merge.js:14:7
    at merge (C:\node_modules\matic\lib\merge.js:64:12)
    at C:\node_modules\matic\lib\merge.js:33:36
    at Function._.each._.forEach (C:\node_modules\matic\node_modules\underscore\underscore.js:87:22)
    at C:\node_modules\matic\lib\merge.js:14:7

As example I cloned matic-draft4-example([https://github.com/mattyod/matic-draft4-example). And I changed the example.json file to the following:

{
  "title": "Matic Draft 4 Example",
  "Description": "An example schema to demonstrate Matic",
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "items": {
    "allOf": [
      {
        "$ref": "#definitions/foo"
      },
      {
        "$ref": "#definitions/bar"
      }
    ],
    "anyOf": [
      {
        "$ref": "#definitions/foo"
      }
    ],
    "oneOf": [
      {
        "$ref": "#definitions/bar"
      }
    ]
  },
  "definitions": {
    "foo": {
      "title": "Foo",
      "description": "A foo schema",
      "$schema": "http://json-schema.org/draft-04/schema#",
      "type": "object",
      "properties": {
        "url": {
          "title": "A url",
          "description": "An example url schema",
          "type": "string",
          "format": "uri",
          "example": "http://github.com/mattyod/matic"
        },
        "list": {
          "title": "A list",
          "description": "A list of things",
          "type": "array",
          "example": [
            "one",
            "two",
            "three"
          ]
        },
        "recursion" : {
          "$ref": "#definitions/foo"
        }
      },
      "required": [
        "url"
      ]
    },
    "bar": {
      "title": "Bar",
      "description": "A bar schema",
      "$schema": "http://json-schema.org/draft-04/schema#",
      "type": "object",
      "properties": {
        "thing": {
          "title": "thing",
          "description": "a thing that uses a local pointer to an other thing for it's properties",
          "type": "object",
          "properties": {
            "$ref": "example"
          }
        },
        "thing2": {
          "$ref": "#/properties/otherThing"
        },
        "otherThing": {
          "id": "example",
          "type": "integer"
        }
      },
      "required": [
        "thing",
        "thing2",
        "otherThing"
      ]
    }
  }
}

Note that I've inlined the bar and foo objects. And to foo I have added the following property:

"recursion" : {
          "$ref": "#definitions/foo"
        }

So it contains a recursive reference which is allowed by the JSON Schema spec.

bascoder avatar Dec 12 '16 13:12 bascoder