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

Deserialization does not work on repeated included relationships #173

Open
rfblue2 opened this issue Apr 24, 2018 · 5 comments · May be fixed by #240
Open

Deserialization does not work on repeated included relationships #173

rfblue2 opened this issue Apr 24, 2018 · 5 comments · May be fixed by #240

Comments

@rfblue2
Copy link

rfblue2 commented Apr 24, 2018

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"
    }
  ]
}
@AELSchauer
Copy link
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
Copy link

shupac commented Oct 17, 2018

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

@SeyZ
Copy link
Owner

SeyZ commented Oct 28, 2018

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
Copy link

vovkvlad commented Jun 5, 2019

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
Copy link

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.

FbN added a commit to FbN/jsonapi-serializer that referenced this issue Sep 30, 2020
Ancestry check based on type and id.  Resolve SeyZ#173
@FbN FbN linked a pull request Sep 30, 2020 that will close this issue
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

Successfully merging a pull request may close this issue.

6 participants