mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
TemplateSrv: Fix interpolating strings with object variables (#28171)
This commit is contained in:
parent
92c2a6c239
commit
21ed77d7a5
@ -687,4 +687,35 @@ describe('templateSrv', () => {
|
|||||||
expect(target).toBe('2020-07');
|
expect(target).toBe('2020-07');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('handle objects gracefully', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
initTemplateSrv([{ type: 'query', name: 'test', current: { value: { test: 'A' } } }]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not pass object to custom function', () => {
|
||||||
|
let passedValue: any = null;
|
||||||
|
_templateSrv.replace('this.${test}.filters', {}, (value: any) => {
|
||||||
|
passedValue = value;
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(passedValue).toBe('[object Object]');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('handle objects gracefully and call toString if defined', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
const value = { test: 'A', toString: () => 'hello' };
|
||||||
|
initTemplateSrv([{ type: 'query', name: 'test', current: { value } }]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not pass object to custom function', () => {
|
||||||
|
let passedValue: any = null;
|
||||||
|
_templateSrv.replace('this.${test}.filters', {}, (value: any) => {
|
||||||
|
passedValue = value;
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(passedValue).toBe('hello');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -112,6 +112,15 @@ export class TemplateSrv implements BaseTemplateSrv {
|
|||||||
// for some scopedVars there is no variable
|
// for some scopedVars there is no variable
|
||||||
variable = variable || {};
|
variable = variable || {};
|
||||||
|
|
||||||
|
if (value === null || value === undefined) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
// if it's an object transform value to string
|
||||||
|
if (!Array.isArray(value) && typeof value === 'object') {
|
||||||
|
value = `${value}`;
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof format === 'function') {
|
if (typeof format === 'function') {
|
||||||
return format(value, variable, this.formatValue);
|
return format(value, variable, this.formatValue);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user