From 244608aa5c9e17938bf2fa43d03d5a1a0c5211b0 Mon Sep 17 00:00:00 2001 From: Art Lowel Date: Wed, 31 May 2023 12:11:15 +0200 Subject: [PATCH] fix issue where a colon in a uuid would get misinterpreted as a route param --- src/app/shared/menu/menu.service.spec.ts | 37 ++++++++++++++++++++++++ src/app/shared/menu/menu.service.ts | 20 +++++-------- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/app/shared/menu/menu.service.spec.ts b/src/app/shared/menu/menu.service.spec.ts index f4b0fe3db8..0d8d669a0a 100644 --- a/src/app/shared/menu/menu.service.spec.ts +++ b/src/app/shared/menu/menu.service.spec.ts @@ -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); + }); + }); + }); diff --git a/src/app/shared/menu/menu.service.ts b/src/app/shared/menu/menu.service.ts index 2253e9ce09..fac3b3fba7 100644 --- a/src/app/shared/menu/menu.service.ts +++ b/src/app/shared/menu/menu.service.ts @@ -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) => {