mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 01:54:15 +00:00
fix issue where a colon in a uuid would get misinterpreted as a route param
This commit is contained in:
@@ -567,4 +567,41 @@ describe('MenuService', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe(`resolveSubstitutions`, () => {
|
||||
let linkPrefix;
|
||||
let link;
|
||||
let uuid;
|
||||
|
||||
beforeEach(() => {
|
||||
linkPrefix = 'statistics_collection_';
|
||||
link = `${linkPrefix}:id`;
|
||||
uuid = 'f7cc3ca4-3c2c-464d-8af8-add9f84f711c';
|
||||
});
|
||||
|
||||
it(`shouldn't do anything when there are no params`, () => {
|
||||
let result = (service as any).resolveSubstitutions(link, undefined);
|
||||
expect(result).toEqual(link);
|
||||
result = (service as any).resolveSubstitutions(link, null);
|
||||
expect(result).toEqual(link);
|
||||
result = (service as any).resolveSubstitutions(link, {});
|
||||
expect(result).toEqual(link);
|
||||
});
|
||||
|
||||
it(`should replace link params that are also route params`, () => {
|
||||
const result = (service as any).resolveSubstitutions(link,{ 'id': uuid });
|
||||
expect(result).toEqual(linkPrefix + uuid);
|
||||
});
|
||||
|
||||
it(`should not replace link params that aren't route params`, () => {
|
||||
const result = (service as any).resolveSubstitutions(link,{ 'something': 'else' });
|
||||
expect(result).toEqual(link);
|
||||
});
|
||||
|
||||
it(`should gracefully deal with routes that contain the name of the route param`, () => {
|
||||
const selfReferentialParam = `:id:something`;
|
||||
const result = (service as any).resolveSubstitutions(link,{ 'id': selfReferentialParam });
|
||||
expect(result).toEqual(linkPrefix + selfReferentialParam);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
@@ -17,7 +17,7 @@ import {
|
||||
ToggleActiveMenuSectionAction,
|
||||
ToggleMenuAction,
|
||||
} from './menu.actions';
|
||||
import { hasNoValue, hasValue, hasValueOperator, isNotEmpty } from '../empty.util';
|
||||
import { hasNoValue, hasValue, hasValueOperator, isNotEmpty, isEmpty } from '../empty.util';
|
||||
import { MenuState } from './menu-state.model';
|
||||
import { MenuSections } from './menu-sections.model';
|
||||
import { MenuSection } from './menu-section.model';
|
||||
@@ -409,20 +409,14 @@ export class MenuService {
|
||||
}
|
||||
|
||||
protected resolveSubstitutions(object, params) {
|
||||
|
||||
let resolved;
|
||||
if (typeof object === 'string') {
|
||||
if (isEmpty(params)) {
|
||||
resolved = object;
|
||||
let match: RegExpMatchArray;
|
||||
do {
|
||||
match = resolved.match(/:(\w+)/);
|
||||
if (match) {
|
||||
const substitute = params[match[1]];
|
||||
if (hasValue(substitute)) {
|
||||
resolved = resolved.replace(match[0], `${substitute}`);
|
||||
}
|
||||
}
|
||||
} while (match);
|
||||
} else if (typeof object === 'string') {
|
||||
resolved = object;
|
||||
Object.entries(params).forEach(([key, value]: [string, string]) =>
|
||||
resolved = resolved.replaceAll(`:${key}`, value)
|
||||
);
|
||||
} else if (Array.isArray(object)) {
|
||||
resolved = [];
|
||||
object.forEach((entry, index) => {
|
||||
|
Reference in New Issue
Block a user