From 7115fa966eaac5cc57f29d5504236e9d6492521b Mon Sep 17 00:00:00 2001 From: Felix Fichte Date: Sun, 26 Jan 2020 14:50:33 +0100 Subject: [PATCH 1/2] Add Failing Tests --- test/serializer.js | 218 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 218 insertions(+) diff --git a/test/serializer.js b/test/serializer.js index 22ed0e4..69a18cc 100644 --- a/test/serializer.js +++ b/test/serializer.js @@ -1152,6 +1152,96 @@ describe('JSON API Serializer', function () { } }); + done(null, json); + }); + it('should be set into the `data.relationships` and `included` for Classes as well', function (done) { + class Author{ + constructor(id, firstName, lastName){ + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + } + } + let vance = new Author('2934f384bb824a7cb7b238b8dc194a22', 'Ashlee', 'Vance'); + let isaacson = new Author('5ed95269a8334d8a970a2bd9fa599288', 'Walter', 'Isaacson'); + var dataSet = [{ + id: '54735750e16638ba1eee59cb', + firstName: 'Sandro', + lastName: 'Munda', + books: [{ + id: '52735730e16632ba1eee62dd', + title: 'Tesla, SpaceX, and the Quest for a Fantastic Future', + isbn: '978-0062301239', + author: vance + }, { + id: '52735780e16610ba1eee15cd', + title: 'Steve Jobs', + isbn: '978-1451648546', + author: isaacson + }] + }]; + + var json = new JSONAPISerializer('users', { + attributes: ['firstName', 'lastName', 'books'], + books: { + ref: 'id', + attributes: ['title', 'isbn', 'author'], + author: { + ref: 'id', + attributes: ['firstName', 'lastName'] + } + } + }).serialize(dataSet); + + expect(json.included).to.include({ + type: 'books', + id: '52735730e16632ba1eee62dd', + attributes: { + title: 'Tesla, SpaceX, and the Quest for a Fantastic Future', + isbn: '978-0062301239' + }, + relationships: { + author: { + data: { id: '2934f384bb824a7cb7b238b8dc194a22', type: 'authors' } + } + } + }); + + expect(json.included).to.include({ + type: 'books', + id: '52735780e16610ba1eee15cd', + attributes: { + title: 'Steve Jobs', + isbn: '978-1451648546' + }, + relationships: { + author: { + data: { + id: '5ed95269a8334d8a970a2bd9fa599288', + type: 'authors' + } + } + } + }); + + expect(json.included).to.include({ + id: '2934f384bb824a7cb7b238b8dc194a22', + type: 'authors', + attributes: { + 'first-name': 'Ashlee', + 'last-name': 'Vance' + } + }); + + expect(json.included).to.include({ + id: '5ed95269a8334d8a970a2bd9fa599288', + type: 'authors', + attributes: { + 'first-name': 'Walter', + 'last-name': 'Isaacson' + } + }); + done(null, json); }); }); @@ -1225,6 +1315,77 @@ describe('JSON API Serializer', function () { } }); + done(null, json); + }); + it('should be set into the `data.relationships` and `included for Classes as well`', function (done) { + class Author{ + constructor(id, firstName, lastName){ + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + } + } + let vance = new Author('2934f384bb824a7cb7b238b8dc194a22', 'Ashlee', 'Vance'); + let isaacson = new Author('5ed95269a8334d8a970a2bd9fa599288', 'Walter', 'Isaacson'); + var dataSet = [{ + id: '54735750e16638ba1eee59cb', + firstName: 'Sandro', + lastName: 'Munda', + books: [{ + id: '52735730e16632ba1eee62dd', + title: 'Tesla, SpaceX, and the Quest for a Fantastic Future', + isbn: '978-0062301239', + authors: [vance, isaacson] + }] + }]; + + var json = new JSONAPISerializer('users', { + attributes: ['firstName', 'lastName', 'books'], + books: { + ref: 'id', + attributes: ['title', 'isbn', 'authors'], + authors: { + ref: 'id', + attributes: ['firstName', 'lastName'] + } + } + }).serialize(dataSet); + + expect(json.included).to.include({ + type: 'books', + id: '52735730e16632ba1eee62dd', + attributes: { + title: 'Tesla, SpaceX, and the Quest for a Fantastic Future', + isbn: '978-0062301239' + }, + relationships: { + authors: { + data: [ + { id: '2934f384bb824a7cb7b238b8dc194a22', type: 'authors' }, + { id: '5ed95269a8334d8a970a2bd9fa599288', type: 'authors' }, + ] + } + } + }); + + expect(json.included).to.include({ + id: '2934f384bb824a7cb7b238b8dc194a22', + type: 'authors', + attributes: { + 'first-name': 'Ashlee', + 'last-name': 'Vance' + } + }); + + expect(json.included).to.include({ + id: '5ed95269a8334d8a970a2bd9fa599288', + type: 'authors', + attributes: { + 'first-name': 'Walter', + 'last-name': 'Isaacson' + } + }); + done(null, json); }); }); @@ -1282,6 +1443,63 @@ describe('JSON API Serializer', function () { attributes: { 'first-name': 'Lawrence', 'last-name': 'Bennett' } }); + done(null, json); + }); + it('should be set into the `data.relationships` and `included for Classes as well`', function (done) { + class Neighbour{ + constructor(id, firstName, lastName){ + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + } + } + let bennett = new Neighbour('5490143e69e49d0c8f9fc6bc', 'Lawrence', 'Bennett'); + var dataSet = [{ + id: '54735750e16638ba1eee59cb', + firstName: 'Sandro', + lastName: 'Munda', + address: { + id: '5cd95269a8334d8a970a2bd9fa599278', + addressLine1: '406 Madison Court', + zipCode: '49426', + country: 'USA', + neighbours: [bennett] + } + }]; + + var json = new JSONAPISerializer('users', { + attributes: ['firstName', 'lastName', 'address'], + address: { + ref: 'id', + attributes: ['addressLine1', 'zipCode', 'country', 'neighbours'], + neighbours: { + ref: 'id', + attributes: ['firstName', 'lastName'], + } + } + }).serialize(dataSet); + + expect(json.included).to.include({ + id: '5cd95269a8334d8a970a2bd9fa599278', + type: 'addresses', + attributes: { + 'address-line1': '406 Madison Court', + 'zip-code': '49426', + country: 'USA' + }, + relationships: { + neighbours: { + data: [{ type: 'neighbours', id: '5490143e69e49d0c8f9fc6bc' }] + } + } + }); + + expect(json.included).to.include({ + type: 'neighbours', + id: '5490143e69e49d0c8f9fc6bc', + attributes: { 'first-name': 'Lawrence', 'last-name': 'Bennett' } + }); + done(null, json); }); }); From 120d221df354e0ec574033bb0be91668a76dce0e Mon Sep 17 00:00:00 2001 From: Felix Fichte Date: Sun, 26 Jan 2020 14:52:54 +0100 Subject: [PATCH 2/2] Add fix for compound relationships --- lib/serializer-utils.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/serializer-utils.js b/lib/serializer-utils.js index c37520e..8bb8436 100644 --- a/lib/serializer-utils.js +++ b/lib/serializer-utils.js @@ -1,5 +1,6 @@ 'use strict'; var isPlainObject = require('lodash/isPlainObject'); +var isObjectLike = require('lodash/isObjectLike'); var isFunction = require('lodash/isFunction'); var _find = require('lodash/find'); var _merge = require('lodash/merge'); @@ -16,7 +17,7 @@ var Inflector = require('./inflector'); module.exports = function (collectionName, record, payload, opts) { function isComplexType(obj) { - return Array.isArray(obj) || isPlainObject(obj); + return Array.isArray(obj) || isObjectLike(obj); } function keyForAttribute(attribute) {