olosegres/jsona

Circular structure jsonapi breaks deserializer

kgathi2 opened this issue · 3 comments

The following JSONAPI response will break the deserializer

{
  data: {
    id: '1',
    type: 'test',
    relationships:{
      questions: {
        data: [{ id: '1', type: 'result' }]
      },
      answers: {
        data: [{ id: '1', type: 'answer' }]
      }
    }
  }, 
  included :[
    {
      id: '1',
      type: 'question',
      attributes: {
        one: '1',
        two: '2'
      },
      relationships:{
        test :{
          data: {id: '1', type: 'answer'}
        }
      }
    },
    {
      id: '1',
      type: 'answer',
      attributes: {
        three: '3',
        four: '4'
      },
      relationships:{
        test :{
          data: {id: '1', type: 'question'}
        }
      }
    },
  ]
}

Error is

TypeError: Converting circular structure to JSON
        --> starting at object with constructor 'Object'
        |     property 'test' -> object with constructor 'Object'
        --- property 'test' closes the circle
        at stringify (<anonymous>)

It's bsaiclly trying to do test.question.answer.question.answer.... i am guessing.

Perhaps have a limit to the depth of an option for depth to stop the circular reference.

Not sure why , but this behaviour is intermittent. Now checking my tests in my app and all are passing where i expect the error. Using the below bring error on and off.

{
  "data": {
    "id": "1677066013",
    "type": "session",
    "attributes": {
      "user": 1,
      "user_name": "qanisa",
      "user_confirmed": true,
      "ctas": {
        "share_app": "pending",
        "create_apps": "pending",
        "use_sandbox": "done",
        "create_integrations": "pending"
      }
    },
    "relationships": {
      "workspaces": {
        "data": [{ "id": "1", "type": "workspace" }],
        "meta": { "total": 1 },
        "links": { "related": "https://example.com/workspaces" }
      },
      "messages": {
        "data": [{ "id": "1", "type": "message" }],
        "meta": { "total": 1 },
        "links": { "related": "https://example.com/messages" }
      },
      "received_invites": {
        "data": [],
        "meta": { "total": 0 },
        "links": { "related": "https://example.com/invites" }
      },
      "sent_invites": {
        "data": [],
        "meta": { "total": 0 },
        "links": { "related": "https://example.com/invites" }
      },
      "user": {
        "data": { "id": "1", "type": "user" },
        "links": { "self": "https://example.com/users/1" }
      }
    },
    "links": { "self": "https://example.com/auth/user" }
  },
  "included": [
    {
      "id": "1",
      "type": "workspace",
      "attributes": {
        "name": "qanisa",
        "signup_voucher_id": null,
        "wallet_balance": "0.0",
        "voucher_balance": "0.0",
        "settings": null
      },
      "relationships": {
        "memberships": {
          "data": [{ "id": "1", "type": "membership" }],
          "links": { "related": "https://example.com/memberships" }
        },
        "users": {
          "data": [{ "id": "1", "type": "user" }],
          "links": { "related": "https://example.com/users" }
        },
        "invites": {
          "data": [],
          "links": { "related": "https://example.com/invites" }
        }
      },
      "links": { "self": "https://example.com/workspaces/1" }
    },
    {
      "id": "1",
      "type": "message",
      "attributes": { "body": null, "status": "unread", "category": "devise" },
      "relationships": { "user": { "data": { "id": "1", "type": "user" } } },
      "links": { "self": "https://example.com/messages/1" }
    },
    {
      "id": "1",
      "type": "user",
      "attributes": {
        "email": "qanisa@mailinator.com",
        "name": "qanisa",
        "mobile_no": null,
        "invitation_limit": 5,
        "unconfirmed_email": null,
        "email_confirmed": true
      },
      "relationships": {
        "memberships": {
          "data": [{ "id": "1", "type": "membership" }],
          "links": { "related": "https://example.com/memberships" }
        },
        "workspaces": {
          "data": [{ "id": "1", "type": "workspace" }],
          "links": { "related": "https://example.com/workspaces" }
        },
        "received_invites": {
          "data": [],
          "links": { "related": "https://example.com/invites" }
        },
        "sent_invites": {
          "data": [],
          "links": { "related": "https://example.com/invites" }
        },
        "messages": {
          "data": [{ "id": "1", "type": "message" }],
          "links": { "related": "https://example.com/messages" }
        }
      },
      "links": { "self": "https://example.com/users/1" }
    }
  ]
}

Any clue what's up?

Hi,
Serializing via JSON.stringify is not supported for objects with circular relationships.
You need to serialize it using jsona.serialize({ stuff, includeNames }) first, then you can serialize the result using JSON.stringify.

Similar issue #3