From 21ed77d7a59e94a9c47b13622f0d70a68aa60b8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 12 Oct 2020 14:22:26 +0200 Subject: [PATCH] TemplateSrv: Fix interpolating strings with object variables (#28171) --- .../features/templating/template_srv.test.ts | 31 +++++++++++++++++++ .../app/features/templating/template_srv.ts | 9 ++++++ 2 files changed, 40 insertions(+) diff --git a/public/app/features/templating/template_srv.test.ts b/public/app/features/templating/template_srv.test.ts index d4acb2608a4..aa7a203ad84 100644 --- a/public/app/features/templating/template_srv.test.ts +++ b/public/app/features/templating/template_srv.test.ts @@ -687,4 +687,35 @@ describe('templateSrv', () => { 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'); + }); + }); }); diff --git a/public/app/features/templating/template_srv.ts b/public/app/features/templating/template_srv.ts index 2ffab87fc1f..5dfc72a360f 100644 --- a/public/app/features/templating/template_srv.ts +++ b/public/app/features/templating/template_srv.ts @@ -112,6 +112,15 @@ export class TemplateSrv implements BaseTemplateSrv { // for some scopedVars there is no 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') { return format(value, variable, this.formatValue); }