Skip to content

Relationships do not work with ES2015 classes #88

@firechicago

Description

@firechicago

When the data for a relationship of an object is passed in as an instance of an ES2015 class, json-api-serializer seems unable to properly read the object, returning a relationship with data: { type: 'sometype', id: '[object Object]' } instead of correctly reading the id and placing the data under included.

See below for full reproduction case:

const JSONAPISerializer = require('json-api-serializer');
const Serializer = new JSONAPISerializer();

Serializer.register('article', {
  id: 'id',
  relationships: { author: { type: 'author' } }
});

Serializer.register('author', {
  id: 'id'
});

class Author {
  constructor({ id, name }) {
    this.id = id;
    this.name = name;
  }
}

const articleData = {
  id: '1',
  author: new Author({
    id: '2',
    name: 'John Jacob Jingleheimer Schmidt'
  })
};

console.log(Serializer.serialize('article', articleData));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions