Skip to content

Commit

Permalink
fix(odata): encode URI also for IN/NIN operators, fixes #463 (#471)
Browse files Browse the repository at this point in the history
Co-authored-by: Ghislain Beaulac <[email protected]>
  • Loading branch information
ghiscoding and ghiscoding-SE authored May 26, 2020
1 parent d978946 commit 92bf9e3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,20 @@ describe('GridOdataService', () => {
expect(query).toBe(expectation);
});

it('should return a query with a CSV string when the filter operator is IN ', () => {
const expectation = `$top=10&$filter=(Gender eq 'female' or Gender eq 'ma%2Fle')`;
const mockColumn = { id: 'gender', field: 'gender' } as Column;
const mockColumnFilters = {
gender: { columnId: 'gender', columnDef: mockColumn, searchTerms: ['female', 'ma/le'], operator: 'IN' },
} 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 a CSV string when the filter operator is IN for numeric column type', () => {
const expectation = `$top=10&$filter=(Id eq 100 or Id eq 101)`;
const mockColumn = { id: 'id', field: 'id', type: FieldType.number } as Column;
Expand Down Expand Up @@ -748,6 +762,20 @@ describe('GridOdataService', () => {
expect(query).toBe(expectation);
});

it('should return a query with a CSV string when the filter operator is NOT_IN', () => {
const expectation = `$top=10&$filter=(Gender ne 'female' and Gender ne 'ma%2Fle')`;
const mockColumn = { id: 'gender', field: 'gender' } as Column;
const mockColumnFilters = {
gender: { columnId: 'gender', columnDef: mockColumn, searchTerms: ['female', 'ma/le'], operator: OperatorType.notIn },
} 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 a CSV string and use the operator from the Column Definition Operator when provided', () => {
const expectation = `$top=10&$filter=(Gender ne 'female' and Gender ne 'male')`;
const mockColumn = { id: 'gender', field: 'gender', filter: { operator: OperatorType.notIn } } as Column;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ export class GridOdataService implements BackendService {
// example:: (Stage eq "Expired" or Stage eq "Renewal")
for (let j = 0, lnj = searchTerms.length; j < lnj; j++) {
if (fieldType === FieldType.string) {
const searchVal = searchTerms[j].replace(`'`, `''`);
const searchVal = encodeURIComponent(searchTerms[j].replace(`'`, `''`));
tmpSearchTerms.push(`${fieldName} eq '${searchVal}'`);
} else {
// Single quote escape is not needed for non string type
Expand All @@ -342,7 +342,7 @@ export class GridOdataService implements BackendService {
} else {
// example:: (Stage ne "Expired" and Stage ne "Renewal")
for (let k = 0, lnk = searchTerms.length; k < lnk; k++) {
const searchVal = searchTerms[k].replace(`'`, `''`);
const searchVal = encodeURIComponent(searchTerms[k].replace(`'`, `''`));
tmpSearchTerms.push(`${fieldName} ne '${searchVal}'`);
}
searchBy = tmpSearchTerms.join(' and ');
Expand Down

0 comments on commit 92bf9e3

Please sign in to comment.