Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize empty hasMany relationships #5466

Merged
merged 1 commit into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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