Skip to content

Commit

Permalink
Serialize empty hasMany relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanto committed May 8, 2018
1 parent 029fbfd commit bff2093
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 19 deletions.
36 changes: 17 additions & 19 deletions addon/serializers/json-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,31 +516,29 @@ const JSONAPISerializer = JSONSerializer.extend({
if (this.shouldSerializeHasMany(snapshot, key, relationship)) {
let hasMany = snapshot.hasMany(key);
if (hasMany !== undefined) {
// only serialize has many relationships that are not new
let nonNewHasMany = hasMany.filter(item => item.record && !item.record.get('isNew'));

if (nonNewHasMany.length > 0) {
json.relationships = json.relationships || {};

let payloadKey = this._getMappedKey(key, snapshot.type);
if (payloadKey === key && this.keyForRelationship) {
payloadKey = this.keyForRelationship(key, 'hasMany', 'serialize');
}
json.relationships = json.relationships || {};

let data = new Array(nonNewHasMany.length);
let payloadKey = this._getMappedKey(key, snapshot.type);
if (payloadKey === key && this.keyForRelationship) {
payloadKey = this.keyForRelationship(key, 'hasMany', 'serialize');
}

for (let i = 0; i < nonNewHasMany.length; i++) {
let item = hasMany[i];
let payloadType = this.payloadKeyFromModelName(item.modelName);
// only serialize has many relationships that are not new
let nonNewHasMany = hasMany.filter(item => item.record && !item.record.get('isNew'));
let data = new Array(nonNewHasMany.length);

data[i] = {
type: payloadType,
id: item.id
};
for (let i = 0; i < nonNewHasMany.length; i++) {
let item = hasMany[i];
let payloadType = this.payloadKeyFromModelName(item.modelName);

json.relationships[payloadKey] = { data };
}
data[i] = {
type: payloadType,
id: item.id
};
}

json.relationships[payloadKey] = { data };
}
}
}
Expand Down
59 changes: 59 additions & 0 deletions tests/integration/serializers/json-api-serializer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,65 @@ test('it should not include any records when serializing a hasMany relationship
'first-name': null,
'last-name': null,
title: null
},
relationships: {
handles: {
data: []
}
}
}
});
});
});

test('it should include an empty list when serializing an empty hasMany relationship', function(assert) {
env.registry.register("serializer:user", DS.JSONAPISerializer.extend({
attrs: {
handles: { serialize: true }
}
}));

run(function() {
serializer.pushPayload(store, {
data: {
type: 'users',
id: 1,
relationships: {
handles: {
data: [
{ type: 'handles', id: 1 },
{ type: 'handles', id: 2 }
]
}
}
},
included: [
{ type: 'handles', id: 1 },
{ type: 'handles', id: 2 }
]
});

let user = store.peekRecord('user', 1);
let handle1 = store.peekRecord('handle', 1);
let handle2 = store.peekRecord('handle', 2);
user.get('handles').removeObject(handle1);
user.get('handles').removeObject(handle2);

let serialized = user.serialize({ includeId: true });

assert.deepEqual(serialized, {
data: {
type: 'users',
id: '1',
attributes: {
'first-name': null,
'last-name': null,
title: null
},
relationships: {
handles: {
data: []
}
}
}
});
Expand Down

0 comments on commit bff2093

Please sign in to comment.