Skip to content

Commit ae14283

Browse files
Filippo Contitypicode
Filippo Conti
authored andcommittedSep 2, 2019
Fix for multiple _ne operators (#1013)
When filtering with multiple _ne it should use logical AND instead of OR to verify that all checks succeed, close #929
1 parent 4f315b0 commit ae14283

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed
 

‎__tests__/server/plural.js

+13
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,13 @@ describe('Server', () => {
348348
.expect('Content-Type', /json/)
349349
.expect(db.comments.slice(1))
350350
.expect(200))
351+
352+
test('should accept multiple parameters', () =>
353+
request(server)
354+
.get('/comments?id_ne=1&id_ne=2')
355+
.expect('Content-Type', /json/)
356+
.expect(db.comments.slice(2))
357+
.expect(200))
351358
})
352359

353360
describe('GET /:resource?attr_like=', () => {
@@ -357,6 +364,12 @@ describe('Server', () => {
357364
.expect('Content-Type', /json/)
358365
.expect([db.tags[1], db.tags[2]])
359366
.expect(200))
367+
test('should accept multiple parameters', () =>
368+
request(server)
369+
.get('/tags?body_like=photo&body_like=tech')
370+
.expect('Content-Type', /json/)
371+
.expect(db.tags)
372+
.expect(200))
360373
})
361374

362375
describe('GET /:parent/:parentId/:resource', () => {

‎src/server/router/plural.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,14 @@ module.exports = (db, name, opts) => {
116116
// Always use an array, in case req.query is an array
117117
const arr = [].concat(req.query[key])
118118

119+
const isDifferent = /_ne$/.test(key)
120+
const isRange = /_lte$/.test(key) || /_gte$/.test(key)
121+
const isLike = /_like$/.test(key)
122+
const path = key.replace(/(_lte|_gte|_ne|_like)$/, '')
123+
119124
chain = chain.filter(element => {
120125
return arr
121126
.map(function(value) {
122-
const isDifferent = /_ne$/.test(key)
123-
const isRange = /_lte$/.test(key) || /_gte$/.test(key)
124-
const isLike = /_like$/.test(key)
125-
const path = key.replace(/(_lte|_gte|_ne|_like)$/, '')
126127
// get item value based on path
127128
// i.e post.title -> 'foo'
128129
const elementValue = _.get(element, path)
@@ -146,7 +147,7 @@ module.exports = (db, name, opts) => {
146147
return value === elementValue.toString()
147148
}
148149
})
149-
.reduce((a, b) => a || b)
150+
.reduce((a, b) => (isDifferent ? a && b : a || b))
150151
})
151152
}
152153
})

0 commit comments

Comments
 (0)
Please sign in to comment.