Skip to content

Infinite recursion when data contains cyclic dependencies #97

@znerol

Description

@znerol

Given the following data-structure containing cyclic dependencies:

var jsonld = require('jsonld');

var a = {
  "@context": {"@vocab": "http://schema.org"},
  "@id": "http://example.com/a",
  hasPart: []
}
var b = {
  "@context": {"@vocab": "http://schema.org"},
  "@id": "http://example.com/b",
  isPartOf: []
}

a.hasPart.push(b);
b.isPartOf.push(a);

jsonld.flatten(a);

I get the following RangeError:

/tmp/node_modules/jsonld/js/jsonld.js:5836
  return (Object.prototype.toString.call(v) === '[object Object]');
                                    ^
RangeError: Maximum call stack size exceeded

Instead I'd expect the following output:

{
  "@context": {
    "@vocab": "http://schema.org/"
  },
  "@graph": [
    {
      "@id": "http://example.com/a",
      "hasPart": {
        "@id": "http://example.com/b"
      }
    },
    {
      "@id": "http://example.com/b",
      "isPartOf": {
        "@id": "http://example.com/a"
      }
    }
  ]
}

I see the same behavior in pyld, so I'm not sure whether this is an implementation problem or an issue with the API spec.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions