From e3b3b35dca4ea3c8deae022ecbe57b59a1c8d867 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Fri, 15 Mar 2019 10:07:20 +0300 Subject: [PATCH 1/3] panels: fix loading panels with non-array targets --- public/app/features/dashboard/state/PanelModel.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/public/app/features/dashboard/state/PanelModel.ts b/public/app/features/dashboard/state/PanelModel.ts index 88065fdf208..1bac7cae3d6 100644 --- a/public/app/features/dashboard/state/PanelModel.ts +++ b/public/app/features/dashboard/state/PanelModel.ts @@ -126,7 +126,8 @@ export class PanelModel { ensureQueryIds() { if (this.targets) { - for (const query of this.targets) { + for (let i = 0; i < this.targets.length; i++) { + const query = this.targets[i]; if (!query.refId) { query.refId = this.getNextQueryLetter(); } From fe798239b225298550e3d052aaba59ae6da07c22 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Fri, 15 Mar 2019 10:26:56 +0300 Subject: [PATCH 2/3] panels: fix loading panels with non-array targets (refactor) --- public/app/features/dashboard/state/PanelModel.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/public/app/features/dashboard/state/PanelModel.ts b/public/app/features/dashboard/state/PanelModel.ts index 1bac7cae3d6..16d1f64f750 100644 --- a/public/app/features/dashboard/state/PanelModel.ts +++ b/public/app/features/dashboard/state/PanelModel.ts @@ -125,9 +125,8 @@ export class PanelModel { } ensureQueryIds() { - if (this.targets) { - for (let i = 0; i < this.targets.length; i++) { - const query = this.targets[i]; + if (this.targets && _.isArray(this.targets)) { + for (const query of this.targets) { if (!query.refId) { query.refId = this.getNextQueryLetter(); } From c028d410ecef70addb9f1acfbbed21c0750b03df Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Fri, 15 Mar 2019 10:49:41 +0300 Subject: [PATCH 3/3] panels: fix loading panels with non-array targets (add tests) --- .../features/dashboard/state/PanelModel.test.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/public/app/features/dashboard/state/PanelModel.test.ts b/public/app/features/dashboard/state/PanelModel.test.ts index 686a8ba6d28..82af0804029 100644 --- a/public/app/features/dashboard/state/PanelModel.test.ts +++ b/public/app/features/dashboard/state/PanelModel.test.ts @@ -3,9 +3,10 @@ import { PanelModel } from './PanelModel'; describe('PanelModel', () => { describe('when creating new panel model', () => { let model; + let modelJson; beforeEach(() => { - model = new PanelModel({ + modelJson = { type: 'table', showColumns: true, targets: [{ refId: 'A' }, { noRefId: true }], @@ -23,7 +24,8 @@ describe('PanelModel', () => { }, ], }, - }); + }; + model = new PanelModel(modelJson); }); it('should apply defaults', () => { @@ -38,6 +40,15 @@ describe('PanelModel', () => { expect(model.targets[1].refId).toBe('B'); }); + it("shouldn't break panel with non-array targets", () => { + modelJson.targets = { + 0: { refId: 'A' }, + foo: { bar: 'baz' }, + }; + model = new PanelModel(modelJson); + expect(model.targets[0].refId).toBe('A'); + }); + it('getSaveModel should remove defaults', () => { const saveModel = model.getSaveModel(); expect(saveModel.gridPos).toBe(undefined);