Skip to content

Commit

Permalink
fix: double quote in filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Soontao committed Mar 23, 2023
1 parent c4c6bab commit 6f5ec49
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function boolCommonExpr(value: SourceArray, index: number): Lexer.Token {
}

let commonMoreExpr = undefined;
if (token.type === Lexer.TokenType.CommonExpression) {
if (token. type === Lexer.TokenType.CommonExpression) {
commonMoreExpr =
eqExpr(value, token.next) ||
neExpr(value, token.next) ||
Expand Down
3 changes: 3 additions & 0 deletions src/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ export function pchar(value: SourceArray, index: number): number {
index
);
}

export function pcharNoSQUOTE(value: SourceArray, index: number): number {
if (
unreserved(value[index]) ||
Expand All @@ -438,11 +439,13 @@ export function pcharNoSQUOTE(value: SourceArray, index: number): number {
) {
return index + 1;
}
// TODO: review here, maybe additional token required.
return (
otherDelims(value, index) ||
EQ(value, index) ||
COLON(value, index) ||
AT(value, index) ||
quotationMark(value, index) ||
pctEncodedNoSQUOTE(value, index) ||
index
);
Expand Down
127 changes: 127 additions & 0 deletions test/__snapshots__/filter.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Filter Test Suite shuold support filter with double quote 1`] = `"$filter=key eq 'val\\"'"`;

exports[`Filter Test Suite shuold support filter with double quote 2`] = `
Token {
"next": 21,
"position": 0,
"raw": "$filter=key eq 'val\\"'",
"type": "QueryOptions",
"value": Object {
"options": Array [
Token {
"next": 21,
"position": 0,
"raw": "$filter=key eq 'val\\"'",
"type": "Filter",
"value": Token {
"next": 21,
"position": 8,
"raw": "key eq 'val\\"'",
"type": "EqualsExpression",
"value": Object {
"left": Token {
"next": 11,
"position": 8,
"raw": "key",
"type": "FirstMemberExpression",
"value": Token {
"next": 11,
"position": 8,
"raw": "key",
"type": "MemberExpression",
"value": Token {
"next": 11,
"position": 8,
"raw": "key",
"type": "PropertyPathExpression",
"value": Token {
"next": 11,
"position": 8,
"raw": "key",
"type": "ODataIdentifier",
"value": Object {
"name": "key",
},
},
},
},
},
"right": Token {
"next": 21,
"position": 15,
"raw": "'val\\"'",
"type": "Literal",
"value": "Edm.String",
},
},
},
},
],
},
}
`;

exports[`Filter Test Suite shuold support filter without double quote 1`] = `"$filter=key eq 'val'"`;

exports[`Filter Test Suite shuold support filter without double quote 2`] = `
Token {
"next": 20,
"position": 0,
"raw": "$filter=key eq 'val'",
"type": "QueryOptions",
"value": Object {
"options": Array [
Token {
"next": 20,
"position": 0,
"raw": "$filter=key eq 'val'",
"type": "Filter",
"value": Token {
"next": 20,
"position": 8,
"raw": "key eq 'val'",
"type": "EqualsExpression",
"value": Object {
"left": Token {
"next": 11,
"position": 8,
"raw": "key",
"type": "FirstMemberExpression",
"value": Token {
"next": 11,
"position": 8,
"raw": "key",
"type": "MemberExpression",
"value": Token {
"next": 11,
"position": 8,
"raw": "key",
"type": "PropertyPathExpression",
"value": Token {
"next": 11,
"position": 8,
"raw": "key",
"type": "ODataIdentifier",
"value": Object {
"name": "key",
},
},
},
},
},
"right": Token {
"next": 20,
"position": 15,
"raw": "'val'",
"type": "Literal",
"value": "Edm.String",
},
},
},
},
],
},
}
`;
20 changes: 18 additions & 2 deletions test/filter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { get } from '@newdash/newdash';
import { Edm } from '@odata/metadata';
import { defaultParser, ODataFilter } from '../src';
import { defaultParser, ODataFilter, ODataParam } from '../src';


describe('Filter Test Suite', () => {
Expand All @@ -17,8 +17,24 @@ describe('Filter Test Suite', () => {
const ast = defaultParser.query("$filter=A eq 702dac82-923d-4958-805b-ca41c593d74f and B eq 'strValue'");
expect(ast).not.toBeNull();
expect(get(ast, 'value.options[0].value.value.left.value.right.value')).toBe(Edm.Guid.className);
expect(get(ast,'value.options[0].value.value.right.value.right.value')).toBe(Edm.String.className);
expect(get(ast, 'value.options[0].value.value.right.value.right.value')).toBe(Edm.String.className);
});

it('shuold support filter without double quote', () => {
const filter = ODataFilter.New().field("key").eq('val')
const filterStr = ODataParam.New().filter(filter).toString()
expect(filterStr).toMatchSnapshot()
const ast = defaultParser.query(filterStr)
expect(ast).toMatchSnapshot()
})

it('shuold support filter with double quote', () => {
const filter = ODataFilter.New().field("key").eq('val"')
const filterStr = ODataParam.New().filter(filter).toString()
expect(filterStr).toMatchSnapshot()
const ast = defaultParser.query(filterStr)
expect(ast).toMatchSnapshot()
})

it('should support complex filter', () => {

Expand Down

0 comments on commit 6f5ec49

Please sign in to comment.