Skip to content

Commit 7c1dcff

Browse files
committed
Serializer will return null when id is undefined
Serializer used to return null when there were no attributes. This caused problems for creating models that only had relationships. I changed the serializer to return null when id is undefined. This broke include tests because they expected customMethod to be able to return attributes without an id. JSONAPI require id to be present in a resource document, but the id can be null. Note: This commit changes expectation of customMethod to return an id, which can be null.
1 parent acc14c3 commit 7c1dcff

File tree

3 files changed

+32
-32
lines changed

3 files changed

+32
-32
lines changed

lib/serializer.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ function parseResource (type, data, relations, options) {
127127
}
128128
});
129129

130-
// If there is no attributes found return
131-
if (_.isEmpty(attributes)) {
130+
if (_.isUndefined(resource.id)) {
132131
return null;
133132
}
134133

test/include.test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ describe('include option', function () {
1515
Post = ds.createModel('post', { title: String });
1616
app.model(Post);
1717
Post.customMethod = function (cb) {
18-
cb(null, {prop: 'value'});
18+
cb(null, {prop: 'value', id: null});
1919
};
2020
Post.remoteMethod('customMethod', {
2121
http: {verb: 'get', path: '/custom'},
2222
returns: {root: true}
2323
});
2424
Post.customMethod2 = function (cb) {
25-
cb(null, {prop: 'value'});
25+
cb(null, {prop: 'value', id: null});
2626
};
2727
Post.remoteMethod('customMethod2', {
2828
http: {verb: 'get', path: '/custom2'},
@@ -32,7 +32,7 @@ describe('include option', function () {
3232
Comment = ds.createModel('comment', { comment: String });
3333
app.model(Comment);
3434
Comment.customMethod = function (cb) {
35-
cb(null, {prop: 'value'});
35+
cb(null, {prop: 'value', id: null});
3636
};
3737
Comment.remoteMethod('customMethod', {
3838
http: {verb: 'get', path: '/custom'},
@@ -60,7 +60,7 @@ describe('include option', function () {
6060
.expect(200)
6161
.end(function (err, res) {
6262
expect(err).to.equal(null);
63-
expect(res.body.data).to.have.keys('type', 'attributes', 'links');
63+
expect(res.body.data).to.have.keys('id', 'type', 'attributes', 'links');
6464
done();
6565
});
6666
});
@@ -70,7 +70,7 @@ describe('include option', function () {
7070
.expect(200)
7171
.end(function (err, res) {
7272
expect(err).to.equal(null);
73-
expect(res.body.data).to.have.keys('type', 'attributes', 'links');
73+
expect(res.body.data).to.have.keys('id', 'type', 'attributes', 'links');
7474
done();
7575
});
7676
});
@@ -90,7 +90,7 @@ describe('include option', function () {
9090
.expect(200)
9191
.end(function (err, res) {
9292
expect(err).to.equal(null);
93-
expect(res.body.data).to.have.keys('type', 'attributes', 'links');
93+
expect(res.body.data).to.have.keys('id', 'type', 'attributes', 'links');
9494
done();
9595
});
9696
});
@@ -100,7 +100,7 @@ describe('include option', function () {
100100
.expect(200)
101101
.end(function (err, res) {
102102
expect(err).to.equal(null);
103-
expect(res.body).to.deep.equal({ prop: 'value'});
103+
expect(res.body).to.deep.equal({ prop: 'value', id: null });
104104
done();
105105
});
106106
});
@@ -120,7 +120,7 @@ describe('include option', function () {
120120
.expect(200)
121121
.end(function (err, res) {
122122
expect(err).to.equal(null);
123-
expect(res.body.data).to.have.keys('type', 'attributes', 'links');
123+
expect(res.body.data).to.have.keys('id', 'type', 'attributes', 'links');
124124
done();
125125
});
126126
});
@@ -130,7 +130,7 @@ describe('include option', function () {
130130
.expect(200)
131131
.end(function (err, res) {
132132
expect(err).to.equal(null);
133-
expect(res.body.data).to.have.keys('type', 'attributes', 'links');
133+
expect(res.body.data).to.have.keys('id', 'type', 'attributes', 'links');
134134
done();
135135
});
136136
});

test/throughModel.test.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@ var expect = require('chai').expect;
44
var JSONAPIComponent = require('../');
55
var app, User, Interest, Topic;
66

7-
describe('through Model', function(){
8-
7+
describe('through Model', function () {
8+
99
beforeEach(function () {
1010
app = loopback();
1111
app.set('legacyExplorer', false);
12-
ds = loopback.createDataSource('memory');
12+
var ds = loopback.createDataSource('memory');
1313

1414
User = ds.createModel('user', {
1515
id: {type: Number, id: true},
1616
name: String
1717
});
18-
18+
1919
app.model(User);
20-
20+
2121
Topic = ds.createModel('topic', {
2222
id: {type: Number, id: true},
2323
name: String
2424
});
2525

2626
app.model(Topic);
27-
27+
2828
Interest = ds.createModel('interest', {
2929
id: {type: Number, id: true}
3030
});
@@ -33,43 +33,44 @@ describe('through Model', function(){
3333

3434
User.hasMany(Topic, { through: Interest });
3535
Topic.hasMany(User, { through: Interest });
36-
36+
3737
Interest.belongsTo(User);
3838
Interest.belongsTo(Topic);
39-
39+
4040
app.use(loopback.rest());
4141
JSONAPIComponent(app, {restApiRoot: '/'});
4242
});
4343

44-
it('should allow interest to be created', function(done){
45-
46-
User.create({ name: 'User 1' }, function(err, user){
44+
it('should allow interest to be created', function (done) {
45+
46+
User.create({ name: 'User 1' }, function (err, user) {
4747
expect(err).to.equal(null);
48-
49-
Topic.create({ name: 'Topic 1'}, function(err, topic){
48+
49+
Topic.create({ name: 'Topic 1'}, function (err, topic) {
5050
expect(err).to.equal(null);
51-
51+
5252
request(app)
5353
.post('/interests')
5454
.send({
5555
data: {
5656
type: 'interests',
57+
attributes: {},
5758
relationships: {
58-
user: { id: user.id, type: 'users' },
59-
topic: { id: topic.id, type: 'topics' }
59+
user: { data: { id: user.id, type: 'users' } },
60+
topic: { data: { id: topic.id, type: 'topics' } }
6061
}
6162
}
6263
})
63-
.end(function(err, res){
64+
.end(function (err, res) {
6465
expect(err).to.equal(null);
6566
console.error(res.body.errors);
6667
expect(res).to.not.have.deep.property('body.errors');
67-
expect(res).to.have.deep.property('body.data.id').and.equal("1");
68+
expect(res).to.have.deep.property('body.data.id').and.equal('1');
6869
done(err);
6970
});
7071
});
7172
});
72-
73+
7374
});
74-
75-
});
75+
76+
});

0 commit comments

Comments
 (0)