Skip to content

Commit 4f08ef0

Browse files
authored
Format top-level object values in data table (7.17) (#234964)
## Summary Applies changes from #234228 to 7.17. ### Checklist Check the PR satisfies following conditions. Reviewers should verify this PR satisfies this list as well. - [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [ ] This was checked for breaking HTTP API changes, and any breaking changes have been approved by the breaking-change committee. The `release_note:breaking` label should be applied in these situations. - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [ ] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) - [ ] Review the [backport guidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing) and apply applicable `backport:*` labels. ### Identify risks Does this PR introduce any risks? For example, consider risks like hard to test bugs, performance regression, potential of data loss. Describe the risk, its severity, and mitigation for each identified risk. Invite stakeholders and evaluate how to proceed before merging. - [ ] [See some risk examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) - [ ] ...
1 parent 28506f5 commit 4f08ef0

File tree

4 files changed

+92
-26
lines changed

4 files changed

+92
-26
lines changed

src/plugins/discover/public/application/apps/main/components/doc_table/lib/row_formatter.test.ts

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { setServices } from '../../../../../../kibana_services';
1414
import { DiscoverServices } from '../../../../../../build_services';
1515
import { stubbedSavedObjectIndexPattern } from '../../../../../../../../data/common/stubs';
1616

17+
let mockConvert = jest.fn();
1718
describe('Row formatter', () => {
1819
const hit = {
1920
_id: 'a',
@@ -49,12 +50,13 @@ describe('Row formatter', () => {
4950
const fieldsToShow = indexPattern.fields.getAll().map((fld) => fld.name);
5051

5152
beforeEach(() => {
53+
mockConvert = jest.fn((value) => value);
5254
setServices({
5355
uiSettings: {
5456
get: () => 100,
5557
},
5658
fieldFormats: {
57-
getDefaultInstance: jest.fn(() => ({ convert: (value: unknown) => value })),
59+
getDefaultInstance: jest.fn(() => ({ convert: (value: unknown) => mockConvert(value) })),
5860
getFormatterForField: jest.fn(() => ({ convert: (value: unknown) => value })),
5961
},
6062
} as unknown as DiscoverServices);
@@ -197,6 +199,8 @@ describe('Row formatter', () => {
197199
expect(
198200
formatTopLevelObject(
199201
{
202+
_id: '1',
203+
_index: 'test',
200204
fields: {
201205
'object.value': [5, 10],
202206
},
@@ -230,7 +234,11 @@ describe('Row formatter', () => {
230234
});
231235
const formatted = ReactDOM.renderToStaticMarkup(
232236
formatTopLevelObject(
233-
{ fields: { 'a.zzz': [100], 'a.ccc': [50] } },
237+
{
238+
_id: '1',
239+
_index: 'test',
240+
fields: { 'a.zzz': [100], 'a.ccc': [50] },
241+
},
234242
{ 'a.zzz': [100], 'a.ccc': [50], getByName: jest.fn() },
235243
indexPattern
236244
)
@@ -248,12 +256,14 @@ describe('Row formatter', () => {
248256
expect(
249257
formatTopLevelObject(
250258
{
259+
_id: '1',
260+
_index: 'test',
251261
fields: {
252262
'object.value': [5, 10],
253263
'object.keys': ['a', 'b'],
254264
},
255265
highlight: {
256-
'object.keys': 'a',
266+
'object.keys': ['a'],
257267
},
258268
},
259269
{
@@ -287,6 +297,8 @@ describe('Row formatter', () => {
287297
expect(
288298
formatTopLevelObject(
289299
{
300+
_id: '1',
301+
_index: 'test',
290302
fields: {
291303
'object.value': [5, 10],
292304
},
@@ -310,4 +322,35 @@ describe('Row formatter', () => {
310322
/>
311323
`);
312324
});
325+
326+
it('passes values through default formatter for unmapped objects', () => {
327+
mockConvert = jest.fn((value: unknown) => `${value}`.replaceAll('foo', 'bar'));
328+
indexPattern.getFieldByName = jest.fn().mockReturnValue(undefined);
329+
setServices({
330+
uiSettings: {
331+
get: () => 100,
332+
},
333+
fieldFormats: {
334+
getDefaultInstance: jest.fn(() => ({ convert: (value: unknown) => mockConvert(value) })),
335+
getFormatterForField: jest.fn(() => ({ convert: (value: unknown) => mockConvert(value) })),
336+
},
337+
} as unknown as DiscoverServices);
338+
339+
const formatted = formatTopLevelObject(
340+
{
341+
_id: '1',
342+
_index: 'test',
343+
fields: {
344+
'foo.data': ['my foo value'],
345+
},
346+
},
347+
{
348+
'foo.data': ['my foo value'],
349+
getByName: jest.fn(),
350+
},
351+
indexPattern
352+
);
353+
expect(mockConvert).toHaveBeenCalled();
354+
expect(ReactDOM.renderToStaticMarkup(formatted)).toContain('my bar value');
355+
});
313356
});

src/plugins/discover/public/application/apps/main/components/doc_table/lib/row_formatter.tsx

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { getServices } from '../../../../../../kibana_services';
1414
import { formatHit } from '../../../../../helpers/format_hit';
1515

1616
import './row_formatter.scss';
17+
import { formatFieldValue } from '../../../../../helpers/format_value';
1718

1819
interface Props {
1920
defPairs: Array<[string, string]>;
@@ -45,8 +46,7 @@ export const formatRow = (
4546
};
4647

4748
export const formatTopLevelObject = (
48-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
49-
row: Record<string, any>,
49+
row: estypes.SearchHit,
5050
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5151
fields: Record<string, any>,
5252
indexPattern: IndexPattern
@@ -58,18 +58,9 @@ export const formatTopLevelObject = (
5858
sorted.forEach(([key, values]) => {
5959
const field = indexPattern.getFieldByName(key);
6060
const displayKey = fields.getByName ? fields.getByName(key)?.displayName : undefined;
61-
const formatter = field
62-
? indexPattern.getFormatterForField(field)
63-
: { convert: (v: unknown, ...rest: unknown[]) => String(v) };
6461
if (!values.map) return;
6562
const formatted = values
66-
.map((val: unknown) =>
67-
formatter.convert(val, 'html', {
68-
field,
69-
hit: row,
70-
indexPattern,
71-
})
72-
)
63+
.map((val: unknown) => formatFieldValue(val, row, indexPattern, field))
7364
.join(', ');
7465
const pairs = highlights[key] ? highlightPairs : sourcePairs;
7566
pairs.push([displayKey ? displayKey : key, formatted]);

src/plugins/discover/public/application/components/discover_grid/get_render_cell_value.test.tsx

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ jest.mock('../../../../../kibana_react/public', () => ({
2020
},
2121
}));
2222

23+
let mockConvert = jest.fn();
2324
jest.mock('../../../kibana_services', () => ({
2425
getServices: () => ({
2526
uiSettings: {
2627
get: jest.fn(),
2728
},
2829
fieldFormats: {
29-
getDefaultInstance: jest.fn(() => ({ convert: (value: unknown) => (value ? value : '-') })),
30+
getDefaultInstance: jest.fn(() => ({ convert: (value: unknown) => mockConvert(value) })),
3031
},
3132
}),
3233
}));
@@ -77,6 +78,10 @@ const flatten = (hit: ElasticSearchHit): Record<string, unknown> => {
7778
};
7879

7980
describe('Discover grid cell rendering', function () {
81+
beforeEach(() => {
82+
mockConvert = jest.fn((value) => (value ? value : '-'));
83+
});
84+
8085
it('renders bytes column correctly', () => {
8186
const DiscoverGridCellValue = getRenderCellValueFn(
8287
indexPatternMock,
@@ -665,4 +670,40 @@ describe('Discover grid cell rendering', function () {
665670
/>
666671
`);
667672
});
673+
674+
it('passes values through default formatter for unmapped objects', () => {
675+
mockConvert = jest.fn((value: unknown) => `${value}`.replaceAll('foo', 'bar'));
676+
(indexPatternMock.getFieldByName as jest.Mock).mockReturnValueOnce(undefined);
677+
const rowsFieldsUnmapped: ElasticSearchHit[] = [
678+
{
679+
_id: '1',
680+
_index: 'test',
681+
_score: 1,
682+
_source: undefined,
683+
fields: {
684+
'foo.data': ['my foo value'],
685+
},
686+
},
687+
];
688+
const DiscoverGridCellValue = getRenderCellValueFn(
689+
indexPatternMock,
690+
rowsFieldsUnmapped,
691+
rowsFieldsUnmapped.map(flatten),
692+
true,
693+
['unmapped'],
694+
100
695+
);
696+
const component = shallow(
697+
<DiscoverGridCellValue
698+
rowIndex={0}
699+
columnId="foo"
700+
isDetails={false}
701+
isExpanded={false}
702+
isExpandable={true}
703+
setCellProps={jest.fn()}
704+
/>
705+
);
706+
expect(mockConvert).toHaveBeenCalled();
707+
expect(component.html()).toContain('my bar value');
708+
});
668709
});

src/plugins/discover/public/application/components/discover_grid/get_render_cell_value.tsx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,8 @@ export const getRenderCellValueFn =
9393
const displayKey = indexPattern.fields.getByName
9494
? indexPattern.fields.getByName(key)?.displayName
9595
: undefined;
96-
const formatter = subField
97-
? indexPattern.getFormatterForField(subField)
98-
: { convert: (v: unknown, ...rest: unknown[]) => String(v) };
9996
const formatted = (values as unknown[])
100-
.map((val: unknown) =>
101-
formatter.convert(val, 'html', {
102-
field: subField,
103-
hit: row,
104-
indexPattern,
105-
})
106-
)
97+
.map((val: unknown) => formatFieldValue(val, row, indexPattern, subField))
10798
.join(', ');
10899
const pairs = highlights[key] ? highlightPairs : sourcePairs;
109100
if (displayKey) {

0 commit comments

Comments
 (0)