Allow getAsSavedObjectBody to return non-scripted fields when allowNoIndex is true

(cherry picked from commit 69b93da180)
This commit is contained in:
Jen Huang 2021-03-11 14:32:22 -08:00
parent 76dd02b6c0
commit afc91ce32f
3 changed files with 49 additions and 3 deletions

File diff suppressed because one or more lines are too long

View file

@ -17,7 +17,7 @@ import { stubbedSavedObjectIndexPattern } from './fixtures/stubbed_saved_object_
import { IndexPatternField } from '../fields';
import { fieldFormatsMock } from '../../field_formats/mocks';
import { FieldFormat } from '../..';
import { FieldFormat, IndexPatternSpec } from '../..';
import { RuntimeField } from '../types';
class MockFieldFormatter {}
@ -42,7 +42,7 @@ const runtimeField = {
fieldFormatsMock.getInstance = jest.fn().mockImplementation(() => new MockFieldFormatter()) as any;
// helper function to create index patterns
function create(id: string) {
function create(id: string, spec?: Partial<IndexPatternSpec>) {
const {
type,
version,
@ -58,6 +58,7 @@ function create(id: string) {
fields: { ...fields, runtime_field: runtimeField },
title,
runtimeFieldMap,
...(spec || {}),
},
fieldFormats: fieldFormatsMock,
shortDotsEnable: false,
@ -314,4 +315,15 @@ describe('IndexPattern', () => {
expect(restoredPattern.fields.length).toEqual(indexPattern.fields.length);
});
});
describe('getAsSavedObjectBody', () => {
test('should match snapshot', () => {
expect(indexPattern.getAsSavedObjectBody()).toMatchSnapshot();
});
test('allowNoIndex perserves all fields', () => {
const allowNoIndexIndexPattern = create('test-no-index-pattern', { allowNoIndex: true });
expect(allowNoIndexIndexPattern.getAsSavedObjectBody()).toMatchSnapshot();
});
});
});

View file

@ -322,7 +322,9 @@ export class IndexPattern implements IIndexPattern {
intervalName: this.intervalName,
sourceFilters: this.sourceFilters ? JSON.stringify(this.sourceFilters) : undefined,
fields: this.fields
? JSON.stringify(this.fields.filter((field) => field.scripted))
? JSON.stringify(
this.allowNoIndex ? this.fields : this.fields.filter((field) => field.scripted)
)
: undefined,
fieldFormatMap,
type: this.type,