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 structure jsonapi breaks deserializer #62

Closed
kgathi2 opened this issue Feb 22, 2023 · 3 comments
Closed

Circular structure jsonapi breaks deserializer #62

kgathi2 opened this issue Feb 22, 2023 · 3 comments

Comments

@kgathi2
Copy link

kgathi2 commented Feb 22, 2023

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.

@kgathi2
Copy link
Author

kgathi2 commented Feb 23, 2023

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?

@kgathi2
Copy link
Author

kgathi2 commented Feb 23, 2023

@olosegres
Copy link
Owner

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

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