Skip to content

Commit 882c039

Browse files
authored
Merge pull request #235 from rocknrolla777/fix-include-filter
parse include filter
2 parents ae2dafb + 56ee087 commit 882c039

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

lib/serialize.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ module.exports = function (app, defaults) {
102102

103103
// If we're sideloading, we need to add the includes
104104
if (ctx.req.isSideloadingRelationships) {
105-
requestedIncludes = ctx.req.remotingContext.args.filter.include
105+
requestedIncludes = utils.setRequestedIncludes(ctx.req.remotingContext.args.filter.include)
106106
}
107107

108108
if (model.definition.settings.scope) {
@@ -119,7 +119,7 @@ module.exports = function (app, defaults) {
119119
if (typeof include === 'string') {
120120
requestedIncludes.push(include)
121121
} else if (_.isArray(include)) {
122-
requestedIncludes = requestedIncludes.concat(include)
122+
requestedIncludes = requestedIncludes.concat(utils.setRequestedIncludes(include))
123123
}
124124
}
125125

lib/utils.js

+21
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module.exports = {
1919
shouldNotApplyJsonApi: shouldNotApplyJsonApi,
2020
shouldApplyJsonApi: shouldApplyJsonApi,
2121
relationFkOnModelFrom: relationFkOnModelFrom,
22+
setRequestedIncludes: setRequestedIncludes,
2223
setIncludedRelations: setIncludedRelations
2324
}
2425

@@ -238,3 +239,23 @@ function setIncludedRelations (relations, app) {
238239
}
239240
return relations
240241
}
242+
243+
function setRequestedIncludes (include) {
244+
if (!include) return undefined
245+
246+
if (typeof include === 'string') {
247+
return include
248+
}
249+
if (include instanceof Array) {
250+
return include.map(function (inc) {
251+
if (typeof inc === 'string') {
252+
return inc
253+
}
254+
255+
if (inc instanceof Object) {
256+
return inc.relation
257+
}
258+
})
259+
}
260+
return include
261+
}

test/scopeInclude.test.js

+22
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,28 @@ describe('include option', function () {
127127
done()
128128
})
129129
})
130+
it('should include comments', function (done) {
131+
request(app)
132+
.get('/posts/1?filter={"include":["comments"]}')
133+
.end(function (err, res) {
134+
expect(err).to.equal(null)
135+
expect(res.body.included.length).equal(2)
136+
expect(res.body.included[0].type).equal('comments')
137+
expect(res.body.included[1].type).equal('comments')
138+
done()
139+
})
140+
})
141+
it('should include categories with empty attributes object', function (done) {
142+
request(app)
143+
.get('/posts/1?filter={"include":[{"relation":"category", "scope": {"fields": ["id"]}}]}')
144+
.end(function (err, res) {
145+
expect(err).to.equal(null)
146+
expect(res.body.included.length).equal(3)
147+
expect(res.body.included[0].type).equal('categories')
148+
expect(res.body.included[0].attributes).to.include({})
149+
done()
150+
})
151+
})
130152
})
131153
})
132154
})

0 commit comments

Comments
 (0)