Skip to content

Commit 38d7412

Browse files
authored
Merge pull request DSpace#2284 from atmire/fix-issue-with-colons-in-ids
Fix issue where a colon in a uuid would get misinterpreted as a route param
2 parents d2fa8cd + 244608a commit 38d7412

2 files changed

Lines changed: 44 additions & 13 deletions

File tree

src/app/shared/menu/menu.service.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,4 +567,41 @@ describe('MenuService', () => {
567567
});
568568
});
569569

570+
describe(`resolveSubstitutions`, () => {
571+
let linkPrefix;
572+
let link;
573+
let uuid;
574+
575+
beforeEach(() => {
576+
linkPrefix = 'statistics_collection_';
577+
link = `${linkPrefix}:id`;
578+
uuid = 'f7cc3ca4-3c2c-464d-8af8-add9f84f711c';
579+
});
580+
581+
it(`shouldn't do anything when there are no params`, () => {
582+
let result = (service as any).resolveSubstitutions(link, undefined);
583+
expect(result).toEqual(link);
584+
result = (service as any).resolveSubstitutions(link, null);
585+
expect(result).toEqual(link);
586+
result = (service as any).resolveSubstitutions(link, {});
587+
expect(result).toEqual(link);
588+
});
589+
590+
it(`should replace link params that are also route params`, () => {
591+
const result = (service as any).resolveSubstitutions(link,{ 'id': uuid });
592+
expect(result).toEqual(linkPrefix + uuid);
593+
});
594+
595+
it(`should not replace link params that aren't route params`, () => {
596+
const result = (service as any).resolveSubstitutions(link,{ 'something': 'else' });
597+
expect(result).toEqual(link);
598+
});
599+
600+
it(`should gracefully deal with routes that contain the name of the route param`, () => {
601+
const selfReferentialParam = `:id:something`;
602+
const result = (service as any).resolveSubstitutions(link,{ 'id': selfReferentialParam });
603+
expect(result).toEqual(linkPrefix + selfReferentialParam);
604+
});
605+
});
606+
570607
});

src/app/shared/menu/menu.service.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
ToggleActiveMenuSectionAction,
1818
ToggleMenuAction,
1919
} from './menu.actions';
20-
import { hasNoValue, hasValue, hasValueOperator, isNotEmpty } from '../empty.util';
20+
import { hasNoValue, hasValue, hasValueOperator, isNotEmpty, isEmpty } from '../empty.util';
2121
import { MenuState } from './menu-state.model';
2222
import { MenuSections } from './menu-sections.model';
2323
import { MenuSection } from './menu-section.model';
@@ -409,20 +409,14 @@ export class MenuService {
409409
}
410410

411411
protected resolveSubstitutions(object, params) {
412-
413412
let resolved;
414-
if (typeof object === 'string') {
413+
if (isEmpty(params)) {
415414
resolved = object;
416-
let match: RegExpMatchArray;
417-
do {
418-
match = resolved.match(/:(\w+)/);
419-
if (match) {
420-
const substitute = params[match[1]];
421-
if (hasValue(substitute)) {
422-
resolved = resolved.replace(match[0], `${substitute}`);
423-
}
424-
}
425-
} while (match);
415+
} else if (typeof object === 'string') {
416+
resolved = object;
417+
Object.entries(params).forEach(([key, value]: [string, string]) =>
418+
resolved = resolved.replaceAll(`:${key}`, value)
419+
);
426420
} else if (Array.isArray(object)) {
427421
resolved = [];
428422
object.forEach((entry, index) => {

0 commit comments

Comments
 (0)