Skip to content

Commit

Permalink
fix(adapter-commons): Keep Symbols when filtering a query (#1141)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl authored Dec 21, 2018
1 parent c198e43 commit c9f55d8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
10 changes: 8 additions & 2 deletions packages/adapter-commons/lib/filter-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,25 @@ function convertSort (sort) {
function cleanQuery (query, operators, filters) {
if (_.isObject(query) && query.constructor === {}.constructor) {
const result = {};

_.each(query, (value, key) => {
if (key[0] === '$') {
if(filters[key] !== undefined) {
if (filters[key] !== undefined) {
return;
}

if(!operators.includes(key)) {
if (!operators.includes(key)) {
throw new BadRequest(`Invalid query parameter ${key}`, query);
}
}

result[key] = cleanQuery(value, operators, filters);
});

Object.getOwnPropertySymbols(query).forEach(symbol => {
result[symbol] = query[symbol];
});

return result;
}

Expand Down
19 changes: 18 additions & 1 deletion packages/adapter-commons/test/filter-query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,23 @@ describe('@feathersjs/adapter-commons/filterQuery', () => {
assert.strictEqual(filters.$select, undefined);
});

it('includes Symbols', () => {
const TEST = Symbol('testing');
const original = {
[TEST]: 'message',
other: true,
sub: { [TEST]: 'othermessage' }
};

const { query } = filterQuery(original);

assert.deepStrictEqual(query, {
[TEST]: 'message',
other: true,
sub: { [TEST]: 'othermessage' }
});
});

it('only converts plain objects', () => {
const userId = ObjectId();
const original = {
Expand All @@ -176,7 +193,7 @@ describe('@feathersjs/adapter-commons/filterQuery', () => {
try {
filterQuery({ $select: 1, $known: 1 });
assert.ok(false, 'Should never get here');
} catch(error) {
} catch (error) {
assert.strictEqual(error.message, 'Invalid query parameter $known');
}
});
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-commons/test/service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe('@feathersjs/adapter-commons/service', () => {
const withWhitelisted = service.filterQuery({
query: { $limit: 10, $something: 'else' }
});

assert.deepStrictEqual(withWhitelisted, {
paginate: {},
filters: { $limit: 10 },
Expand Down

0 comments on commit c9f55d8

Please sign in to comment.