URLParams: Stringify true values as key=true always (fixes issues with variables with true value) (#106440)

* Restore prev fix

* added one more test

* Fix linkUrl issue

(cherry picked from commit 1564e1bac9)
This commit is contained in:
Torkel Ödegaard
2025-06-10 10:15:19 +00:00
committed by github-actions[bot]
parent c3ffd59702
commit e3bebe7f7a
6 changed files with 20 additions and 16 deletions
+6 -4
View File
@@ -11,7 +11,9 @@ describe('toUrlParams', () => {
isNull: null,
isUndefined: undefined,
});
expect(url).toBe('server=backend-01&hasSpace=has%20space&many=1&many=2&many=3&true&number=20&isNull=&isUndefined=');
expect(url).toBe(
'server=backend-01&hasSpace=has%20space&many=1&many=2&many=3&true=true&number=20&isNull=&isUndefined='
);
});
it('should encode the same way as angularjs', () => {
const url = urlUtil.toUrlParams({
@@ -25,7 +27,7 @@ describe('toUrlParams', () => {
bool1: true,
bool2: false,
});
expect(url).toBe('bool1&bool2=false');
expect(url).toBe('bool1=true&bool2=false');
});
it("should encode the following special characters [!'()*]", () => {
const url = urlUtil.toUrlParams({
@@ -45,7 +47,7 @@ describe('toUrlParams', () => {
oneMore: false,
});
expect(params).toBe(
'server=backend-01&hasSpace=has%20space&many=1&many=2&many=3&true&number=20&isNull=&isUndefined=&oneMore=false'
'server=backend-01&hasSpace=has%20space&many=1&many=2&many=3&true=true&number=20&isNull=&isUndefined=&oneMore=false'
);
});
@@ -61,7 +63,7 @@ describe('toUrlParams', () => {
bool1: true,
bool2: false,
});
expect(url).toBe('bool1&bool2=false');
expect(url).toBe('bool1=true&bool2=false');
});
});
+1 -6
View File
@@ -60,12 +60,7 @@ function toUrlParams(a: any, encodeAsAngularJS = true) {
const add = (k: string, v: any) => {
v = typeof v === 'function' ? v() : v === null ? '' : v === undefined ? '' : v;
if (typeof v !== 'boolean') {
s[s.length] = encodingFunction(k, true) + '=' + encodingFunction(v, true);
} else {
const valueQueryPart = v ? '' : '=' + encodingFunction('false', true);
s[s.length] = encodingFunction(k, true) + valueQueryPart;
}
s[s.length] = encodingFunction(k, true) + '=' + encodingFunction(v, true);
};
const buildParams = (prefix: string, obj: any) => {
@@ -38,6 +38,13 @@ describe('LocationService', () => {
expect(locationService.getLocation().search).toBe('?servers=A&servers=B&servers=C');
});
it('should handle boolean string values', () => {
locationService.push('/?query1=false&query2=true&query3');
locationService.partial({ newProp: 'a' });
expect(locationService.getLocation().search).toBe('?query1=false&query2=true&query3=true&newProp=a');
});
it('persist state', () => {
locationService.push({
pathname: '/d/123',
@@ -88,7 +88,7 @@ describe('ShareLinkTab', () => {
await screen.findByRole('link', { name: selectors.pages.SharePanelModal.linkToRenderedImage })
).toHaveAttribute(
'href',
'http://dashboards.grafana.com/grafana/render/d-solo/dash-1?from=2019-02-11T13:00:00.000Z&to=2019-02-11T19:00:00.000Z&panelId=panel-12&__feature.dashboardSceneSolo&width=1000&height=500&tz=Pacific%2FEaster'
'http://dashboards.grafana.com/grafana/render/d-solo/dash-1?from=2019-02-11T13:00:00.000Z&to=2019-02-11T19:00:00.000Z&panelId=panel-12&__feature.dashboardSceneSolo=true&width=1000&height=500&tz=Pacific%2FEaster'
);
});
});
@@ -297,7 +297,7 @@ describe('timeSrv', () => {
timeSrv.setTime({ from: 'now-1h', to: 'now-10s' });
timeSrv.setTime({ from: 'now-1h', to: 'now-10s' });
expect(locationUpdates[1].search).toEqual('?kiosk&from=now-1h&to=now-10s');
expect(locationUpdates[1].search).toEqual('?kiosk=true&from=now-1h&to=now-10s');
});
it('should not change the URL if the updateUrl param is false', () => {
@@ -252,18 +252,18 @@ export interface LinkService {
export class LinkSrv implements LinkService {
getLinkUrl(link: DashboardLink) {
let params: { [key: string]: boolean } = {};
let url = link.url ?? '';
if (link.keepTime) {
params[`\$${DataLinkBuiltInVars.keepTime}`] = true;
url = urlUtil.appendQueryToUrl(url, `\$${DataLinkBuiltInVars.keepTime}`);
}
if (link.includeVars) {
params[`\$${DataLinkBuiltInVars.includeVars}`] = true;
url = urlUtil.appendQueryToUrl(url, `\$${DataLinkBuiltInVars.includeVars}`);
}
let url = locationUtil.assureBaseUrl(urlUtil.appendQueryToUrl(link.url || '', urlUtil.toUrlParams(params)));
url = getTemplateSrv().replace(url);
url = locationUtil.assureBaseUrl(url);
return getConfig().disableSanitizeHtml ? url : textUtil.sanitizeUrl(url);
}