diff --git a/addon/serializers/json-api.js b/addon/serializers/json-api.js index 24f16827522..b9c587f11aa 100644 --- a/addon/serializers/json-api.js +++ b/addon/serializers/json-api.js @@ -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 }; } } } diff --git a/tests/integration/serializers/json-api-serializer-test.js b/tests/integration/serializers/json-api-serializer-test.js index 157aa85903b..5ec6e7cb08a 100644 --- a/tests/integration/serializers/json-api-serializer-test.js +++ b/tests/integration/serializers/json-api-serializer-test.js @@ -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: [] + } } } });