Skip to content

Commit 97f71f6

Browse files
committed
fix serializeRelationship with classes
resolves #88
1 parent 66f19e5 commit 97f71f6

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

Diff for: lib/JSONAPISerializer.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const {
33
isEmpty,
44
omit,
55
isPlainObject,
6+
isObjectLike,
67
transform,
78
get,
89
set,
@@ -694,7 +695,7 @@ module.exports = class JSONAPISerializer {
694695
const serializedRelationship = { type };
695696

696697
// Support for unpopulated relationships (an id, or array of ids)
697-
if (!isPlainObject(rData)) {
698+
if (!isObjectLike(rData)) {
698699
serializedRelationship.id = rData.toString();
699700
} else {
700701
serializedRelationship.id = rData[rOptions.id].toString();

Diff for: lib/helpers.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const {
33
isEmpty,
44
omit,
55
isPlainObject,
6+
isObjectLike,
67
transform,
78
toKebabCase,
89
toSnakeCase,
@@ -24,6 +25,7 @@ module.exports = {
2425
isEmpty,
2526
omit,
2627
isPlainObject,
28+
isObjectLike,
2729
transform,
2830
toKebabCase,
2931
toSnakeCase,

Diff for: test/unit/JSONAPISerializer.test.js

+32
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,38 @@ describe('JSONAPISerializer', function() {
485485
]);
486486
done();
487487
});
488+
489+
it('should serialize relationship with classes', function(done) {
490+
const included = new Map();
491+
const typeFn = (data) => data.type;
492+
493+
const Serializer = new JSONAPISerializer();
494+
Serializer.register('author');
495+
class Author {
496+
constructor({ id, name }) {
497+
this.id = id;
498+
this.name = name;
499+
}
500+
}
501+
502+
const data = new Author({
503+
id: '1',
504+
name: 'Kaley Maggio'
505+
});
506+
507+
const serializedRelationships = Serializer.serializeRelationship('author', 'default', data, included);
508+
expect(serializedRelationships).to.deep.equal({ type: 'author', id: '1' });
509+
expect([...included.values()]).to.deep.equal([
510+
{
511+
type: 'author',
512+
id: '1',
513+
attributes: { name: 'Kaley Maggio' },
514+
relationships: undefined,
515+
meta: undefined,
516+
links: undefined
517+
}]);
518+
done();
519+
});
488520
});
489521

490522
describe('serializeRelationships', function() {

0 commit comments

Comments
 (0)