Skip to content

Commit 97f7b76

Browse files
committed
Fix invalid include handling, uncomment tests
1 parent 2cf9005 commit 97f7b76

File tree

6 files changed

+28
-20
lines changed

6 files changed

+28
-20
lines changed

lib/errors.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,16 @@ function JSONAPIErrorHandler (err, req, res, next) {
6060
err.code = 'presence';
6161
err.name = 'ValidationError';
6262
}
63-
errors.push(buildErrorResponse(statusCode, err.message, err.code, err.name));
6463

64+
debug('Handling invalid relationship specified in url');
65+
if (/Relation (.*) is not defined for (.*) model/.test(err.message)) {
66+
statusCode = 400;
67+
err.message = 'Bad Request';
68+
err.code = 'INVALID_INCLUDE_TARGET';
69+
err.name = 'BadRequest';
70+
}
71+
72+
errors.push(buildErrorResponse(statusCode, err.message, err.code, err.name));
6573
} else {
6674
debug('Unable to determin error type. Treating error as a general 500 server error.');
6775
//catch all server 500 error if we were unable to understand the error.

lib/serialize.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ module.exports = function (app, defaults) {
8181
if (ctx.req.isSideloadingRelationships) {
8282
requestedIncludes = ctx.req.remotingContext.args.filter.include;
8383
}
84-
8584
options = {
8685
primaryKeyField: primaryKeyField,
8786
requestedIncludes: requestedIncludes,

lib/utilities/relationship-utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function getInvalidIncludesError (message) {
2020
var error = new Error(message || 'JSON API resource does not support `include`');
2121
error.statusCode = 400;
2222
error.code = 400;
23+
error.status = 400;
2324

2425
return error;
2526
}

test/belongsTo.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ describe('loopback json api belongsTo relationships', function () {
236236
});
237237
});
238238

239-
it.skip('should return a 400 Bad Request error if a non existent relationship is specified.', function (done) {
240-
request(app).get('/comments?filter={"include":"doesnotexist"}')
239+
it('should return a 400 Bad Request error if a non existent relationship is specified.', function (done) {
240+
request(app).get('/comments?filter[include]=doesnotexist')
241241
.expect(400)
242242
.end(done);
243243
});
@@ -252,7 +252,7 @@ describe('loopback json api belongsTo relationships', function () {
252252
});
253253
});
254254

255-
it.skip('should return a 400 Bad Request error if a non existent relationship is specified using JSON API syntax.', function (done) {
255+
it('should return a 400 Bad Request error if a non existent relationship is specified using JSON API syntax.', function (done) {
256256
request(app).get('/comments?include=doesnotexist')
257257
.expect(400)
258258
.end(done);
@@ -311,8 +311,8 @@ describe('loopback json api belongsTo relationships', function () {
311311
});
312312
});
313313

314-
it.skip('should return a 400 Bad Request error if a non existent relationship is specified.', function (done) {
315-
request(app).get('/comments/1?filter={"include":"doesnotexist"}')
314+
it('should return a 400 Bad Request error if a non existent relationship is specified.', function (done) {
315+
request(app).get('/comments/1?filter[include]=doesnotexist')
316316
.expect(400)
317317
.end(done);
318318
});
@@ -327,7 +327,7 @@ describe('loopback json api belongsTo relationships', function () {
327327
});
328328
});
329329

330-
it.skip('should return a 400 Bad Request error if a non existent relationship is specified using JSON API syntax.', function (done) {
330+
it('should return a 400 Bad Request error if a non existent relationship is specified using JSON API syntax.', function (done) {
331331
request(app).get('/comments/1?include=doesnotexist')
332332
.expect(400)
333333
.end(done);

test/hasMany.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ describe('loopback json api hasMany relationships', function () {
195195
});
196196
});
197197

198-
it.skip('should return a 400 Bad Request error if a non existent relationship is specified.', function (done) {
199-
request(app).get('/posts?filter={"include":"doesnotexist"}')
198+
it('should return a 400 Bad Request error if a non existent relationship is specified.', function (done) {
199+
request(app).get('/posts?filter[include]=doesnotexist')
200200
.expect(400)
201201
.end(done);
202202
});
@@ -228,7 +228,7 @@ describe('loopback json api hasMany relationships', function () {
228228
});
229229
});
230230

231-
it.skip('should return a 400 Bad Request error if a non existent relationship is specified using JSON API syntax.', function (done) {
231+
it('should return a 400 Bad Request error if a non existent relationship is specified using JSON API syntax.', function (done) {
232232
request(app).get('/posts?include=doesnotexist')
233233
.expect(400)
234234
.end(done);
@@ -281,8 +281,8 @@ describe('loopback json api hasMany relationships', function () {
281281
});
282282
});
283283

284-
it.skip('should return a 400 Bad Request error if a non existent relationship is specified.', function (done) {
285-
request(app).get('/posts/1?filter={"include":"doesnotexist"}')
284+
it('should return a 400 Bad Request error if a non existent relationship is specified.', function (done) {
285+
request(app).get('/posts/1?filter[include]=doesnotexist')
286286
.expect(400)
287287
.end(done);
288288
});
@@ -297,7 +297,7 @@ describe('loopback json api hasMany relationships', function () {
297297
});
298298
});
299299

300-
it.skip('should return a 400 Bad Request error if a non existent relationship is specified using JSON API syntax.', function (done) {
300+
it('should return a 400 Bad Request error if a non existent relationship is specified using JSON API syntax.', function (done) {
301301
request(app).get('/posts/1?include=doesnotexist')
302302
.expect(400)
303303
.end(done);

test/hasOne.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ describe('loopback json api hasOne relationships', function () {
187187
});
188188
});
189189

190-
it.skip('should return a 400 Bad Request error if a non existent relationship is specified.', function (done) {
191-
request(app).get('/posts?filter={"include":"doesnotexist"}')
190+
it('should return a 400 Bad Request error if a non existent relationship is specified.', function (done) {
191+
request(app).get('/posts?filter[include]=doesnotexist')
192192
.expect(400)
193193
.end(done);
194194
});
@@ -211,7 +211,7 @@ describe('loopback json api hasOne relationships', function () {
211211
});
212212
});
213213

214-
it.skip('should return a 400 Bad Request error if a non existent relationship is specified using JSON API syntax.', function (done) {
214+
it('should return a 400 Bad Request error if a non existent relationship is specified using JSON API syntax.', function (done) {
215215
request(app).get('/posts?include=doesnotexist')
216216
.expect(400)
217217
.end(done);
@@ -261,8 +261,8 @@ describe('loopback json api hasOne relationships', function () {
261261
});
262262
});
263263

264-
it.skip('should return a 400 Bad Request error if a non existent relationship is specified.', function (done) {
265-
request(app).get('/posts/1?filter={"include":"doesnotexist"}')
264+
it('should return a 400 Bad Request error if a non existent relationship is specified.', function (done) {
265+
request(app).get('/posts/1?filter[include]=doesnotexist')
266266
.expect(400)
267267
.end(done);
268268
});
@@ -277,7 +277,7 @@ describe('loopback json api hasOne relationships', function () {
277277
});
278278
});
279279

280-
it.skip('should return a 400 Bad Request error if a non existent relationship is specified using JSON API syntax.', function (done) {
280+
it('should return a 400 Bad Request error if a non existent relationship is specified using JSON API syntax.', function (done) {
281281
request(app).get('/posts/1?include=doesnotexist')
282282
.expect(400)
283283
.end(done);

0 commit comments

Comments
 (0)