78991: URI-encode SearchOptions query values

This commit is contained in:
Yura Bondarenko
2021-05-04 10:34:05 +02:00
parent 340f9518cd
commit e682997195
3 changed files with 37 additions and 15 deletions

View File

@@ -8,7 +8,11 @@ describe('PaginatedSearchOptions', () => {
let options: PaginatedSearchOptions;
const sortOptions = new SortOptions('test.field', SortDirection.DESC);
const pageOptions = Object.assign(new PaginationComponentOptions(), { pageSize: 40, page: 1 });
const filters = [new SearchFilter('f.test', ['value']), new SearchFilter('f.example', ['another value', 'second value'])];
const filters = [
new SearchFilter('f.test', ['value']),
new SearchFilter('f.example', ['another value', 'second value']),
new SearchFilter('f.range', ['[2002 TO 2021]'], 'equals'),
];
const query = 'search query';
const scope = '0fde1ecb-82cc-425a-b600-ac3576d76b47';
const baseUrl = 'www.rest.com';
@@ -31,12 +35,13 @@ describe('PaginatedSearchOptions', () => {
'sort=test.field,DESC&' +
'page=0&' +
'size=40&' +
'query=search query&' +
'query=search%20query&' +
'scope=0fde1ecb-82cc-425a-b600-ac3576d76b47&' +
'dsoType=ITEM&' +
'f.test=value&' +
'f.example=another value&' +
'f.example=second value'
'f.example=another%20value&' +
'f.example=second%20value&' +
'f.range=%5B2002%20TO%202021%5D,equals'
);
});

View File

@@ -4,8 +4,13 @@ import { SearchFilter } from './search-filter.model';
import { DSpaceObjectType } from '../../core/shared/dspace-object-type.model';
describe('SearchOptions', () => {
let options: PaginatedSearchOptions;
const filters = [new SearchFilter('f.test', ['value']), new SearchFilter('f.example', ['another value', 'second value'])];
let options: SearchOptions;
const filters = [
new SearchFilter('f.test', ['value']),
new SearchFilter('f.example', ['another value', 'second value']),
new SearchFilter('f.range', ['[2002 TO 2021]'], 'equals'),
];
const query = 'search query';
const scope = '0fde1ecb-82cc-425a-b600-ac3576d76b47';
const baseUrl = 'www.rest.com';
@@ -18,12 +23,13 @@ describe('SearchOptions', () => {
it('should generate a string with all parameters that are present', () => {
const outcome = options.toRestUrl(baseUrl);
expect(outcome).toEqual('www.rest.com?' +
'query=search query&' +
'query=search%20query&' +
'scope=0fde1ecb-82cc-425a-b600-ac3576d76b47&' +
'dsoType=ITEM&' +
'f.test=value&' +
'f.example=another value&' +
'f.example=second value'
'f.example=another%20value&' +
'f.example=second%20value&' +
'f.range=%5B2002%20TO%202021%5D,equals'
);
});

View File

@@ -38,27 +38,38 @@ export class SearchOptions {
*/
toRestUrl(url: string, args: string[] = []): string {
if (isNotEmpty(this.configuration)) {
args.push(`configuration=${this.configuration}`);
args.push(`configuration=${encodeURIComponent(this.configuration)}`);
}
if (isNotEmpty(this.fixedFilter)) {
args.push(this.fixedFilter);
let fixedFilter: string;
const match = this.fixedFilter.match(/^([^=]+=)(.+)$/);
if (match) {
fixedFilter = match[1] + encodeURIComponent(match[2]);
} else {
fixedFilter = encodeURIComponent(this.fixedFilter);
}
args.push(fixedFilter);
}
if (isNotEmpty(this.query)) {
args.push(`query=${this.query}`);
args.push(`query=${encodeURIComponent(this.query)}`);
}
if (isNotEmpty(this.scope)) {
args.push(`scope=${this.scope}`);
args.push(`scope=${encodeURIComponent(this.scope)}`);
}
if (isNotEmpty(this.dsoTypes)) {
this.dsoTypes.forEach((dsoType: string) => {
args.push(`dsoType=${dsoType}`);
args.push(`dsoType=${encodeURIComponent(dsoType)}`);
});
}
if (isNotEmpty(this.filters)) {
this.filters.forEach((filter: SearchFilter) => {
filter.values.forEach((value) => {
const filterValue = value.includes(',') ? `${value}` : value + (filter.operator ? ',' + filter.operator : '');
args.push(`${filter.key}=${filterValue}`);
// we don't want commas to get URI-encoded
args.push(`${filter.key}=${encodeURIComponent(filterValue).replace(/%2C/g, ',')}`);
});
});
}