fix issue where a colon in a uuid would get misinterpreted as a route param

This commit is contained in:
Art Lowel
2023-05-31 12:11:15 +02:00
parent d2fa8cda6a
commit 244608aa5c
2 changed files with 44 additions and 13 deletions

View File

@@ -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);
});
});
}); });

View File

@@ -17,7 +17,7 @@ import {
ToggleActiveMenuSectionAction, ToggleActiveMenuSectionAction,
ToggleMenuAction, ToggleMenuAction,
} from './menu.actions'; } 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 { MenuState } from './menu-state.model';
import { MenuSections } from './menu-sections.model'; import { MenuSections } from './menu-sections.model';
import { MenuSection } from './menu-section.model'; import { MenuSection } from './menu-section.model';
@@ -409,20 +409,14 @@ export class MenuService {
} }
protected resolveSubstitutions(object, params) { protected resolveSubstitutions(object, params) {
let resolved; let resolved;
if (typeof object === 'string') { if (isEmpty(params)) {
resolved = object; resolved = object;
let match: RegExpMatchArray; } else if (typeof object === 'string') {
do { resolved = object;
match = resolved.match(/:(\w+)/); Object.entries(params).forEach(([key, value]: [string, string]) =>
if (match) { resolved = resolved.replaceAll(`:${key}`, value)
const substitute = params[match[1]]; );
if (hasValue(substitute)) {
resolved = resolved.replace(match[0], `${substitute}`);
}
}
} while (match);
} else if (Array.isArray(object)) { } else if (Array.isArray(object)) {
resolved = []; resolved = [];
object.forEach((entry, index) => { object.forEach((entry, index) => {