DataLinks: Fixes interpolation (formatting) of __all_variables and __url_time_range (#65162)

* DataLinks: Fixes interpolation (formatting) of __all_variables and __url_time_range

* simplify if statement
This commit is contained in:
Torkel Ödegaard
2023-03-22 18:56:18 +01:00
committed by GitHub
parent 0409cfd116
commit cd490ba0be
9 changed files with 28 additions and 11 deletions

View File

@@ -647,11 +647,13 @@ export class PanelModel implements DataConfigSource, IPanelModel {
vars[DataLinkBuiltInVars.keepTime] = {
text: timeRangeUrl,
value: timeRangeUrl,
skipFormat: true,
};
vars[DataLinkBuiltInVars.includeVars] = {
text: variablesQuery,
value: variablesQuery,
skipFormat: true,
};
return getTemplateSrv().replace(value, vars, format);

View File

@@ -3,7 +3,7 @@ import {
DataSourceInstanceSettings,
DataSourcePlugin,
DataSourcePluginMeta,
ScopedVar,
ScopedVars,
} from '@grafana/data';
import { ExpressionDatasourceRef } from '@grafana/runtime/src/utils/DataSourceWithBackend';
import { DatasourceSrv } from 'app/features/plugins/datasource_srv';
@@ -26,7 +26,7 @@ const templateSrv: any = {
},
},
],
replace: (v: string, scopedVars: ScopedVar) => {
replace: (v: string, scopedVars: ScopedVars) => {
if (scopedVars && scopedVars.datasource) {
return v.replace('${datasource}', scopedVars.datasource.value);
}

View File

@@ -821,6 +821,15 @@ describe('templateSrv', () => {
const target = _templateSrv.replace('${adhoc}', { adhoc: { value: 'value2', text: 'value2' } }, 'queryparam');
expect(target).toBe('var-adhoc=value2');
});
it('Variable named ${__all_variables} is already formatted so skip any formatting', () => {
const target = _templateSrv.replace(
'${__all_variables}',
{ __all_variables: { value: 'var-server=server+name+with+plus%2B', skipFormat: true } },
'percentencode'
);
expect(target).toBe('var-server=server+name+with+plus%2B');
});
});
describe('scenes compatibility', () => {

View File

@@ -295,13 +295,17 @@ export class TemplateSrv implements BaseTemplateSrv {
return target.replace(this.regex, (match, var1, var2, fmt2, var3, fieldPath, fmt3) => {
const variableName = var1 || var2 || var3;
const variable = this.getVariableAtIndex(variableName);
const fmt = fmt2 || fmt3 || format;
let fmt = fmt2 || fmt3 || format;
if (scopedVars) {
const value = this.getVariableValue(variableName, fieldPath, scopedVars);
const text = this.getVariableText(variableName, value, scopedVars);
if (value !== null && value !== undefined) {
if (scopedVars[variableName].skipFormat) {
fmt = undefined;
}
return this.formatValue(value, fmt, variable, text);
}
}

View File

@@ -107,7 +107,7 @@ describe('getAllVariableValuesForUrl', () => {
it('should not set scoped value as url params', () => {
const params = getVariablesUrlParams({
test: { name: 'test', value: 'val1', text: 'val1text', skipUrlSync: true },
test: { value: 'val1', text: 'val1text', skipUrlSync: true },
});
expect(params['var-test']).toBe(undefined);
});