Skip to content

Commit

Permalink
fix: serialize hasMany relationships (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharygolba committed Apr 28, 2016
1 parent 2f5aa41 commit b94c526
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 33 deletions.
70 changes: 37 additions & 33 deletions src/packages/serializer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Serializer extends Base {
relationshipsFor(item, include, fields) {
const { domain, hasOne, hasMany } = this;
const hash = { data: {}, included: [] };
let i, _i, id, key, type, records, related, relatedSerializer;
let i, id, key, type, records, related, relatedSerializer;

for (i = 0; i < hasOne.length; i++) {
key = hasOne[i];
Expand Down Expand Up @@ -86,48 +86,52 @@ class Serializer extends Base {
}
}

for (i = 0; i < hasMany.length; i++) {
key = hasMany[i];
records = item[key];
hash.data = {
...hash.data,

if (records && records.length) {
hash.data[key] = [];
records = records.slice();
...hasMany.reduce((group, relatedKey) => {
records = item[relatedKey];

for (_i = 0; i < records.length; _i++) {
related = records[_i];

if (related) {
id = related.id;
if (!records || !records.length) {
return group;
}

if (!type) {
type = pluralize(related.getModelName());
}
return {
...group,

hash.data[key][i] = {
id,
type,
[relatedKey]: {
data: records.map(record => {
id = record.id;

links: {
self: `${domain}/${type}/${id}`
if (!type) {
type = pluralize(record.getModelName());
}
};

if (include.indexOf(key) >= 0) {
if (!relatedSerializer) {
relatedSerializer = this.serializers.get(type);
}
if (include.indexOf(relatedKey) >= 0) {
if (!relatedSerializer) {
relatedSerializer = this.serializers.get(type);
}

if (relatedSerializer) {
hash.included.push(
relatedSerializer.serializeOne(related, [], fields)
);
if (relatedSerializer) {
hash.included.push(
relatedSerializer.serializeOne(record, [], fields)
);
}
}
}

return {
id,
type,

links: {
self: `${domain}/${type}/${id}`
}
};
})
}
}
}
}
};
}, {})
};

return hash;
}
Expand Down
4 changes: 4 additions & 0 deletions test/test-app/app/serializers/authors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ class AuthorsSerializer extends Serializer {
'name',
'createdAt'
];

hasMany = [
'posts'
];
}

export default AuthorsSerializer;
25 changes: 25 additions & 0 deletions test/unit/packages/serializer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expect } from 'chai';
import fetch from 'isomorphic-fetch';

const host = 'http://localhost:4000';

describe('Unit: class Serializer ', () => {
describe('Regression: #relationshipsFor() (https://github.com/postlight/lux/issues/59)', () => {
it('can serialize hasMany relationships', async () => {
const {
data: [
{
relationships: {
posts: {
data: posts
}
}
}
]
} = await (await fetch(`${host}/authors`)).json();

expect(posts).to.be.an.instanceOf(Array);
expect(posts).to.have.length.above(0);
});
});
});

0 comments on commit b94c526

Please sign in to comment.