Skip to content

Commit

Permalink
Fix removing a single field formatter (elastic#141078)
Browse files Browse the repository at this point in the history
(cherry picked from commit f44f1d1)
  • Loading branch information
Dosant committed Sep 21, 2022
1 parent d6c97fa commit 51e4974
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/plugins/data_views/common/data_views/data_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
} from '@kbn/field-formats-plugin/common';
import { castEsToKbnFieldTypeName, ES_FIELD_TYPES, KBN_FIELD_TYPES } from '@kbn/field-types';
import { CharacterNotAllowedInField } from '@kbn/kibana-utils-plugin/common';
import _, { cloneDeep, each, reject } from 'lodash';
import { cloneDeep, each, reject } from 'lodash';
import type { DataViewAttributes, FieldAttrs, FieldAttrSet } from '..';
import type { DataViewField, IIndexPatternFieldList } from '../fields';
import { fieldList } from '../fields';
Expand Down Expand Up @@ -395,9 +395,6 @@ export class DataView implements DataViewBase {
* Returns index pattern as saved object body for saving
*/
getAsSavedObjectBody(): DataViewAttributes {
const fieldFormatMap = _.isEmpty(this.fieldFormatMap)
? undefined
: JSON.stringify(this.fieldFormatMap);
const fieldAttrs = this.getFieldAttrs();
const runtimeFieldMap = this.runtimeFieldMap;

Expand All @@ -407,7 +404,7 @@ export class DataView implements DataViewBase {
timeFieldName: this.timeFieldName,
sourceFilters: this.sourceFilters ? JSON.stringify(this.sourceFilters) : undefined,
fields: JSON.stringify(this.fields?.filter((field) => field.scripted) ?? []),
fieldFormatMap,
fieldFormatMap: this.fieldFormatMap ? JSON.stringify(this.fieldFormatMap) : undefined,
type: this.type!,
typeMeta: JSON.stringify(this.typeMeta ?? {}),
allowNoIndex: this.allowNoIndex ? this.allowNoIndex : undefined,
Expand Down
20 changes: 20 additions & 0 deletions src/plugins/data_views/common/data_views/data_views.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,26 @@ describe('IndexPatterns', () => {
expect(async () => await indexPatterns.get(id)).toBeDefined();
});

test('can set and remove field format', async () => {
const id = 'id';
setDocsourcePayload(id, savedObject);
const dataView = await indexPatterns.get(id);
dataView.setFieldFormat('field', { id: 'formatId' });
await indexPatterns.updateSavedObject(dataView);
let lastCall = (savedObjectsClient.update as jest.Mock).mock.calls.pop() ?? [];
let [, , attrs] = lastCall;
expect(attrs).toHaveProperty('fieldFormatMap');
expect(attrs.fieldFormatMap).toMatchInlineSnapshot(`"{\\"field\\":{\\"id\\":\\"formatId\\"}}"`);
dataView.deleteFieldFormat('field');
await indexPatterns.updateSavedObject(dataView);
lastCall = (savedObjectsClient.update as jest.Mock).mock.calls.pop() ?? [];
[, , attrs] = lastCall;

// https://github.com/elastic/kibana/issues/134873: must keep an empty object and not delete it
expect(attrs).toHaveProperty('fieldFormatMap');
expect(attrs.fieldFormatMap).toMatchInlineSnapshot(`"{}"`);
});

describe('getDefaultDataView', () => {
beforeEach(() => {
indexPatterns.clearCache();
Expand Down

0 comments on commit 51e4974

Please sign in to comment.