Skip to content

Commit 8c2c7bd

Browse files
committed
Merge pull request #98 from holidayextras/fixNullRelatedResource
Handle missing to-one related resource.
2 parents f16cc00 + 8539a3f commit 8c2c7bd

File tree

8 files changed

+116
-85
lines changed

8 files changed

+116
-85
lines changed

CHANGELOG.md

Lines changed: 83 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,83 @@
1-
2015-06-29 - Initial release
2-
2015-07-03 - Refactoring handlers to improve readability
3-
2915-07-03 - Separating out route handlers into separate files
4-
2015-07-05 - Allow user to implement their own error logging
5-
2015-07-06 - Handle 404s and allow logging for uncaught exceptions
6-
2015-07-08 - Added additional info to relation metadata
7-
2015-07-11 - Code Complexity tool via npm-run-complexity
8-
2015-07-12 - v0.7.0
9-
2015-07-12 - Split postProcessing into smaller modules
10-
2015-07-12 - Split out Joi modifications into separate file
11-
2015-07-13 - Split out documentation into more manageable chunks
12-
2015-07-13 - Updating dependencies to latest stable releases
13-
2015-07-13 - v0.8.0
14-
2015-09-25 - Enabling meta blocks on Resources and Relations
15-
2015-09-25 - Adding cookies to Request objects
16-
2015-09-25 - Updating dependencies
17-
2015-09-25 - Enable basic authentication
18-
2015-09-25 - Validate attribute names
19-
2015-09-25 - Stricter handling of HTTP Accept headers
20-
2015-09-25 - Stricter handling of HTTP Content-Type headers
21-
2015-09-27 - Improve validation when updating a relationship
22-
2015-09-27 - v0.9.0
23-
2015-09-27 - Always respond with absolute URLs
24-
2015-11-05 - v0.10.0
25-
2015-11-06 - Improve resource-update validation
26-
2015-11-06 - v0.11.0
27-
2015-11-09 - Ignore non-js routes within the example app
28-
2015-11-09 - Pass around headers on inclusion requests
29-
2015-11-09 - Creating resources gives a 201 CREATED
30-
2015-11-10 - Additional error handling for foreign relation lookup
31-
2015-11-10 - Additional error detail on 403 and 404
32-
2015-11-10 - v0.12.0
33-
2015-11-11 - Correctly build related URLs when using HTTPS
34-
2015-11-11 - v0.12.1
35-
2015-11-11 - Blacklist some HTTP headers we dont want to pass onwards
36-
2015-11-11 - v0.12.2
37-
2015-11-12 - Handlers readiness check
38-
2015-11-12 - v0.13.0
39-
2015-11-17 - Additional error handling for foreign relation lookup
40-
2015-11-17 - v0.13.1
41-
2015-11-17 - Handlers are now objects instead of modules
42-
2015-11-17 - v0.14.0
43-
2015-11-18 - Rename MockHandler to MemoryHandler
44-
2015-11-18 - v0.15.0
45-
2015-11-18 - Updated documentation
46-
2015-11-18 - v0.15.1
47-
2015-11-19 - Validate filter params before handing over to resourceHandler.search
48-
2015-11-19 - v0.15.2
49-
2015-11-25 - Automatic Swagger Generation
50-
2015-11-25 - Debugging functionality
51-
2015-11-25 - Validation around handlers behaviour
52-
2015-11-25 - v0.16.0
53-
2015-12-03 - Top level jsonapi blocks
54-
2015-12-05 - Tooling improvements
55-
2015-12-07 - Efficient inclusions
56-
2015-12-08 - Pagination support
57-
2015-12-10 - CPU profiling
58-
2015-12-10 - Move to lodash
59-
2015-12-10 - Use filter[] instead of relationships[]
60-
2015-12-10 - Inclusion bug fixes
61-
2015-12-10 - v1.0.0
62-
2015-12-10 - Correcting v8-profiler dependency
63-
2015-12-10 - v1.0.1
64-
2015-12-11 - Test harness improvements
65-
2015-12-11 - v1.0.2
66-
2015-12-17 - Don't pass on content-length HTTP headers on inclusion requests
67-
2015-12-17 - Filter won't match undefined properties anymore
68-
2015-12-17 - v1.0.3
69-
2015-12-30 - Enable path-less API
70-
2015-12-30 - v1.0.4
71-
2015-12-31 - Set Location header correctly when creating resources
72-
2015-12-31 - v1.0.5
73-
2015-01-11 - Big documentation improvements
74-
2015-01-11 - Sanitise input + output with Joi
75-
2015-01-11 - Updating dependencies
76-
2015-01-11 - Error hard and fast when incorrectly defining foreign relations
77-
2015-01-11 - v1.1.0
78-
2015-01-21 - Set better default CORS headers
79-
2015-01-21 - v1.2.0
80-
2015-01-25 - Aggressive cache-expiry headers by default
81-
2015-01-25 - v1.3.0
1+
- 2016-02-10 - v1.3.1
2+
- 2016-02-10 - Correctly handle missing to-one related resources
3+
- 2016-01-25 - v1.3.0
4+
- 2016-01-25 - Aggressive cache-expiry headers by default
5+
- 2016-01-21 - v1.2.0
6+
- 2016-01-21 - Set better default CORS headers
7+
- 2016-01-11 - v1.1.0
8+
- 2016-01-11 - Error hard and fast when incorrectly defining foreign relations
9+
- 2016-01-11 - Updating dependencies
10+
- 2016-01-11 - Sanitise input + output with Joi
11+
- 2016-01-11 - Big documentation improvements
12+
- 2015-12-31 - v1.0.5
13+
- 2015-12-31 - Set Location header correctly when creating resources
14+
- 2015-12-30 - v1.0.4
15+
- 2015-12-30 - Enable path-less API
16+
- 2015-12-17 - v1.0.3
17+
- 2015-12-17 - Filter won't match undefined properties anymore
18+
- 2015-12-17 - Don't pass on content-length HTTP headers on inclusion requests
19+
- 2015-12-11 - v1.0.2
20+
- 2015-12-11 - Test harness improvements
21+
- 2015-12-10 - v1.0.1
22+
- 2015-12-10 - Correcting v8-profiler dependency
23+
- 2015-12-10 - v1.0.0
24+
- 2015-12-10 - Inclusion bug fixes
25+
- 2015-12-10 - Use filter[] instead of relationships[]
26+
- 2015-12-10 - Move to lodash
27+
- 2015-12-10 - CPU profiling
28+
- 2015-12-08 - Pagination support
29+
- 2015-12-07 - Efficient inclusions
30+
- 2015-12-05 - Tooling improvements
31+
- 2015-12-03 - Top level jsonapi blocks
32+
- 2015-11-25 - v0.16.0
33+
- 2015-11-25 - Validation around handlers behaviour
34+
- 2015-11-25 - Debugging functionality
35+
- 2015-11-25 - Automatic Swagger Generation
36+
- 2015-11-19 - v0.15.2
37+
- 2015-11-19 - Validate filter params before handing over to resourceHandler.search
38+
- 2015-11-18 - v0.15.1
39+
- 2015-11-18 - Updated documentation
40+
- 2015-11-18 - v0.15.0
41+
- 2015-11-18 - Rename MockHandler to MemoryHandler
42+
- 2015-11-17 - v0.14.0
43+
- 2015-11-17 - Handlers are now objects instead of modules
44+
- 2015-11-17 - v0.13.1
45+
- 2015-11-17 - Additional error handling for foreign relation lookup
46+
- 2015-11-12 - v0.13.0
47+
- 2015-11-12 - Handlers readiness check
48+
- 2015-11-11 - v0.12.2
49+
- 2015-11-11 - Blacklist some HTTP headers we dont want to pass onwards
50+
- 2015-11-11 - v0.12.1
51+
- 2015-11-11 - Correctly build related URLs when using HTTPS
52+
- 2015-11-10 - v0.12.0
53+
- 2015-11-10 - Additional error detail on 403 and 404
54+
- 2015-11-10 - Additional error handling for foreign relation lookup
55+
- 2015-11-09 - Creating resources gives a 201 CREATED
56+
- 2015-11-09 - Pass around headers on inclusion requests
57+
- 2015-11-09 - Ignore non-js routes within the example app
58+
- 2015-11-06 - v0.11.0
59+
- 2015-11-06 - Improve resource-update validation
60+
- 2015-11-05 - v0.10.0
61+
- 2015-09-27 - Always respond with absolute URLs
62+
- 2015-09-27 - v0.9.0
63+
- 2015-09-27 - Improve validation when updating a relationship
64+
- 2015-09-25 - Stricter handling of HTTP Content-Type headers
65+
- 2015-09-25 - Stricter handling of HTTP Accept headers
66+
- 2015-09-25 - Validate attribute names
67+
- 2015-09-25 - Enable basic authentication
68+
- 2015-09-25 - Updating dependencies
69+
- 2015-09-25 - Adding cookies to Request objects
70+
- 2015-09-25 - Enabling meta blocks on Resources and Relations
71+
- 2015-07-13 - v0.8.0
72+
- 2015-07-13 - Updating dependencies to latest stable releases
73+
- 2015-07-13 - Split out documentation into more manageable chunks
74+
- 2015-07-12 - Split out Joi modifications into separate file
75+
- 2015-07-12 - Split postProcessing into smaller modules
76+
- 2015-07-12 - v0.7.0
77+
- 2015-07-11 - Code Complexity tool via npm-run-complexity
78+
- 2015-07-08 - Added additional info to relation metadata
79+
- 2015-07-06 - Handle 404s and allow logging for uncaught exceptions
80+
- 2015-07-05 - Allow user to implement their own error logging
81+
- 2015-07-03 - Separating out route handlers into separate files
82+
- 2015-07-03 - Refactoring handlers to improve readability
83+
- 2015-06-29 - Initial release

