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

Renders relationship objects even where's no data on included #276

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/deserializer-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ module.exports = function (jsonapi, data, opts) {

function findIncluded(relationshipData, ancestry) {
return new Promise(function (resolve) {
if (!jsonapi.included || !relationshipData) { resolve(null); }
if (!relationshipData) { resolve(null); }

var included = _find(jsonapi.included, {
var included = _find(jsonapi.included || [], {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line is useless, you can call _find with null or undefined 👍

id: relationshipData.id,
type: relationshipData.type
});
Expand All @@ -54,7 +54,7 @@ module.exports = function (jsonapi, data, opts) {
resolve(_extend(attributes, relationships));
});
} else {
return resolve(null);
return resolve({ id: relationshipData.id });
}
});
}
Expand Down
44 changes: 34 additions & 10 deletions test/deserializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,8 @@ describe('JSON API Deserializer', function () {
id: '54735697e16624ba1eee36bf',
'address-line1': '361 Shady Lane',
'zip-code': '23185',
country: 'USA'
country: 'USA',
lock: { id: '2' }
});

done();
Expand Down Expand Up @@ -478,7 +479,7 @@ describe('JSON API Deserializer', function () {
.deserialize(dataSet, function (err, json) {
expect(json).to.be.an('object');

expect(json).to.have.key('id', 'first-name', 'last-name',
expect(json).to.have.key('id', 'first-name', 'last-name',
'username', 'images');

expect(json.images).to.be.an('array').with.length(2)
Expand Down Expand Up @@ -553,7 +554,7 @@ describe('JSON API Deserializer', function () {
id: '2',
type: 'stores',
attributes: {
name: 'Fashionable Clothes'
name: 'Fashionable Clothes'
},
relationships: {
deals: {
Expand Down Expand Up @@ -619,7 +620,7 @@ describe('JSON API Deserializer', function () {
name: 'Twin Pines Mall',
id: '1',
stores: [
{
{
name: 'Tasty Food',
id: '1',
deals: [
Expand All @@ -634,10 +635,10 @@ describe('JSON API Deserializer', function () {
id: '2',
stores: [
{ name: 'Tasty Food', id: '1' }
]
}
]
}
]
}, {
}, {
name: 'Fashionable Clothes',
id: '2',
deals: [
Expand All @@ -649,10 +650,10 @@ describe('JSON API Deserializer', function () {
]
}
]
}, {
}, {
name: 'Readable Books',
id: '3'
}
}
],
deals: [
{
Expand Down Expand Up @@ -696,7 +697,7 @@ describe('JSON API Deserializer', function () {
}
]
}
]
]
});

done(null, json);
Expand Down Expand Up @@ -1130,6 +1131,29 @@ describe('JSON API Deserializer', function () {
done(null, json);
});
});

it('Should include relationship object withonly id', function (done){
var dataSet = _.cloneDeep(baseDataSet);

new JSONAPIDeserializer()
.deserialize(dataSet, function (err, json) {
expect(json).to.be.an('array').with.length(2);
expect(json[0]).to.be.eql({
id: '54735750e16638ba1eee59cb',
'first-name': 'Sandro',
'last-name': 'Munda',
'address': { id: '54735722e16620ba1eee36af' }
});
expect(json[1]).to.be.eql({
id: '5490143e69e49d0c8f9fc6bc',
'first-name': 'Lawrence',
'last-name': 'Bennett',
'address': { id: '54735697e16624ba1eee36bf' }
});

done(null, json);
});
})
});

describe('With empty relationship', function () {
Expand Down