diff --git a/src/expressions.ts b/src/expressions.ts index 5b40397..e36dad3 100644 --- a/src/expressions.ts +++ b/src/expressions.ts @@ -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) || diff --git a/src/lexer.ts b/src/lexer.ts index 52924ba..3cb32bb 100644 --- a/src/lexer.ts +++ b/src/lexer.ts @@ -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]) || @@ -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 ); diff --git a/test/__snapshots__/filter.spec.ts.snap b/test/__snapshots__/filter.spec.ts.snap new file mode 100644 index 0000000..be83e9e --- /dev/null +++ b/test/__snapshots__/filter.spec.ts.snap @@ -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", + }, + }, + }, + }, + ], + }, +} +`; diff --git a/test/filter.spec.ts b/test/filter.spec.ts index bf8e3e0..80c46ae 100644 --- a/test/filter.spec.ts +++ b/test/filter.spec.ts @@ -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', () => { @@ -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', () => {