example/resources/comments.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ jsonApi.define({
3535
body: "I like XML better",
3636
timestamp: "2017-06-20",
3737
author: { type: "people", id: "32fb0105-acaa-4adb-9ec4-8b49633695e1", meta: { created: "2010-01-01" } }
38+
},
39+
{
40+
id: "2f716574-cef6-4238-8285-520911af86c1",
41+
type: "comments",
42+
body: "Wibble wibble.",
43+
timestamp: "2017-12-31",
44+
author: null
3845
}
3946
]
4047
});

lib/postProcess.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ postProcess._fetchRelatedResources = function(request, mainResource, callback) {
3535

3636
// Fetch the other objects
3737
var dataItems = mainResource[request.params.relation];
38+
39+
if (!dataItems) return callback(null, [ null ]);
40+
3841
if (!(dataItems instanceof Array)) dataItems = [ dataItems ];
3942

4043
var resourcesToFetch = dataItems.reduce(function(map, dataItem) {

lib/routes/related.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ relatedRoute.register = function() {
5757
}
5858
request.resourceConfig = jsonApi._resources[relation._settings.__one || relation._settings.__many];
5959
response = responseHelper._generateResponse(request, resourceConfig, relatedResources);
60-
response.included = [ ];
60+
if (relatedResources !== null) {
61+
response.included = [ ];
62+
}
6163
postProcess.handle(request, response, callback);
6264
}
6365
], function(err) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsonapi-server",
3-
"version": "1.3.0",
3+
"version": "1.3.1",
44
"description": "A config driven NodeJS framework implementing json:api",
55
"keywords": [
66
"jsonapi",

test/_preResourceValidationCheck.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var jsonApiTestServer = require("../example/server.js");
77
describe("Testing jsonapi-server", function() {
88

99
[ { name: "articles", count: 4 },
10-
{ name: "comments", count: 2 },
10+
{ name: "comments", count: 3 },
1111
{ name: "people", count: 4 },
1212
{ name: "photos", count: 3 },
1313
{ name: "tags", count: 5 }

test/get-resource-id-related.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ describe("Testing jsonapi-server", function() {
6666
});
6767
});
6868

69+
it("with null data", function(done) {
70+
var url = "http://localhost:16006/rest/comments/2f716574-cef6-4238-8285-520911af86c1/author";
71+
helpers.request({
72+
method: "GET",
73+
url: url
74+
}, function(err, res, json) {
75+
assert.equal(err, null);
76+
json = helpers.validateJson(json);
77+
assert.equal(res.statusCode, "200", "Expecting 200 OK");
78+
assert.strictEqual(json.data, null);
79+
assert(!("included" in json), "Null resource not NOT have `includes` attribute");
80+
81+
done();
82+
83+
});
84+
});
85+
6986
it("with fields", function(done) {
7087
var url = "http://localhost:16006/rest/articles/de305d54-75b4-431b-adb2-eb6b9e546014/author?fields[people]=email";
7188
helpers.request({

test/zzPostResourceValidationCheck.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var jsonApiTestServer = require("../example/server.js");
77
describe("Testing jsonapi-server", function() {
88

99
[ { name: "articles", count: 4 },
10-
{ name: "comments", count: 1 },
10+
{ name: "comments", count: 2 },
1111
{ name: "people", count: 4 },
1212
{ name: "photos", count: 4 },
1313
{ name: "tags", count: 5 }

0 commit comments

Comments
 (0)