Skip to content

Deserialization does not work on repeated included relationships #173

@rfblue2

Description

@rfblue2

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"
    }
  ]
}

Activity

AELSchauer

AELSchauer commented on May 3, 2018

@AELSchauer
Contributor

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:
#170

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

shupac

shupac commented on Oct 17, 2018

@shupac

just ran into this issue. @AELSchauer I checked out your branch but it wasn't working for me

SeyZ

SeyZ commented on Oct 28, 2018

@SeyZ
Owner

I'm currently trying to reproduce this issue on my side and everything looks fine:

    describe('FOO', function () {
      it('should return all data without circular error', function (done) {
        var dataSet = {
          data: {
            type: 'users',
            id: '54735750e16638ba1eee59cb',
            attributes: { 'first-name': 'Sandro', 'last-name': 'Munda' },
            relationships: {
              favorite: {
                data: [{
                  type: 'book',
                  id: '5aca58621d13177065279691'
                }]
              },
              selling: {
                data: [{
                  type: 'book',
                  id: '5ac8dd784f904b5a281aa061'
                }, {
                  type: 'book',
                  id: '5aca58621d13177065279691'
                }]
              }
            }
          },
          included: [{
            type: 'book',
            id: '5ac8dd784f904b5a281aa061',
            attributes: {
              'book-title': 'Tesla, SpaceX.',
              isbn: '978-0062301239'
            }
          }, {
            type: 'book',
            id: '5aca58621d13177065279691',
            attributes: {
              'book-title': 'Steve Jobs',
              isbn: '978-1451648546'
            }
          }]
        };

        new JSONAPIDeserializer()
         .deserialize(dataSet, function (err, json) {
           console.log(json);
           done(null, json);
         });
      });
    });

OUTPUT:

{ 'first-name': 'Sandro',
  'last-name': 'Munda',
  id: '54735750e16638ba1eee59cb',
  favorite: 
   [ { 'book-title': 'Steve Jobs',
       isbn: '978-1451648546',
       id: '5aca58621d13177065279691' } ],
  selling: 
   [ { 'book-title': 'Tesla, SpaceX.',
       isbn: '978-0062301239',
       id: '5ac8dd784f904b5a281aa061' },
     { 'book-title': 'Steve Jobs',
       isbn: '978-1451648546',
       id: '5aca58621d13177065279691' } ] }

Any help to reproduce ?

vovkvlad

vovkvlad commented on Jun 5, 2019

@vovkvlad

I have a pretty similar issue, but without null - it just does not include nested relationship for the second time - it's just omitted. It looks that as if it has already used it for the first time, and in the second time - it's not there. Reroducible code is here:
https://codesandbox.io/s/jsonapiserializer-bug-wez5c?fontsize=14

konovalov-nk

konovalov-nk commented on Aug 8, 2019

@konovalov-nk

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"
                  }
               ]
            }
         }
      }
   ]
}

4e70016 without a patch produces following result:

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

Demonstration available here.

added a commit that references this issue on Sep 30, 2020
linked a pull request that will close this issue on Sep 30, 2020
added a commit that references this issue on Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @SeyZ@rfblue2@shupac@vovkvlad@konovalov-nk

      Issue actions

        Deserialization does not work on repeated included relationships · Issue #173 · SeyZ/jsonapi-serializer