question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deserialization does not work on repeated included relationships

See original GitHub issue

If there are relationships that share resources, and are included only once each (see the example below - there are only two books but one is both a “selling” and a “favorite”) when they are deserialized the second instance of the repeated relationship is not properly included (it just places null)

{
    "data": {
        "type": "user",
        "id": "5ad2601522c6f6733408ae1f",
        "attributes": {...},
        "relationships": {
            "favorite": {
                "data": [
                    {
                        "type": "book",
                        "id": "5aca58621d13177065279691"
                    }
                ]
            },
            "selling": {
                "data": [
                    {
                        "type": "book",
                        "id": "5ac8dd784f904b5a281aa061"
                    },
                    {
                        "type": "book",
                        "id": "5aca58621d13177065279691"
                    }
                ]
            }
        }
    },
    "included": [
        {
            "type": "book",
            "id": "5aca58621d13177065279691",
            "attributes": {...},
        },
        {
            "type": "book",
            "id": "5ac8dd784f904b5a281aa061",
            "attributes": {...}
        }
    ]
}

When deserialized, this returns

{
  ...
  "id": "5ad2601522c6f6733408ae1f",
  "selling": [
    {
      ...
      "id": "5ac8dd784f904b5a281aa061"
    },
    null <-- Relationship was included but presumably used up for something else!
  ],
  "favorite": [
    {
      ...
      "id": "5aca58621d13177065279691"
    }
  ]
}

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:6
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
AELSchauercommented, May 3, 2018

This is a known issue with this repo but the author hasn’t done anything to publicly address it. I forked this repo to fix this issue. You can view the PR here: https://github.com/SeyZ/jsonapi-serializer/pull/170

And you can view my fork here: https://github.com/AELSchauer/jsonapi-serializer

0reactions
konovalov-nkcommented, Aug 8, 2019

Can confirm. Just spent ~6 hours figuring this out + 2 hours on adapting solution by @AELSchauer (thanks a lot!). Glad this issue was already pointed out or I’d have to spend even more time thinking I’ve gone mad 😄

Basically, having circular dependencies on your resources is not possible due to references evaluated as null.

A simple example would be the following JSONAPI response:

{
   "data": {
      "type": "product",
      "id": "1",
      "attributes": {
         "name": "Product 1"
      },
      "relationships": {
         "related": {
            "data": [
               {
                  "type": "product",
                  "id": "2"
               }
            ]
         }
      }
   },
   "included": [
      {
         "type": "product",
         "id": "2",
         "attributes": {
            "name": "Product 2"
         },
         "relationships": {
            "related": {
               "data": [
                  {
                     "type": "product",
                     "id": "1"
                  }
               ]
            }
         }
      }
   ]
}

https://github.com/SeyZ/jsonapi-serializer/commit/4e7001663493e124040de228349e1ac4e6ba0890 without a patch produces following result:

{
  "name": "Product 1",
  "id": "1",
  "related": [
    {
      "name": "Product 2",
      "id": "2",
      "related": [
        null
      ]
    }
  ]
}

Demonstration available here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to deserialize one to many relationship with both eager ...
When I query for product, I want product with ProductGroup (ProductGroup should not serialize again the products inside) ...
Read more >
Jackson - Bidirectional Relationships - Baeldung
First, we'll discuss the Jackson JSON infinite recursion problem. Then we'll see how to serialize entities with bidirectional relationships.
Read more >
Bulletproof Interface Deserialization in Json.NET
NET provides a simple setting to solve the problem of deserializing a property that is an interface with multiple implementing classes:
Read more >
[C #] Some scenarios for deserializing a JSON to a type with ...
Recently I feel that using the System.Text.Json library for serializing/deserializing with JSON is increasing in C# programming.
Read more >
pickle — Python object serialization — Python 3.11.1 ...
Object sharing happens when there are multiple references to the same object in ... Unlike pickle, deserializing untrusted JSON does not in itself...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found