From e4c7e21324b4b5a4f552c2757f192a867c4f1bbf Mon Sep 17 00:00:00 2001 From: Marco Vettorello Date: Mon, 10 Sep 2018 12:07:05 +0200 Subject: [PATCH] Fix _source formatting (#22800) (#22864) * Fix _source formatting * Update unit test * Add functional test * Fix CI error, move functional test to the end --- .../kibana/common/field_formats/types/source.js | 2 +- .../public/field_formats/__tests__/_source.js | 5 +++-- test/functional/apps/visualize/_data_table.js | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/core_plugins/kibana/common/field_formats/types/source.js b/src/core_plugins/kibana/common/field_formats/types/source.js index 81a6eb51eb20dd..61509a746c163f 100644 --- a/src/core_plugins/kibana/common/field_formats/types/source.js +++ b/src/core_plugins/kibana/common/field_formats/types/source.js @@ -48,7 +48,7 @@ export function createSourceFormat(FieldFormat) { SourceFormat.prototype._convert = { text: (value) => toJson(value), html: function sourceToHtml(source, field, hit) { - if (!field) return this.getConverterFor('text')(source, field, hit); + if (!field) return _.escape(this.getConverterFor('text')(source)); const highlights = (hit && hit.highlight) || {}; const formatted = field.indexPattern.formatHit(hit); diff --git a/src/core_plugins/kibana/public/field_formats/__tests__/_source.js b/src/core_plugins/kibana/public/field_formats/__tests__/_source.js index 09a84ae7754306..4726c3993771eb 100644 --- a/src/core_plugins/kibana/public/field_formats/__tests__/_source.js +++ b/src/core_plugins/kibana/public/field_formats/__tests__/_source.js @@ -43,8 +43,9 @@ describe('_source formatting', function () { })); it('should use the text content type if a field is not passed', function () { - const hit = _.first(hits); - expect(convertHtml(hit._source)).to.be(`${JSON.stringify(hit._source)}`); + const hit = { 'foo': 'bar', 'number': 42, 'hello': '

World

', 'also': 'with "quotes" or \'single quotes\'' }; + // eslint-disable-next-line + expect(convertHtml(hit)).to.be('{"foo":"bar","number":42,"hello":"<h1>World</h1>","also":"with \\"quotes\\" or 'single quotes'"}'); }); it('uses the _source, field, and hit to create a
', function () { diff --git a/test/functional/apps/visualize/_data_table.js b/test/functional/apps/visualize/_data_table.js index d6940437f24eef..9901df941bef16 100644 --- a/test/functional/apps/visualize/_data_table.js +++ b/test/functional/apps/visualize/_data_table.js @@ -178,5 +178,19 @@ export default function ({ getService, getPageObjects }) { '2015-09-20', '4,757', ]); }); + + it('should show correct data for a data table with top hits', async () => { + await PageObjects.visualize.navigateToNewVisualization(); + await PageObjects.visualize.clickDataTable(); + await PageObjects.visualize.clickNewSearch(); + await PageObjects.header.setAbsoluteRange(fromTime, toTime); + await PageObjects.visualize.clickMetricEditor(); + await PageObjects.visualize.selectAggregation('Top Hit', 'metrics'); + await PageObjects.visualize.selectField('_source', 'metrics'); + await PageObjects.visualize.clickGo(); + const data = await PageObjects.visualize.getTableVisData(); + log.debug(data); + expect(data.length).to.be.greaterThan(0); + }); }); }