Skip to content

Commit f07a99f

Browse files
committed
Merge pull request #60 from digitalsadhu/bug/fix-self-links
#49 fixing multi-resource requests from not having correct self links
2 parents 35cfcb7 + a5b423b commit f07a99f

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

lib/serializer.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,17 @@ function makeRelations (type, ids, options) {
207207
* @return {Object}
208208
*/
209209
function makeLinks (links, item) {
210+
var retLinks = {};
211+
210212
_.each(links, function (value, name) {
211213
if (_.isFunction(value)) {
212-
links[name] = value(item);
214+
retLinks[name] = value(item);
213215
} else {
214-
links[name] = _(value).toString();
216+
retLinks[name] = _(value).toString();
215217
}
216218
});
217219

218-
return links;
220+
return retLinks;
219221
}
220222

221223
/**

test/find.test.js

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('loopback json api component find methods', function () {
2020
JSONAPIComponent(app);
2121
});
2222

23-
describe('headers', function () {
23+
describe('Headers', function () {
2424
it('GET /models should have the JSON API Content-Type header set on collection responses', function (done) {
2525
request(app).get('/posts')
2626
.expect(200)
@@ -36,7 +36,7 @@ describe('loopback json api component find methods', function () {
3636
});
3737
});
3838

39-
describe('relationship objects', function () {
39+
describe('Relationship objects', function () {
4040
beforeEach(function (done) {
4141
Post.create({
4242
title: 'my post',
@@ -77,42 +77,65 @@ describe('loopback json api component find methods', function () {
7777
Post.create({
7878
title: 'my post 2',
7979
content: 'my post content 2'
80-
}, done);
80+
}, function () {
81+
Post.create({
82+
title: 'my post 3',
83+
content: 'my post content 3'
84+
}, done);
85+
});
8186
});
8287
});
8388

8489
//TODO: see https://github.com/digitalsadhu/loopback-component-jsonapi/issues/11
85-
it('should produce correct top level self links');
90+
it('GET /posts should produce top level self links', function (done) {
91+
request(app).get('/posts')
92+
.expect(200)
93+
.end(function (err, res) {
94+
expect(err).to.equal(null);
95+
expect(res.body).to.have.deep.property('links.self');
96+
expect(res.body.links.self).to.match(/http:\/\/127\.0\.0\.1.*\/posts$/);
97+
done();
98+
});
99+
});
86100

87-
it('should produce resource level self links', function (done) {
101+
it('GET /posts/1 should produce resource level self links', function (done) {
88102
request(app).get('/posts/1')
89103
.expect(200)
90104
.end(function (err, res) {
91105
expect(err).to.equal(null);
92106
expect(res.body).to.have.deep.property('data.links.self');
93-
expect(res.body.data.links.self).to.match(/http:\/\/127\.0\.0\.1.*\/posts\/1/);
107+
expect(res.body.data.links.self).to.match(/http:\/\/127\.0\.0\.1.*\/posts\/1$/);
94108
done();
95109
});
96110
});
97111

98-
it('should produce correct resource level self links for individual resources', function (done) {
112+
it('GET /posts/2 should produce correct resource level self links for individual resources', function (done) {
99113
request(app).get('/posts/2')
100114
.expect(200)
101115
.end(function (err, res) {
102116
expect(err).to.equal(null);
103117
expect(res.body).to.have.deep.property('data.links.self');
104-
expect(res.body.data.links.self).to.match(/http:\/\/127\.0\.0\.1.*\/posts\/2/);
118+
expect(res.body.data.links.self).to.match(/http:\/\/127\.0\.0\.1.*\/posts\/2$/);
105119
done();
106120
});
107121
});
108122

109-
it('should produce correct resource level self links for collections', function (done) {
123+
it('GET /posts should produce correct resource level self links for collections', function (done) {
110124
request(app).get('/posts')
111125
.expect(200)
112126
.end(function (err, res) {
127+
var index = 1;
113128
expect(err).to.equal(null);
114-
expect(res.body.data[1]).to.have.deep.property('links.self');
115-
expect(res.body.data[1].links.self).to.match(/http:\/\/127\.0\.0\.1.*\/posts\/2/);
129+
expect(res.body.data).to.be.a('array');
130+
expect(res.body.links.self).to.match(/http:\/\/127\.0\.0\.1.*\/posts$/);
131+
132+
res.body.data.forEach(function (resource) {
133+
expect(resource.links.self).to.match(new RegExp('^http://127.0.0.1.*/posts/' + index));
134+
index++;
135+
});
136+
137+
// Make sure we have tested all 3
138+
expect(index - 1).to.equal(3);
116139
done();
117140
});
118141
});

0 commit comments

Comments
 (0)