Skip to content

Commit 85d9068

Browse files
committed
Support the 'id' option in the deserializer. Closes #100
1 parent d6d3060 commit 85d9068

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

lib/deserializer-utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ module.exports = function (jsonapi, data, opts) {
8383

8484
function extractAttributes(from) {
8585
var dest = keyForAttribute(from.attributes || {});
86-
if ('id' in from) { dest.id = from.id; }
86+
if ('id' in from) { dest[opts.id || 'id'] = from.id; }
87+
8788
if (opts.typeAsAttribute) {
8889
if ('type' in from) { dest.type = from.type; }
8990
}

test/deserializer.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,4 +1033,31 @@ describe('JSON API Deserializer', function () {
10331033
});
10341034
});
10351035
});
1036+
1037+
describe('id', function () {
1038+
it('should override the id field', function (done) {
1039+
var dataSet = {
1040+
data: [{
1041+
type: 'users',
1042+
id: '54735750e16638ba1eee59cb',
1043+
attributes: { 'first-name': 'Sandro', 'last-name': 'Munda' }
1044+
}, {
1045+
type: 'users',
1046+
id: '5490143e69e49d0c8f9fc6bc',
1047+
attributes: { 'first-name': 'Lawrence', 'last-name': 'Bennett' }
1048+
}]
1049+
};
1050+
1051+
new JSONAPIDeserializer({
1052+
id: '_id'
1053+
}).deserialize(dataSet, function (err, json) {
1054+
expect(json[0]).to.not.have.keys('id');
1055+
expect(json[1]).to.not.have.keys('id');
1056+
expect(json[0]._id).equal('54735750e16638ba1eee59cb');
1057+
expect(json[1]._id).equal('5490143e69e49d0c8f9fc6bc');
1058+
done(null, json);
1059+
});
1060+
1061+
});
1062+
});
10361063
});

0 commit comments

Comments
 (0)