jsonapi-serializer icon indicating copy to clipboard operation
jsonapi-serializer copied to clipboard

Nested relationships are not deserialized properly if resource has multiple relations

Open Skysplit opened this issue 8 years ago • 4 comments

There's a problem with includes reusability. It seems that once resource is included, it is no longer available to deserializer

Input

{
  data: [
    {
      type: 'posts',
      id: 1,
      attributes: {
        name: 'foo',
      },
      relationships: {
        author: {
          data: {
            type: 'users',
            id: 1,
          }
        },
        comments: {
          data: [
            {
              type: 'comments',
              id: 1
            }, {
              type: 'comments',
              id: 2,
            }, {
              type: 'comments',
              id: 3
            },
          ]
        },
      },
    },
  ],
  included: [
    {
      type: 'users',
      id: 1,
      attributes: {
        name: 'John',
      },
    },
    {
      type: 'users',
      id: 2,
      attributes: {
        name: 'Jane'
      },
    },
    {
      type: 'comments',
      id: 1,
      attributes: {
        content: 'lorem',
      },
      relationships: {
        author: {
          data: {
            type: 'users',
            id: 1,
          },
        },
      }
    },
    {
      type: 'comments',
      id: 2,
      attributes: {
        content: 'ipsum',
      },
      relationships: {
        author: {
          data: {
            type: 'users',
            id: 2,
          },
        },
      }
    },
    {
      type: 'comments',
      id: 3,
      attributes: {
        content: 'dolor',
      },
      relationships: {
        author: {
          data: {
            type: 'users',
            id: 2,
          },
        },
      }
    },
  ],
}

Expected output

[
  {
    name: 'foo',
    id: 1,
    author: {
      name: 'John', id: 1
    },
    comments: [
      { content: 'lorem', id: 1, author: { name: 'John', id: 1 } },
      { content: 'ipsum', id: 2, author: { name: 'Jane', id: 2 } },
      { content: 'dolor', id: 3, author: { name: 'Jane', id: 2 } }
    ]
  }
]

Actual output

[
  {
    name: 'foo',
    id: 1,
    author: { name: 'John', id: 1 },
    comments: [
      { content: 'lorem', id: 1 },
      { content: 'ipsum', id: 2, author: { name: 'Jane', id: 2 } },
      { content: 'dolor', id: 3 }
    ]
  }
]

Skysplit avatar May 12 '17 19:05 Skysplit

+1

leoskyrocker avatar May 17 '17 01:05 leoskyrocker

This problem solved in similar lib

olosegres avatar Jun 13 '17 19:06 olosegres

jsona is amazing! it solved my issue, thanks @olosegres ! bye bye jsonapi-serializer

charly-palencia avatar Feb 04 '18 17:02 charly-palencia

Hey all, I solved this issue with my PR #170. I'm not sure when (or if) it will be merged, but you can access my forked repo here -- https://github.com/AELSchauer/jsonapi-serializer

AELSchauer avatar Mar 15 '18 22:03 AELSchauer