Skip to content

Commit addc2a2

Browse files
fix: parser should only match full filter keys
1 parent ee5c8e1 commit addc2a2

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { FilterAttribute } from '../filter-attribute';
2+
import { FilterAttributeType } from '../filter-attribute-type';
3+
import { tryParseStringForAttribute } from './parsed-filter';
4+
5+
describe('Filter Parsing utilities', () => {
6+
const testStatusAttribute: FilterAttribute = {
7+
name: 'status',
8+
displayName: 'Status',
9+
type: FilterAttributeType.String
10+
};
11+
12+
const testStatusCodeAttribute: FilterAttribute = {
13+
name: 'statusCode',
14+
displayName: 'Status Code',
15+
type: FilterAttributeType.String
16+
};
17+
18+
test('should match if full name match', () => {
19+
expect(tryParseStringForAttribute(testStatusAttribute, 'status', ['name'])).toEqual({
20+
attribute: testStatusAttribute
21+
});
22+
23+
expect(tryParseStringForAttribute(testStatusAttribute, 'status ', ['name'])).toEqual({
24+
attribute: testStatusAttribute
25+
});
26+
27+
expect(tryParseStringForAttribute(testStatusCodeAttribute, 'statusCode', ['name'])).toEqual({
28+
attribute: testStatusCodeAttribute
29+
});
30+
});
31+
32+
test('should match if followed by beginning of operator', () => {
33+
expect(tryParseStringForAttribute(testStatusAttribute, 'status !', ['name'])).toEqual({
34+
attribute: testStatusAttribute
35+
});
36+
});
37+
38+
test('should not match if partial name match', () => {
39+
expect(tryParseStringForAttribute(testStatusCodeAttribute, 'status', ['name'])).toBe(undefined);
40+
});
41+
42+
test('should not match if text starts with attribute name and continues', () => {
43+
expect(tryParseStringForAttribute(testStatusAttribute, 'statusCode', ['name'])).toBe(undefined);
44+
});
45+
});

projects/components/src/filtering/filter/parser/parsed-filter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ export const tryParseStringForAttribute = (
6262
// Now, we know that it does match. Remove the attribute name from the beginning and try to determine the subpath next.
6363
const stringAfterAttributeName = text.slice(attributeToTest[matchingNameField].length).trim();
6464

65+
// If the string continues with more alphanumeric characters directly after this attribute name, it's no longer a match
66+
if (stringAfterAttributeName.match(/^\w/)) {
67+
return undefined;
68+
}
69+
6570
if (stringAfterAttributeName.startsWith(MAP_LHS_DELIMITER)) {
6671
if (attributeToTest.type !== FilterAttributeType.StringMap) {
6772
// Can't have a subpath if not a map

0 commit comments

Comments
 (0)