From 604dd490fb000e51503bd57c8669ec30014dfd51 Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Mon, 10 Sep 2018 17:47:17 -0600 Subject: [PATCH] Controls vis - safely handle case where value can not be extracted from Kibana filter (#22885) (#22899) * handle case where value can not be extracted from Kibana filter * review feedback --- .../filter_manager/phrase_filter_manager.js | 9 ++++++++- .../filter_manager/phrase_filter_manager.test.js | 16 ++++++++++++++++ .../filter_manager/range_filter_manager.js | 7 +++++-- .../filter_manager/range_filter_manager.test.js | 14 ++++++++++++++ 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/core_plugins/input_control_vis/public/control/filter_manager/phrase_filter_manager.js b/src/core_plugins/input_control_vis/public/control/filter_manager/phrase_filter_manager.js index 1122e104cfe56..4c3a9265c3b67 100644 --- a/src/core_plugins/input_control_vis/public/control/filter_manager/phrase_filter_manager.js +++ b/src/core_plugins/input_control_vis/public/control/filter_manager/phrase_filter_manager.js @@ -61,10 +61,17 @@ export class PhraseFilterManager extends FilterManager { return; } - return kbnFilters + const values = kbnFilters .map((kbnFilter) => { return this._getValueFromFilter(kbnFilter); }) + .filter(value => value != null); + + if (values.length === 0) { + return; + } + + return values .reduce((accumulator, currentValue) => { return accumulator.concat(currentValue); }, []) diff --git a/src/core_plugins/input_control_vis/public/control/filter_manager/phrase_filter_manager.test.js b/src/core_plugins/input_control_vis/public/control/filter_manager/phrase_filter_manager.test.js index 0246cb02beeec..98b6eef1da51a 100644 --- a/src/core_plugins/input_control_vis/public/control/filter_manager/phrase_filter_manager.test.js +++ b/src/core_plugins/input_control_vis/public/control/filter_manager/phrase_filter_manager.test.js @@ -154,6 +154,22 @@ describe('PhraseFilterManager', function () { ]); expect(filterManager.getValueFromFilterBar()).to.eql([{ value: 'ios', label: 'ios' }, { value: 'win xp', label: 'win xp' }]); }); + + test('should return undefined when filter value can not be extracted from Kibana filter', function () { + filterManager.setMockFilters([ + { + query: { + match: { + myFieldWhichIsNotField1: { + query: 'ios', + type: 'phrase' + } + } + } + } + ]); + expect(filterManager.getValueFromFilterBar()).to.eql(undefined); + }); }); }); diff --git a/src/core_plugins/input_control_vis/public/control/filter_manager/range_filter_manager.js b/src/core_plugins/input_control_vis/public/control/filter_manager/range_filter_manager.js index 2217f0fc945b7..3ce646716f48c 100644 --- a/src/core_plugins/input_control_vis/public/control/filter_manager/range_filter_manager.js +++ b/src/core_plugins/input_control_vis/public/control/filter_manager/range_filter_manager.js @@ -69,14 +69,17 @@ export class RangeFilterManager extends FilterManager { return; } - let range = null; + let range; if (_.has(kbnFilters[0], 'script')) { range = _.get(kbnFilters[0], 'script.script.params'); } else { range = _.get(kbnFilters[0], ['range', this.fieldName]); } - return fromRange(range); + if (!range) { + return; + } + return fromRange(range); } } diff --git a/src/core_plugins/input_control_vis/public/control/filter_manager/range_filter_manager.test.js b/src/core_plugins/input_control_vis/public/control/filter_manager/range_filter_manager.test.js index 369afd735bd14..be700ad72ab42 100644 --- a/src/core_plugins/input_control_vis/public/control/filter_manager/range_filter_manager.test.js +++ b/src/core_plugins/input_control_vis/public/control/filter_manager/range_filter_manager.test.js @@ -91,6 +91,20 @@ describe('RangeFilterManager', function () { expect(value).to.have.property('max'); expect(value.max).to.be(3); }); + + test('should return undefined when filter value can not be extracted from Kibana filter', function () { + filterManager.setMockFilters([ + { + range: { + myFieldWhichIsNotField1: { + gte: 1, + lte: 3 + } + } + } + ]); + expect(filterManager.getValueFromFilterBar()).to.eql(undefined); + }); }); });