mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 01:54:15 +00:00
78991: URI-encode SearchOptions query values
This commit is contained in:
@@ -8,7 +8,11 @@ describe('PaginatedSearchOptions', () => {
|
|||||||
let options: PaginatedSearchOptions;
|
let options: PaginatedSearchOptions;
|
||||||
const sortOptions = new SortOptions('test.field', SortDirection.DESC);
|
const sortOptions = new SortOptions('test.field', SortDirection.DESC);
|
||||||
const pageOptions = Object.assign(new PaginationComponentOptions(), { pageSize: 40, page: 1 });
|
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 query = 'search query';
|
||||||
const scope = '0fde1ecb-82cc-425a-b600-ac3576d76b47';
|
const scope = '0fde1ecb-82cc-425a-b600-ac3576d76b47';
|
||||||
const baseUrl = 'www.rest.com';
|
const baseUrl = 'www.rest.com';
|
||||||
@@ -31,12 +35,13 @@ describe('PaginatedSearchOptions', () => {
|
|||||||
'sort=test.field,DESC&' +
|
'sort=test.field,DESC&' +
|
||||||
'page=0&' +
|
'page=0&' +
|
||||||
'size=40&' +
|
'size=40&' +
|
||||||
'query=search query&' +
|
'query=search%20query&' +
|
||||||
'scope=0fde1ecb-82cc-425a-b600-ac3576d76b47&' +
|
'scope=0fde1ecb-82cc-425a-b600-ac3576d76b47&' +
|
||||||
'dsoType=ITEM&' +
|
'dsoType=ITEM&' +
|
||||||
'f.test=value&' +
|
'f.test=value&' +
|
||||||
'f.example=another value&' +
|
'f.example=another%20value&' +
|
||||||
'f.example=second value'
|
'f.example=second%20value&' +
|
||||||
|
'f.range=%5B2002%20TO%202021%5D,equals'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@@ -4,8 +4,13 @@ import { SearchFilter } from './search-filter.model';
|
|||||||
import { DSpaceObjectType } from '../../core/shared/dspace-object-type.model';
|
import { DSpaceObjectType } from '../../core/shared/dspace-object-type.model';
|
||||||
|
|
||||||
describe('SearchOptions', () => {
|
describe('SearchOptions', () => {
|
||||||
let options: PaginatedSearchOptions;
|
let options: SearchOptions;
|
||||||
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 query = 'search query';
|
||||||
const scope = '0fde1ecb-82cc-425a-b600-ac3576d76b47';
|
const scope = '0fde1ecb-82cc-425a-b600-ac3576d76b47';
|
||||||
const baseUrl = 'www.rest.com';
|
const baseUrl = 'www.rest.com';
|
||||||
@@ -18,12 +23,13 @@ describe('SearchOptions', () => {
|
|||||||
it('should generate a string with all parameters that are present', () => {
|
it('should generate a string with all parameters that are present', () => {
|
||||||
const outcome = options.toRestUrl(baseUrl);
|
const outcome = options.toRestUrl(baseUrl);
|
||||||
expect(outcome).toEqual('www.rest.com?' +
|
expect(outcome).toEqual('www.rest.com?' +
|
||||||
'query=search query&' +
|
'query=search%20query&' +
|
||||||
'scope=0fde1ecb-82cc-425a-b600-ac3576d76b47&' +
|
'scope=0fde1ecb-82cc-425a-b600-ac3576d76b47&' +
|
||||||
'dsoType=ITEM&' +
|
'dsoType=ITEM&' +
|
||||||
'f.test=value&' +
|
'f.test=value&' +
|
||||||
'f.example=another value&' +
|
'f.example=another%20value&' +
|
||||||
'f.example=second value'
|
'f.example=second%20value&' +
|
||||||
|
'f.range=%5B2002%20TO%202021%5D,equals'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@@ -38,27 +38,38 @@ export class SearchOptions {
|
|||||||
*/
|
*/
|
||||||
toRestUrl(url: string, args: string[] = []): string {
|
toRestUrl(url: string, args: string[] = []): string {
|
||||||
if (isNotEmpty(this.configuration)) {
|
if (isNotEmpty(this.configuration)) {
|
||||||
args.push(`configuration=${this.configuration}`);
|
args.push(`configuration=${encodeURIComponent(this.configuration)}`);
|
||||||
}
|
}
|
||||||
if (isNotEmpty(this.fixedFilter)) {
|
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)) {
|
if (isNotEmpty(this.query)) {
|
||||||
args.push(`query=${this.query}`);
|
args.push(`query=${encodeURIComponent(this.query)}`);
|
||||||
}
|
}
|
||||||
if (isNotEmpty(this.scope)) {
|
if (isNotEmpty(this.scope)) {
|
||||||
args.push(`scope=${this.scope}`);
|
args.push(`scope=${encodeURIComponent(this.scope)}`);
|
||||||
}
|
}
|
||||||
if (isNotEmpty(this.dsoTypes)) {
|
if (isNotEmpty(this.dsoTypes)) {
|
||||||
this.dsoTypes.forEach((dsoType: string) => {
|
this.dsoTypes.forEach((dsoType: string) => {
|
||||||
args.push(`dsoType=${dsoType}`);
|
args.push(`dsoType=${encodeURIComponent(dsoType)}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (isNotEmpty(this.filters)) {
|
if (isNotEmpty(this.filters)) {
|
||||||
this.filters.forEach((filter: SearchFilter) => {
|
this.filters.forEach((filter: SearchFilter) => {
|
||||||
filter.values.forEach((value) => {
|
filter.values.forEach((value) => {
|
||||||
const filterValue = value.includes(',') ? `${value}` : value + (filter.operator ? ',' + filter.operator : '');
|
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, ',')}`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user