Skip to content

Commit

Permalink
odata-filter: remove no-op case-change of function name (#1237)
Browse files Browse the repository at this point in the history
odata-v4-parser treats function names as case-sensitive, so non-lowercase extractFunction names ever reach here
  • Loading branch information
alxndrsn authored Nov 3, 2024
1 parent 346d78a commit 59defd9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
5 changes: 2 additions & 3 deletions lib/data/odata-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ const odataFilter = (expr, odataToColumnMap) => {
const methodCall = (node) => {
// n.b. odata-v4-parser appears to already validate function name and arity.
const fn = node.value.method;
const lowerName = fn.toLowerCase();
const params = node.value.parameters;
if (extractFunctions.includes(lowerName)) {
return sql`extract(${sql.identifier([lowerName])} from ${op(params[0])})`; // eslint-disable-line no-use-before-define
if (extractFunctions.includes(fn)) {
return sql`extract(${sql.identifier([fn])} from ${op(params[0])})`; // eslint-disable-line no-use-before-define
} else if (fn === 'now') {
return sql`now()`;
} else {
Expand Down
17 changes: 16 additions & 1 deletion test/unit/data/odata-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,29 @@ describe('OData filter query transformer', () => {
});
});

it('should reject unrecognized function names', () => {
it('should reject unsupported OData functions', () => {
assert.throws(() => { odataFilter('123 eq trim(\' 123 \')'); }, (err) => {
err.should.be.a.Problem();
err.problemCode.should.equal(501.4);
err.message.should.equal('The given OData filter expression uses features not supported by this server: MethodCallExpression at 7 ("trim(\' 123 \')")');
return true;
});
});

[
'somethingwhichneverexisted()',
'NOW()', // wrong case
'YEAR(now())', // wrong case
].forEach(badCall => {
it(`should reject unrecognized function name ${badCall}`, () => {
assert.throws(() => { odataFilter(`123 eq ${badCall}`); }, (err) => {
err.should.be.a.Problem();
err.problemCode.should.equal(400.18);
err.message.should.match(/^The OData filter expression you provided could not be parsed: Unexpected character at \d+$/);
return true;
});
});
});
});

describe('OData orderby/sort query transformer', () => {
Expand Down

0 comments on commit 59defd9

Please sign in to comment.