From a8f13bb0c11be602ff9325cca539ad700648d060 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Podlipsk=C3=BD?= Date: Wed, 13 Nov 2019 14:30:08 +0100 Subject: [PATCH] DataFrame processing: Require table rows to be array (#20357) (cherry picked from commit 4260cd548f14398e554ceb6b511067cc23477587) --- .../grafana-data/src/dataframe/processDataFrame.test.ts | 9 +++++++++ packages/grafana-data/src/dataframe/processDataFrame.ts | 8 +++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/grafana-data/src/dataframe/processDataFrame.test.ts b/packages/grafana-data/src/dataframe/processDataFrame.test.ts index 9f823dc035a..040851b97f4 100644 --- a/packages/grafana-data/src/dataframe/processDataFrame.test.ts +++ b/packages/grafana-data/src/dataframe/processDataFrame.test.ts @@ -59,6 +59,15 @@ describe('toDataFrame', () => { expect(again).toBe(input); }); + it('throws when table rows is not array', () => { + expect(() => + toDataFrame({ + columns: [], + rows: {}, + }) + ).toThrowError('Expected table rows to be array, got object.'); + }); + it('migrate from 6.3 style rows', () => { const oldDataFrame = { fields: [{ name: 'A' }, { name: 'B' }, { name: 'C' }], diff --git a/packages/grafana-data/src/dataframe/processDataFrame.ts b/packages/grafana-data/src/dataframe/processDataFrame.ts index e7854629179..808cf9eed2f 100644 --- a/packages/grafana-data/src/dataframe/processDataFrame.ts +++ b/packages/grafana-data/src/dataframe/processDataFrame.ts @@ -1,7 +1,5 @@ // Libraries -import isNumber from 'lodash/isNumber'; -import isString from 'lodash/isString'; -import isBoolean from 'lodash/isBoolean'; +import { isArray, isBoolean, isNumber, isString } from 'lodash'; // Types import { @@ -34,6 +32,10 @@ function convertTableToDataFrame(table: TableData): DataFrame { }; }); + if (!isArray(table.rows)) { + throw new Error(`Expected table rows to be array, got ${typeof table.rows}.`); + } + for (const row of table.rows) { for (let i = 0; i < fields.length; i++) { fields[i].values.buffer.push(row[i]);