From 7c897ae0f2483ca89a95af481ebd318ddedf0024 Mon Sep 17 00:00:00 2001 From: Zachary Golba Date: Fri, 20 May 2016 18:27:05 -0400 Subject: [PATCH] fix: multiple 'hasMany' 'type' values are incorrect in serialized data (#109) --- .../social-network/app/serializers/users.js | 1 - src/packages/serializer/index.js | 49 +++++++++---------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/examples/social-network/app/serializers/users.js b/examples/social-network/app/serializers/users.js index 58df853d..36f8a61e 100644 --- a/examples/social-network/app/serializers/users.js +++ b/examples/social-network/app/serializers/users.js @@ -8,7 +8,6 @@ class UsersSerializer extends Serializer { hasMany = [ 'comments', - 'notifications', 'posts', 'reactions' ]; diff --git a/src/packages/serializer/index.js b/src/packages/serializer/index.js index 62089d7c..370e92af 100644 --- a/src/packages/serializer/index.js +++ b/src/packages/serializer/index.js @@ -95,39 +95,36 @@ class Serializer extends Base { records = item[k]; if (records && records.length) { - return { - [k]: { - data: records.map(r => { - id = r.id; - - if (!type) { - type = pluralize(r.modelName); + obj[k] = { + data: records.map(r => { + id = r.id; + type = pluralize(r.modelName); + + if (include.indexOf(k) >= 0) { + if (!relatedSerializer) { + relatedSerializer = r.constructor.serializer; } - if (include.indexOf(k) >= 0) { - if (!relatedSerializer) { - relatedSerializer = r.constructor.serializer; - } - - if (relatedSerializer) { - hash.included.push( - relatedSerializer.serializeOne(r, [], fields) - ); - } + if (relatedSerializer) { + hash.included.push( + relatedSerializer.serializeOne(r, [], fields) + ); } + } - return { - id, - type, + return { + id, + type, - links: { - self: `${domain}/${type}/${id}` - } - }; - }) - } + links: { + self: `${domain}/${type}/${id}` + } + }; + }) }; } + + return obj; }, {}) };