Skip to content

Commit

Permalink
fix(odata): filter with single quote should be escaped, fixes #328
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding-SE committed Oct 29, 2019
1 parent 8f7233c commit 1cd0b47
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,22 @@ describe('GridOdataService', () => {
expect(query).toBe(expectation);
});

it('should escape single quote by doubling the quote when filter includes a single quote', () => {
const expectation = `$top=10&$filter=(Gender eq 'female' and not substringof('abc''s', Company))`;
const mockColumnGender = { id: 'gender', field: 'gender' } as Column;
const mockColumnCompany = { id: 'company', field: 'company' } as Column;
const mockColumnFilters = {
gender: { columnId: 'gender', columnDef: mockColumnGender, searchTerms: ['female'], operator: 'EQ' },
company: { columnId: 'company', columnDef: mockColumnCompany, searchTerms: [`abc's`], operator: OperatorType.notContains },
} as ColumnFilters;

service.init(serviceOptions, paginationOptions, gridStub);
service.updateFilters(mockColumnFilters, false);
const query = service.buildQuery();

expect(query).toBe(expectation);
});

it('should return a query with multiple filters and expect same query string result as previous test even with "isUpdatedByPreset" enabled', () => {
const expectation = `$top=10&$filter=(Gender eq 'female' and substringof('abc', Company))`;
const mockColumnGender = { id: 'gender', field: 'gender' } as Column;
Expand Down Expand Up @@ -1349,7 +1365,7 @@ describe('GridOdataService', () => {
expect(currentFilters).toEqual(presetFilters);
});

it('should return a query with a filter with range of numbers with decimals when the preset is a filter range with 3 dots (..) separator', () => {
it('should return a query with a filter with range of numbers with decimals when the preset is a filter range with 3 dots (...) separator', () => {
serviceOptions.columnDefinitions = [{ id: 'company', field: 'company' }, { id: 'gender', field: 'gender' }, { id: 'duration', field: 'duration', type: FieldType.number }];
const expectation = `$top=10&$filter=(Duration gt 0.5 and Duration lt .88)`;
const presetFilters = [
Expand Down
10 changes: 6 additions & 4 deletions src/app/modules/angular-slickgrid/services/grid-odata.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ export class GridOdataService implements BackendService {
if (operator === 'IN') {
// example:: (Stage eq "Expired" or Stage eq "Renewal")
for (let j = 0, lnj = searchTerms.length; j < lnj; j++) {
tmpSearchTerms.push(`${fieldName} eq '${searchTerms[j]}'`);
const searchVal = searchTerms[j].replace(`'`, `''`);
tmpSearchTerms.push(`${fieldName} eq '${searchVal}'`);
}
searchBy = tmpSearchTerms.join(' or ');
if (!(typeof searchBy === 'string' && searchBy[0] === '(' && searchBy.slice(-1) === ')')) {
Expand All @@ -336,7 +337,8 @@ export class GridOdataService implements BackendService {
} else {
// example:: (Stage ne "Expired" and Stage ne "Renewal")
for (let k = 0, lnk = searchTerms.length; k < lnk; k++) {
tmpSearchTerms.push(`${fieldName} ne '${searchTerms[k]}'`);
const searchVal = searchTerms[k].replace(`'`, `''`);
tmpSearchTerms.push(`${fieldName} ne '${searchVal}'`);
}
searchBy = tmpSearchTerms.join(' and ');
if (!(typeof searchBy === 'string' && searchBy[0] === '(' && searchBy.slice(-1) === ')')) {
Expand All @@ -349,7 +351,7 @@ export class GridOdataService implements BackendService {
} else if (fieldType === FieldType.string) {
// string field needs to be in single quotes
if (operator === '' || operator === OperatorType.contains || operator === OperatorType.notContains) {
searchBy = this.odataQueryVersionWrapper('substring', odataVersion, fieldName, searchTerms);
searchBy = this.odataQueryVersionWrapper('substring', odataVersion, fieldName, searchValue);
if (operator === OperatorType.notContains) {
searchBy = `not ${searchBy}`;
}
Expand All @@ -373,7 +375,7 @@ export class GridOdataService implements BackendService {
// push to our temp array and also trim white spaces
if (searchBy !== '') {
searchByArray.push(searchBy.trim());
this.saveColumnFilter(fieldName || '', fieldSearchValue, searchTerms);
this.saveColumnFilter(fieldName || '', fieldSearchValue, searchValue);
}
}
}
Expand Down

0 comments on commit 1cd0b47

Please sign in to comment.