From 56edb43cea50a41593ed7eba8e64ae0533eb2985 Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Wed, 11 Mar 2020 16:45:36 +0100 Subject: [PATCH 01/12] move search source parsing and serializing to data --- .../public/saved_visualizations/_saved_vis.ts | 5 +- src/plugins/data/public/index.ts | 2 + src/plugins/data/public/search/index.ts | 2 + .../data/public/search/search_source/index.ts | 2 + .../search_source}/parse_search_source.ts | 59 ++++++------ .../search_source/serialize_search_source.ts | 93 +++++++++++++++++++ .../saved_object/helpers/apply_es_resp.ts | 14 +-- .../helpers/build_saved_object.ts | 3 +- .../helpers/hydrate_index_pattern.ts | 10 +- .../helpers/serialize_saved_object.ts | 58 ++---------- 10 files changed, 152 insertions(+), 96 deletions(-) rename src/plugins/{saved_objects/public/saved_object/helpers => data/public/search/search_source}/parse_search_source.ts (57%) create mode 100644 src/plugins/data/public/search/search_source/serialize_search_source.ts diff --git a/src/legacy/core_plugins/visualizations/public/np_ready/public/saved_visualizations/_saved_vis.ts b/src/legacy/core_plugins/visualizations/public/np_ready/public/saved_visualizations/_saved_vis.ts index e381a01edef8b..d5dfae00c26b2 100644 --- a/src/legacy/core_plugins/visualizations/public/np_ready/public/saved_visualizations/_saved_vis.ts +++ b/src/legacy/core_plugins/visualizations/public/np_ready/public/saved_visualizations/_saved_vis.ts @@ -32,12 +32,15 @@ import { // @ts-ignore import { updateOldState } from '../legacy/vis_update_state'; import { extractReferences, injectReferences } from './saved_visualization_references'; -import { IIndexPattern } from '../../../../../../../plugins/data/public'; +import { IIndexPattern, SearchSource } from '../../../../../../../plugins/data/public'; import { VisSavedObject } from '../types'; import { VisImpl } from '../vis_impl'; import { createSavedSearchesLoader } from '../../../../../../../plugins/discover/public'; async function _afterEsResp(savedVis: VisSavedObject, services: any) { + if (!savedVis.searchSource) { + savedVis.searchSource = new SearchSource(); + } await _getLinkedSavedSearch(savedVis, services); savedVis.searchSource!.setField('size', 0); savedVis.vis = savedVis.vis ? _updateVis(savedVis) : await _createVis(savedVis); diff --git a/src/plugins/data/public/index.ts b/src/plugins/data/public/index.ts index 7487f048525bd..297a3c4d7f37d 100644 --- a/src/plugins/data/public/index.ts +++ b/src/plugins/data/public/index.ts @@ -311,6 +311,8 @@ export { SearchStrategyProvider, ISearchSource, SearchSource, + parseSearchSource, + serializeSearchSource, SearchSourceFields, EsQuerySortValue, SortDirection, diff --git a/src/plugins/data/public/search/index.ts b/src/plugins/data/public/search/index.ts index 2a54cfe2be785..a4ebdb81f4e45 100644 --- a/src/plugins/data/public/search/index.ts +++ b/src/plugins/data/public/search/index.ts @@ -57,6 +57,8 @@ export { SearchSourceFields, EsQuerySortValue, SortDirection, + parseSearchSource, + serializeSearchSource, } from './search_source'; export { FetchOptions } from './fetch'; diff --git a/src/plugins/data/public/search/search_source/index.ts b/src/plugins/data/public/search/search_source/index.ts index 10f1b2bc332e1..d17b2b657d302 100644 --- a/src/plugins/data/public/search/search_source/index.ts +++ b/src/plugins/data/public/search/search_source/index.ts @@ -18,4 +18,6 @@ */ export * from './search_source'; +export { parseSearchSource } from './parse_search_source'; +export { serializeSearchSource } from './serialize_search_source'; export { SortDirection, EsQuerySortValue, SearchSourceFields } from './types'; diff --git a/src/plugins/saved_objects/public/saved_object/helpers/parse_search_source.ts b/src/plugins/data/public/search/search_source/parse_search_source.ts similarity index 57% rename from src/plugins/saved_objects/public/saved_object/helpers/parse_search_source.ts rename to src/plugins/data/public/search/search_source/parse_search_source.ts index cdb191f9e7df8..7099741744860 100644 --- a/src/plugins/saved_objects/public/saved_object/helpers/parse_search_source.ts +++ b/src/plugins/data/public/search/search_source/parse_search_source.ts @@ -17,81 +17,84 @@ * under the License. */ import _ from 'lodash'; +import { SavedObjectReference } from 'kibana/public'; import { migrateLegacyQuery } from '../../../../kibana_legacy/public'; -import { SavedObject } from '../../types'; import { InvalidJSONProperty } from '../../../../kibana_utils/public'; +import { SearchSource } from './search_source'; +import { IndexPatternsContract } from '../../index_patterns/index_patterns'; +import { SearchSourceFields } from './types'; -export function parseSearchSource( - savedObject: SavedObject, - esType: string, +export async function parseSearchSource( searchSourceJson: string, - references: any[] + references: SavedObjectReference[], + indexPatterns: IndexPatternsContract ) { - if (!savedObject.searchSource) return; + const searchSource = new SearchSource(); // if we have a searchSource, set its values based on the searchSourceJson field - let searchSourceValues: Record; + let searchSourceValues: Record; try { searchSourceValues = JSON.parse(searchSourceJson); } catch (e) { throw new InvalidJSONProperty( - `Invalid JSON in ${esType} "${savedObject.id}". ${e.message} JSON: ${searchSourceJson}` + `Invalid JSON in search source. ${e.message} JSON: ${searchSourceJson}` ); } // This detects a scenario where documents with invalid JSON properties have been imported into the saved object index. // (This happened in issue #20308) if (!searchSourceValues || typeof searchSourceValues !== 'object') { - throw new InvalidJSONProperty(`Invalid searchSourceJSON in ${esType} "${savedObject.id}".`); + throw new InvalidJSONProperty('Invalid JSON in search source.'); } // Inject index id if a reference is saved if (searchSourceValues.indexRefName) { - const reference = references.find( - (ref: Record) => ref.name === searchSourceValues.indexRefName - ); + const reference = references.find(ref => ref.name === searchSourceValues.indexRefName); if (!reference) { - throw new Error( - `Could not find reference for ${ - searchSourceValues.indexRefName - } on ${savedObject.getEsType()} ${savedObject.id}` - ); + throw new Error(`Could not find reference for ${searchSourceValues.indexRefName}`); } searchSourceValues.index = reference.id; delete searchSourceValues.indexRefName; } - if (searchSourceValues.filter) { + if (searchSourceValues.filter && Array.isArray(searchSourceValues.filter)) { searchSourceValues.filter.forEach((filterRow: any) => { if (!filterRow.meta || !filterRow.meta.indexRefName) { return; } const reference = references.find((ref: any) => ref.name === filterRow.meta.indexRefName); if (!reference) { - throw new Error( - `Could not find reference for ${ - filterRow.meta.indexRefName - } on ${savedObject.getEsType()}` - ); + throw new Error(`Could not find reference for ${filterRow.meta.indexRefName}`); } filterRow.meta.index = reference.id; delete filterRow.meta.indexRefName; }); } - const searchSourceFields = savedObject.searchSource.getFields(); + if (searchSourceValues.index && typeof searchSourceValues.index === 'string') { + const indexObj = await indexPatterns.get(searchSourceValues.index); + searchSourceValues.index = indexObj; + } + + const searchSourceFields = searchSource.getFields(); const fnProps = _.transform( searchSourceFields, - function(dynamic: Record, val: any, name: string | undefined) { + function(dynamic, val, name) { if (_.isFunction(val) && name) dynamic[name] = val; }, {} ); - savedObject.searchSource.setFields(_.defaults(searchSourceValues, fnProps)); - const query = savedObject.searchSource.getOwnField('query'); + // This assignment might hide problems because the type of values passed from the parsed JSON + // might not fit the SearchSourceFields interface. + const newFields: SearchSourceFields = _.defaults(searchSourceValues, fnProps); + + searchSource.setFields(newFields); + const query = searchSource.getOwnField('query'); if (typeof query !== 'undefined') { - savedObject.searchSource.setField('query', migrateLegacyQuery(query)); + searchSource.setField('query', migrateLegacyQuery(query)); } + + return searchSource; } diff --git a/src/plugins/data/public/search/search_source/serialize_search_source.ts b/src/plugins/data/public/search/search_source/serialize_search_source.ts new file mode 100644 index 0000000000000..8bc20f31157ad --- /dev/null +++ b/src/plugins/data/public/search/search_source/serialize_search_source.ts @@ -0,0 +1,93 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import _ from 'lodash'; +import { SavedObjectReference } from 'kibana/public'; +import { ISearchSource } from './search_source'; +import { SearchSourceFields } from './types'; +import { Filter } from '../../../common/es_query/filters'; + +function getFilters(filterField: SearchSourceFields['filter']): Filter[] { + if (!filterField) { + return []; + } + + if (Array.isArray(filterField)) { + return filterField; + } + + if (_.isFunction(filterField)) { + return getFilters(filterField()); + } + + return [filterField]; +} + +export function serializeSearchSource(searchSource: ISearchSource) { + const references: SavedObjectReference[] = []; + + const { + filter: originalFilters, + ...searchSourceFields + }: Omit = _.omit(searchSource.getFields(), ['sort', 'size']); + let serializedSearchSourceFields: Omit & { + indexRefName?: string; + filter?: Array & { meta: Filter['meta'] & { indexRefName?: string } }>; + } = searchSourceFields; + if (searchSourceFields.index) { + const indexId = searchSourceFields.index.id!; + const refName = 'kibanaSavedObjectMeta.searchSourceJSON.index'; + references.push({ + name: refName, + type: 'index-pattern', + id: indexId, + }); + serializedSearchSourceFields = { + ...searchSourceFields, + indexRefName: refName, + index: undefined, + }; + } + if (originalFilters) { + const filters = getFilters(originalFilters); + serializedSearchSourceFields = { + ...searchSourceFields, + filter: filters.map((filterRow, i) => { + if (!filterRow.meta || !filterRow.meta.index) { + return filterRow; + } + const refName = `kibanaSavedObjectMeta.searchSourceJSON.filter[${i}].meta.index`; + references.push({ + name: refName, + type: 'index-pattern', + id: filterRow.meta.index, + }); + return { + ...filterRow, + meta: { + ...filterRow.meta, + indexRefName: refName, + index: undefined, + }, + }; + }), + }; + } + + return { searchSourceJSON: JSON.stringify(serializedSearchSourceFields), references }; +} diff --git a/src/plugins/saved_objects/public/saved_object/helpers/apply_es_resp.ts b/src/plugins/saved_objects/public/saved_object/helpers/apply_es_resp.ts index 2e965eaf1989b..3f58a55f854df 100644 --- a/src/plugins/saved_objects/public/saved_object/helpers/apply_es_resp.ts +++ b/src/plugins/saved_objects/public/saved_object/helpers/apply_es_resp.ts @@ -18,9 +18,8 @@ */ import _ from 'lodash'; import { EsResponse, SavedObject, SavedObjectConfig } from '../../types'; -import { parseSearchSource } from './parse_search_source'; import { expandShorthand, SavedObjectNotFound } from '../../../../kibana_utils/public'; -import { IndexPattern } from '../../../../data/public'; +import { IndexPattern, IndexPatternsContract, parseSearchSource } from '../../../../data/public'; /** * A given response of and ElasticSearch containing a plain saved object is applied to the given @@ -29,13 +28,13 @@ import { IndexPattern } from '../../../../data/public'; export async function applyESResp( resp: EsResponse, savedObject: SavedObject, - config: SavedObjectConfig + config: SavedObjectConfig, + indexPatterns: IndexPatternsContract ) { const mapping = expandShorthand(config.mapping); const esType = config.type || ''; savedObject._source = _.cloneDeep(resp._source); const injectReferences = config.injectReferences; - const hydrateIndexPattern = savedObject.hydrateIndexPattern!; if (typeof resp.found === 'boolean' && !resp.found) { throw new SavedObjectNotFound(esType, savedObject.id || ''); } @@ -64,8 +63,11 @@ export async function applyESResp( _.assign(savedObject, savedObject._source); savedObject.lastSavedTitle = savedObject.title; - await parseSearchSource(savedObject, esType, meta.searchSourceJSON, resp.references); - await hydrateIndexPattern(); + savedObject.searchSource = await parseSearchSource( + meta.searchSourceJSON, + resp.references, + indexPatterns + ); if (injectReferences && resp.references && resp.references.length > 0) { injectReferences(savedObject, resp.references); } diff --git a/src/plugins/saved_objects/public/saved_object/helpers/build_saved_object.ts b/src/plugins/saved_objects/public/saved_object/helpers/build_saved_object.ts index b9043890e2775..9ec43056c07f0 100644 --- a/src/plugins/saved_objects/public/saved_object/helpers/build_saved_object.ts +++ b/src/plugins/saved_objects/public/saved_object/helpers/build_saved_object.ts @@ -81,7 +81,8 @@ export function buildSavedObject( */ savedObject.init = _.once(() => intializeSavedObject(savedObject, savedObjectsClient, config)); - savedObject.applyESResp = (resp: EsResponse) => applyESResp(resp, savedObject, config); + savedObject.applyESResp = (resp: EsResponse) => + applyESResp(resp, savedObject, config, indexPatterns); /** * Serialize this object diff --git a/src/plugins/saved_objects/public/saved_object/helpers/hydrate_index_pattern.ts b/src/plugins/saved_objects/public/saved_object/helpers/hydrate_index_pattern.ts index b55538e4073ba..84275cf35befb 100644 --- a/src/plugins/saved_objects/public/saved_object/helpers/hydrate_index_pattern.ts +++ b/src/plugins/saved_objects/public/saved_object/helpers/hydrate_index_pattern.ts @@ -31,25 +31,19 @@ export async function hydrateIndexPattern( indexPatterns: IndexPatternsContract, config: SavedObjectConfig ) { - const clearSavedIndexPattern = !!config.clearSavedIndexPattern; const indexPattern = config.indexPattern; if (!savedObject.searchSource) { return null; } - if (clearSavedIndexPattern) { - savedObject.searchSource!.setField('index', undefined); - return null; - } - - const index = id || indexPattern || savedObject.searchSource!.getOwnField('index'); + const index = id || indexPattern || savedObject.searchSource.getOwnField('index'); if (typeof index !== 'string' || !index) { return null; } const indexObj = await indexPatterns.get(index); - savedObject.searchSource!.setField('index', indexObj); + savedObject.searchSource.setField('index', indexObj); return indexObj; } diff --git a/src/plugins/saved_objects/public/saved_object/helpers/serialize_saved_object.ts b/src/plugins/saved_objects/public/saved_object/helpers/serialize_saved_object.ts index 8a020ca03aea3..60fdb88460c2c 100644 --- a/src/plugins/saved_objects/public/saved_object/helpers/serialize_saved_object.ts +++ b/src/plugins/saved_objects/public/saved_object/helpers/serialize_saved_object.ts @@ -17,9 +17,9 @@ * under the License. */ import _ from 'lodash'; -import angular from 'angular'; import { SavedObject, SavedObjectConfig } from '../../types'; import { expandShorthand } from '../../../../kibana_utils/public'; +import { serializeSearchSource } from '../../../../data/public'; export function serializeSavedObject(savedObject: SavedObject, config: SavedObjectConfig) { // mapping definition for the fields that this object will expose @@ -41,57 +41,11 @@ export function serializeSavedObject(savedObject: SavedObject, config: SavedObje }); if (savedObject.searchSource) { - let searchSourceFields: Record = _.omit(savedObject.searchSource.getFields(), [ - 'sort', - 'size', - ]); - if (searchSourceFields.index) { - // searchSourceFields.index will normally be an IndexPattern, but can be a string in two scenarios: - // (1) `init()` (and by extension `hydrateIndexPattern()`) hasn't been called on Saved Object - // (2) The IndexPattern doesn't exist, so we fail to resolve it in `hydrateIndexPattern()` - const indexId = - typeof searchSourceFields.index === 'string' - ? searchSourceFields.index - : searchSourceFields.index.id; - const refName = 'kibanaSavedObjectMeta.searchSourceJSON.index'; - references.push({ - name: refName, - type: 'index-pattern', - id: indexId, - }); - searchSourceFields = { - ...searchSourceFields, - indexRefName: refName, - index: undefined, - }; - } - if (searchSourceFields.filter) { - searchSourceFields = { - ...searchSourceFields, - filter: searchSourceFields.filter.map((filterRow: any, i: number) => { - if (!filterRow.meta || !filterRow.meta.index) { - return filterRow; - } - const refName = `kibanaSavedObjectMeta.searchSourceJSON.filter[${i}].meta.index`; - references.push({ - name: refName, - type: 'index-pattern', - id: filterRow.meta.index, - }); - return { - ...filterRow, - meta: { - ...filterRow.meta, - indexRefName: refName, - index: undefined, - }, - }; - }), - }; - } - attributes.kibanaSavedObjectMeta = { - searchSourceJSON: angular.toJson(searchSourceFields), - }; + const { searchSourceJSON, references: searchSourceReferences } = serializeSearchSource( + savedObject.searchSource + ); + attributes.kibanaSavedObjectMeta = { searchSourceJSON }; + references.push(...searchSourceReferences); } return { attributes, references }; From 01c1ca7d1495c2a88c61361c04aa6ea29a2f7b94 Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Wed, 11 Mar 2020 18:25:37 +0100 Subject: [PATCH 02/12] only initialize search source if it is enabled in config --- .../public/saved_object/helpers/apply_es_resp.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/plugins/saved_objects/public/saved_object/helpers/apply_es_resp.ts b/src/plugins/saved_objects/public/saved_object/helpers/apply_es_resp.ts index 3f58a55f854df..65e35edbc2e26 100644 --- a/src/plugins/saved_objects/public/saved_object/helpers/apply_es_resp.ts +++ b/src/plugins/saved_objects/public/saved_object/helpers/apply_es_resp.ts @@ -63,16 +63,21 @@ export async function applyESResp( _.assign(savedObject, savedObject._source); savedObject.lastSavedTitle = savedObject.title; - savedObject.searchSource = await parseSearchSource( - meta.searchSourceJSON, - resp.references, - indexPatterns - ); + if (config.searchSource) { + savedObject.searchSource = await parseSearchSource( + meta.searchSourceJSON, + resp.references, + indexPatterns + ); + } + if (injectReferences && resp.references && resp.references.length > 0) { injectReferences(savedObject, resp.references); } + if (typeof config.afterESResp === 'function') { savedObject = await config.afterESResp(savedObject); } + return savedObject; } From 37cd60e7f4f074a9b97998fc0c741ee9e5860c01 Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Thu, 12 Mar 2020 11:34:31 +0100 Subject: [PATCH 03/12] make functions state and add tests for parsing --- .../kibana/public/dashboard/plugin.ts | 1 + .../kibana/public/discover/build_services.ts | 1 + .../management/saved_object_registry.ts | 1 + .../timelion/public/services/saved_sheets.ts | 1 + .../public/np_ready/public/plugin.ts | 1 + src/plugins/data/public/plugin.ts | 2 +- .../data/public/search/search_service.ts | 6 +- .../search_source/parse_search_source.test.ts | 151 ++++++++++++++++++ .../search_source/parse_search_source.ts | 12 +- src/plugins/data/public/search/types.ts | 27 +++- .../saved_object/helpers/apply_es_resp.ts | 10 +- .../helpers/build_saved_object.ts | 5 +- .../helpers/serialize_saved_object.ts | 8 +- src/plugins/saved_objects/public/types.ts | 8 +- .../plugins/graph/public/application.ts | 1 + .../services/gis_map_saved_object_loader.js | 1 + .../use_search_items/use_search_items.ts | 1 + 17 files changed, 215 insertions(+), 22 deletions(-) create mode 100644 src/plugins/data/public/search/search_source/parse_search_source.test.ts diff --git a/src/legacy/core_plugins/kibana/public/dashboard/plugin.ts b/src/legacy/core_plugins/kibana/public/dashboard/plugin.ts index d94612225782d..2b355c724f3fe 100644 --- a/src/legacy/core_plugins/kibana/public/dashboard/plugin.ts +++ b/src/legacy/core_plugins/kibana/public/dashboard/plugin.ts @@ -133,6 +133,7 @@ export class DashboardPlugin implements Plugin { const savedDashboards = createSavedDashboardLoader({ savedObjectsClient, indexPatterns: dataStart.indexPatterns, + search: dataStart.search, chrome: coreStart.chrome, overlays: coreStart.overlays, }); diff --git a/src/legacy/core_plugins/kibana/public/discover/build_services.ts b/src/legacy/core_plugins/kibana/public/discover/build_services.ts index c58307adaf38c..c117f6195bb23 100644 --- a/src/legacy/core_plugins/kibana/public/discover/build_services.ts +++ b/src/legacy/core_plugins/kibana/public/discover/build_services.ts @@ -67,6 +67,7 @@ export async function buildServices( const services = { savedObjectsClient: core.savedObjects.client, indexPatterns: plugins.data.indexPatterns, + search: plugins.data.search, chrome: core.chrome, overlays: core.overlays, }; diff --git a/src/legacy/core_plugins/kibana/public/management/saved_object_registry.ts b/src/legacy/core_plugins/kibana/public/management/saved_object_registry.ts index 8e73a09480c41..973400df2291f 100644 --- a/src/legacy/core_plugins/kibana/public/management/saved_object_registry.ts +++ b/src/legacy/core_plugins/kibana/public/management/saved_object_registry.ts @@ -52,6 +52,7 @@ export const savedObjectManagementRegistry = { const services = { savedObjectsClient: npStart.core.savedObjects.client, indexPatterns: npStart.plugins.data.indexPatterns, + search: npStart.plugins.data.search, chrome: npStart.core.chrome, overlays: npStart.core.overlays, }; diff --git a/src/legacy/core_plugins/timelion/public/services/saved_sheets.ts b/src/legacy/core_plugins/timelion/public/services/saved_sheets.ts index 201b21f932988..e7f431a178ea0 100644 --- a/src/legacy/core_plugins/timelion/public/services/saved_sheets.ts +++ b/src/legacy/core_plugins/timelion/public/services/saved_sheets.ts @@ -28,6 +28,7 @@ const savedObjectsClient = npStart.core.savedObjects.client; const services = { savedObjectsClient, indexPatterns: npStart.plugins.data.indexPatterns, + search: npStart.plugins.data.search, chrome: npStart.core.chrome, overlays: npStart.core.overlays, }; diff --git a/src/legacy/core_plugins/visualizations/public/np_ready/public/plugin.ts b/src/legacy/core_plugins/visualizations/public/np_ready/public/plugin.ts index b8db611f30815..ab35fe800e35b 100644 --- a/src/legacy/core_plugins/visualizations/public/np_ready/public/plugin.ts +++ b/src/legacy/core_plugins/visualizations/public/np_ready/public/plugin.ts @@ -145,6 +145,7 @@ export class VisualizationsPlugin const savedVisualizationsLoader = createSavedVisLoader({ savedObjectsClient: core.savedObjects.client, indexPatterns: data.indexPatterns, + search: data.search, chrome: core.chrome, overlays: core.overlays, visualizationTypes: types, diff --git a/src/plugins/data/public/plugin.ts b/src/plugins/data/public/plugin.ts index a199a0419aea6..ebccbe2686afc 100644 --- a/src/plugins/data/public/plugin.ts +++ b/src/plugins/data/public/plugin.ts @@ -104,7 +104,7 @@ export class DataPublicPlugin implements Plugin { }; } - public start(core: CoreStart): ISearchStart { + public start(core: CoreStart, indexPatterns: IndexPatternsContract): ISearchStart { return { aggs: { calculateAutoTimeExpression: getCalculateAutoTimeExpression(core.uiSettings), @@ -86,6 +88,8 @@ export class SearchService implements Plugin { }); return search(request as any, options); }, + serializeSearchSource, + parseSearchSource: parseSearchSource(indexPatterns), __LEGACY: { esClient: this.esClient!, }, diff --git a/src/plugins/data/public/search/search_source/parse_search_source.test.ts b/src/plugins/data/public/search/search_source/parse_search_source.test.ts new file mode 100644 index 0000000000000..d2c9da1d40acd --- /dev/null +++ b/src/plugins/data/public/search/search_source/parse_search_source.test.ts @@ -0,0 +1,151 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { parseSearchSource as parseSearchSourceFactory } from './parse_search_source'; +import { IIndexPattern } from '../../../common/index_patterns'; +import { IndexPatternsContract } from '../../index_patterns/index_patterns'; +import { Filter } from '../../../common/es_query/filters'; + +describe('parseSearchSource', function() { + let parseSearchSource: ReturnType; + const indexPatternMock: IIndexPattern = {} as IIndexPattern; + let indexPatternContractMock: jest.Mocked; + + beforeEach(() => { + indexPatternContractMock = ({ + get: jest.fn().mockReturnValue(Promise.resolve(indexPatternMock)), + } as unknown) as jest.Mocked; + parseSearchSource = parseSearchSourceFactory(indexPatternContractMock); + }); + + it('should fail if JSON is invalid', () => { + expect(parseSearchSource('{', [])).rejects.toThrow(); + expect(parseSearchSource('0', [])).rejects.toThrow(); + expect(parseSearchSource('"abcdefg"', [])).rejects.toThrow(); + }); + + it('should set fields', async () => { + const searchSource = await parseSearchSource( + JSON.stringify({ + highlightAll: true, + query: { + query: '', + language: 'kuery', + }, + }), + [] + ); + expect(searchSource.getOwnField('highlightAll')).toBe(true); + expect(searchSource.getOwnField('query')).toEqual({ + query: '', + language: 'kuery', + }); + }); + + it('should resolve referenced index pattern', async () => { + const searchSource = await parseSearchSource( + JSON.stringify({ + indexRefName: 'kibanaSavedObjectMeta.searchSourceJSON.index', + }), + [ + { + id: '123-456', + type: 'index-pattern', + name: 'kibanaSavedObjectMeta.searchSourceJSON.index', + }, + ] + ); + expect(indexPatternContractMock.get).toHaveBeenCalledWith('123-456'); + expect(searchSource.getOwnField('index')).toBe(indexPatternMock); + }); + + it('should set filters and resolve referenced index patterns', async () => { + const searchSource = await parseSearchSource( + JSON.stringify({ + filter: [ + { + meta: { + alias: null, + negate: false, + disabled: false, + type: 'phrase', + key: 'category.keyword', + params: { + query: "Men's Clothing", + }, + indexRefName: 'kibanaSavedObjectMeta.searchSourceJSON.filter[0].meta.index', + }, + query: { + match_phrase: { + 'category.keyword': "Men's Clothing", + }, + }, + $state: { + store: 'appState', + }, + }, + ], + }), + [ + { + id: '123-456', + type: 'index-pattern', + name: 'kibanaSavedObjectMeta.searchSourceJSON.filter[0].meta.index', + }, + ] + ); + const filters = searchSource.getOwnField('filter') as Filter[]; + expect(filters[0]).toMatchInlineSnapshot(` + Object { + "$state": Object { + "store": "appState", + }, + "meta": Object { + "alias": null, + "disabled": false, + "index": "123-456", + "key": "category.keyword", + "negate": false, + "params": Object { + "query": "Men's Clothing", + }, + "type": "phrase", + }, + "query": Object { + "match_phrase": Object { + "category.keyword": "Men's Clothing", + }, + }, + } + `); + }); + + it('should migrate legacy queries on the fly', async () => { + const searchSource = await parseSearchSource( + JSON.stringify({ + highlightAll: true, + query: 'a:b', + }), + [] + ); + expect(searchSource.getOwnField('query')).toEqual({ + query: 'a:b', + language: 'lucene', + }); + }); +}); diff --git a/src/plugins/data/public/search/search_source/parse_search_source.ts b/src/plugins/data/public/search/search_source/parse_search_source.ts index 7099741744860..b2eb58fcb3ea5 100644 --- a/src/plugins/data/public/search/search_source/parse_search_source.ts +++ b/src/plugins/data/public/search/search_source/parse_search_source.ts @@ -24,11 +24,10 @@ import { SearchSource } from './search_source'; import { IndexPatternsContract } from '../../index_patterns/index_patterns'; import { SearchSourceFields } from './types'; -export async function parseSearchSource( +export const parseSearchSource = (indexPatterns: IndexPatternsContract) => async ( searchSourceJson: string, - references: SavedObjectReference[], - indexPatterns: IndexPatternsContract -) { + references: SavedObjectReference[] +) => { const searchSource = new SearchSource(); // if we have a searchSource, set its values based on the searchSourceJson field @@ -72,8 +71,7 @@ export async function parseSearchSource( } if (searchSourceValues.index && typeof searchSourceValues.index === 'string') { - const indexObj = await indexPatterns.get(searchSourceValues.index); - searchSourceValues.index = indexObj; + searchSourceValues.index = await indexPatterns.get(searchSourceValues.index); } const searchSourceFields = searchSource.getFields(); @@ -97,4 +95,4 @@ export async function parseSearchSource( } return searchSource; -} +}; diff --git a/src/plugins/data/public/search/types.ts b/src/plugins/data/public/search/types.ts index caea178212f56..52fa9be05d2a0 100644 --- a/src/plugins/data/public/search/types.ts +++ b/src/plugins/data/public/search/types.ts @@ -17,11 +17,12 @@ * under the License. */ -import { CoreStart } from 'kibana/public'; +import { CoreStart, SavedObjectReference } from 'kibana/public'; import { TimeRange } from '../../common'; import { ISearch, ISearchGeneric } from './i_search'; import { TStrategyTypes } from './strategy_types'; import { LegacyApiCaller } from './es_client'; +import { ISearchSource } from './search_source'; export interface ISearchContext { core: CoreStart; @@ -91,6 +92,30 @@ export interface ISearchSetup { export interface ISearchStart { aggs: SearchAggsStart; search: ISearchGeneric; + /** + * Serializes a `SearchSource` instance to a JSON string and a set of referenced objects. + * Use this method to get a representation of the search source which can be stored in a saved object. + * + * The references returned by this function can be mixed with other references in the same object, + * however make sure there are no name-collisions. The references will be named `kibanaSavedObjectMeta.searchSourceJSON.index` + * and `kibanaSavedObjectMeta.searchSourceJSON.filter[].meta.index`. + * + * Using `parseSearchSource`, the instance can be re-created. + * @param searchSource The search source to serialize + */ + serializeSearchSource: ( + searchSource: ISearchSource + ) => { searchSourceJSON: string; references: SavedObjectReference[] }; + /** + * Deserializes a json string and a set of referenced objects to a `SearchSource` instance. + * Use this method to re-create the search source serialized using `serializeSearchSource` + * @param searchSourceJson The json string returned by `serializeSearchSource` + * @param references A list of references including the ones returned by `serializeSearchSource` + */ + parseSearchSource: ( + searchSourceJson: string, + references: SavedObjectReference[] + ) => Promise; __LEGACY: { esClient: LegacyApiCaller; }; diff --git a/src/plugins/saved_objects/public/saved_object/helpers/apply_es_resp.ts b/src/plugins/saved_objects/public/saved_object/helpers/apply_es_resp.ts index 65e35edbc2e26..574b0f7eda324 100644 --- a/src/plugins/saved_objects/public/saved_object/helpers/apply_es_resp.ts +++ b/src/plugins/saved_objects/public/saved_object/helpers/apply_es_resp.ts @@ -19,7 +19,7 @@ import _ from 'lodash'; import { EsResponse, SavedObject, SavedObjectConfig } from '../../types'; import { expandShorthand, SavedObjectNotFound } from '../../../../kibana_utils/public'; -import { IndexPattern, IndexPatternsContract, parseSearchSource } from '../../../../data/public'; +import { DataPublicPluginStart, IndexPattern } from '../../../../data/public'; /** * A given response of and ElasticSearch containing a plain saved object is applied to the given @@ -29,7 +29,7 @@ export async function applyESResp( resp: EsResponse, savedObject: SavedObject, config: SavedObjectConfig, - indexPatterns: IndexPatternsContract + parseSearchSource: DataPublicPluginStart['search']['parseSearchSource'] ) { const mapping = expandShorthand(config.mapping); const esType = config.type || ''; @@ -64,11 +64,7 @@ export async function applyESResp( savedObject.lastSavedTitle = savedObject.title; if (config.searchSource) { - savedObject.searchSource = await parseSearchSource( - meta.searchSourceJSON, - resp.references, - indexPatterns - ); + savedObject.searchSource = await parseSearchSource(meta.searchSourceJSON, resp.references); } if (injectReferences && resp.references && resp.references.length > 0) { diff --git a/src/plugins/saved_objects/public/saved_object/helpers/build_saved_object.ts b/src/plugins/saved_objects/public/saved_object/helpers/build_saved_object.ts index 9ec43056c07f0..64bd6aa3b1c1a 100644 --- a/src/plugins/saved_objects/public/saved_object/helpers/build_saved_object.ts +++ b/src/plugins/saved_objects/public/saved_object/helpers/build_saved_object.ts @@ -82,13 +82,14 @@ export function buildSavedObject( savedObject.init = _.once(() => intializeSavedObject(savedObject, savedObjectsClient, config)); savedObject.applyESResp = (resp: EsResponse) => - applyESResp(resp, savedObject, config, indexPatterns); + applyESResp(resp, savedObject, config, services.search.parseSearchSource); /** * Serialize this object * @return {Object} */ - savedObject._serialize = () => serializeSavedObject(savedObject, config); + savedObject._serialize = () => + serializeSavedObject(savedObject, config, services.search.serializeSearchSource); /** * Returns true if the object's original title has been changed. New objects return false. diff --git a/src/plugins/saved_objects/public/saved_object/helpers/serialize_saved_object.ts b/src/plugins/saved_objects/public/saved_object/helpers/serialize_saved_object.ts index 60fdb88460c2c..7517613e4af8d 100644 --- a/src/plugins/saved_objects/public/saved_object/helpers/serialize_saved_object.ts +++ b/src/plugins/saved_objects/public/saved_object/helpers/serialize_saved_object.ts @@ -19,9 +19,13 @@ import _ from 'lodash'; import { SavedObject, SavedObjectConfig } from '../../types'; import { expandShorthand } from '../../../../kibana_utils/public'; -import { serializeSearchSource } from '../../../../data/public'; +import { DataPublicPluginStart } from '../../../../data/public'; -export function serializeSavedObject(savedObject: SavedObject, config: SavedObjectConfig) { +export function serializeSavedObject( + savedObject: SavedObject, + config: SavedObjectConfig, + serializeSearchSource: DataPublicPluginStart['search']['serializeSearchSource'] +) { // mapping definition for the fields that this object will expose const mapping = expandShorthand(config.mapping); const attributes = {} as Record; diff --git a/src/plugins/saved_objects/public/types.ts b/src/plugins/saved_objects/public/types.ts index 99088df84ec36..5b550042e4fd0 100644 --- a/src/plugins/saved_objects/public/types.ts +++ b/src/plugins/saved_objects/public/types.ts @@ -24,7 +24,12 @@ import { SavedObjectAttributes, SavedObjectReference, } from 'kibana/public'; -import { IIndexPattern, IndexPatternsContract, ISearchSource } from '../../data/public'; +import { + DataPublicPluginStart, + IIndexPattern, + IndexPatternsContract, + ISearchSource, +} from '../../data/public'; export interface SavedObject { _serialize: () => { attributes: SavedObjectAttributes; references: SavedObjectReference[] }; @@ -65,6 +70,7 @@ export interface SavedObjectCreationOpts { export interface SavedObjectKibanaServices { savedObjectsClient: SavedObjectsClientContract; indexPatterns: IndexPatternsContract; + search: DataPublicPluginStart['search']; chrome: ChromeStart; overlays: OverlayStart; } diff --git a/x-pack/legacy/plugins/graph/public/application.ts b/x-pack/legacy/plugins/graph/public/application.ts index 536382e62d473..3a954b734a67f 100644 --- a/x-pack/legacy/plugins/graph/public/application.ts +++ b/x-pack/legacy/plugins/graph/public/application.ts @@ -82,6 +82,7 @@ export const renderApp = ({ appBasePath, element, ...deps }: GraphDependencies) const savedWorkspaceLoader = createSavedWorkspacesLoader({ chrome: deps.coreStart.chrome, indexPatterns: deps.npData.indexPatterns, + search: deps.npData.search, overlays: deps.coreStart.overlays, savedObjectsClient: deps.coreStart.savedObjects.client, basePath: deps.coreStart.http.basePath, diff --git a/x-pack/legacy/plugins/maps/public/angular/services/gis_map_saved_object_loader.js b/x-pack/legacy/plugins/maps/public/angular/services/gis_map_saved_object_loader.js index 252d602e8f564..bc636c0b200f8 100644 --- a/x-pack/legacy/plugins/maps/public/angular/services/gis_map_saved_object_loader.js +++ b/x-pack/legacy/plugins/maps/public/angular/services/gis_map_saved_object_loader.js @@ -17,6 +17,7 @@ module.service('gisMapSavedObjectLoader', function() { const services = { savedObjectsClient, indexPatterns: npStart.plugins.data.indexPatterns, + search: npStart.plugins.data.search, chrome: npStart.core.chrome, overlays: npStart.core.overlays, }; diff --git a/x-pack/plugins/transform/public/app/hooks/use_search_items/use_search_items.ts b/x-pack/plugins/transform/public/app/hooks/use_search_items/use_search_items.ts index f5f9e98fe659c..feff17b813112 100644 --- a/x-pack/plugins/transform/public/app/hooks/use_search_items/use_search_items.ts +++ b/x-pack/plugins/transform/public/app/hooks/use_search_items/use_search_items.ts @@ -29,6 +29,7 @@ export const useSearchItems = (defaultSavedObjectId: string | undefined) => { const savedSearches = createSavedSearchesLoader({ savedObjectsClient, indexPatterns, + search: appDeps.data.search, chrome: appDeps.chrome, overlays: appDeps.overlays, }); From 4f9cc2e7e8427e334a53569b3deeceb54df1aae0 Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Fri, 27 Mar 2020 17:33:55 +0100 Subject: [PATCH 04/12] fix referenecs --- src/plugins/dashboard/public/plugin.tsx | 3 ++- .../dashboard/public/saved_dashboards/saved_dashboards.ts | 3 ++- src/plugins/saved_objects/public/plugin.ts | 1 + src/plugins/visualizations/public/plugin.ts | 3 +++ .../visualizations/public/saved_visualizations/_saved_vis.ts | 3 ++- src/plugins/visualizations/public/services.ts | 2 ++ 6 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/plugins/dashboard/public/plugin.tsx b/src/plugins/dashboard/public/plugin.tsx index df3c312c7ae1b..81e15ec8b003d 100644 --- a/src/plugins/dashboard/public/plugin.tsx +++ b/src/plugins/dashboard/public/plugin.tsx @@ -134,7 +134,7 @@ export class DashboardEmbeddableContainerPublicPlugin const { notifications } = core; const { uiActions, - data: { indexPatterns }, + data: { indexPatterns, search }, } = plugins; const SavedObjectFinder = getSavedObjectFinder(core.savedObjects, core.uiSettings); @@ -150,6 +150,7 @@ export class DashboardEmbeddableContainerPublicPlugin const savedDashboardLoader = createSavedDashboardLoader({ savedObjectsClient: core.savedObjects.client, indexPatterns, + search, chrome: core.chrome, overlays: core.overlays, }); diff --git a/src/plugins/dashboard/public/saved_dashboards/saved_dashboards.ts b/src/plugins/dashboard/public/saved_dashboards/saved_dashboards.ts index 2a1e64fa88a02..09357072a13a6 100644 --- a/src/plugins/dashboard/public/saved_dashboards/saved_dashboards.ts +++ b/src/plugins/dashboard/public/saved_dashboards/saved_dashboards.ts @@ -18,13 +18,14 @@ */ import { SavedObjectsClientContract, ChromeStart, OverlayStart } from 'kibana/public'; -import { IndexPatternsContract } from '../../../../plugins/data/public'; +import { DataPublicPluginStart, IndexPatternsContract } from '../../../../plugins/data/public'; import { SavedObjectLoader } from '../../../../plugins/saved_objects/public'; import { createSavedDashboardClass } from './saved_dashboard'; interface Services { savedObjectsClient: SavedObjectsClientContract; indexPatterns: IndexPatternsContract; + search: DataPublicPluginStart['search']; chrome: ChromeStart; overlays: OverlayStart; } diff --git a/src/plugins/saved_objects/public/plugin.ts b/src/plugins/saved_objects/public/plugin.ts index 0f5773c00283e..7927238e12066 100644 --- a/src/plugins/saved_objects/public/plugin.ts +++ b/src/plugins/saved_objects/public/plugin.ts @@ -39,6 +39,7 @@ export class SavedObjectsPublicPlugin SavedObjectClass: createSavedObjectClass({ indexPatterns: data.indexPatterns, savedObjectsClient: core.savedObjects.client, + search: data.search, chrome: core.chrome, overlays: core.overlays, }), diff --git a/src/plugins/visualizations/public/plugin.ts b/src/plugins/visualizations/public/plugin.ts index d3e7b759a4416..ff1da3c930548 100644 --- a/src/plugins/visualizations/public/plugin.ts +++ b/src/plugins/visualizations/public/plugin.ts @@ -26,6 +26,7 @@ import { setCapabilities, setHttp, setIndexPatterns, + setSearch, setSavedObjects, setUsageCollector, setFilterManager, @@ -135,6 +136,7 @@ export class VisualizationsPlugin setHttp(core.http); setSavedObjects(core.savedObjects); setIndexPatterns(data.indexPatterns); + setSearch(data.search); setFilterManager(data.query.filterManager); setExpressions(expressions); setUiActions(uiActions); @@ -145,6 +147,7 @@ export class VisualizationsPlugin const savedVisualizationsLoader = createSavedVisLoader({ savedObjectsClient: core.savedObjects.client, indexPatterns: data.indexPatterns, + search: data.search, chrome: core.chrome, overlays: core.overlays, visualizationTypes: types, diff --git a/src/plugins/visualizations/public/saved_visualizations/_saved_vis.ts b/src/plugins/visualizations/public/saved_visualizations/_saved_vis.ts index bc96e08f4b9da..c99c7a4c2caa1 100644 --- a/src/plugins/visualizations/public/saved_visualizations/_saved_vis.ts +++ b/src/plugins/visualizations/public/saved_visualizations/_saved_vis.ts @@ -35,7 +35,7 @@ import { extractReferences, injectReferences } from './saved_visualization_refer import { IIndexPattern, ISearchSource, SearchSource } from '../../../../plugins/data/public'; import { ISavedVis, SerializedVis } from '../types'; import { createSavedSearchesLoader } from '../../../../plugins/discover/public'; -import { getChrome, getOverlays, getIndexPatterns, getSavedObjects } from '../services'; +import { getChrome, getOverlays, getIndexPatterns, getSavedObjects, getSearch } from '../services'; export const convertToSerializedVis = async (savedVis: ISavedVis): Promise => { const { visState } = savedVis; @@ -87,6 +87,7 @@ const getSearchSource = async (inputSearchSource: ISearchSource, savedSearchId?: const savedSearch = await createSavedSearchesLoader({ savedObjectsClient: getSavedObjects().client, indexPatterns: getIndexPatterns(), + search: getSearch(), chrome: getChrome(), overlays: getOverlays(), }).get(savedSearchId); diff --git a/src/plugins/visualizations/public/services.ts b/src/plugins/visualizations/public/services.ts index c4668fa4b0c79..618c61dff176a 100644 --- a/src/plugins/visualizations/public/services.ts +++ b/src/plugins/visualizations/public/services.ts @@ -63,6 +63,8 @@ export const [getIndexPatterns, setIndexPatterns] = createGetterSetter('Search'); + export const [getUsageCollector, setUsageCollector] = createGetterSetter( 'UsageCollection' ); From 72f969d4c6892037ce4636fd7c998d4c5341ed6c Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Fri, 27 Mar 2020 18:15:45 +0100 Subject: [PATCH 05/12] add tests and fix bugs --- .../serialize_search_source.test.ts | 104 ++++++++++++++++++ .../search_source/serialize_search_source.ts | 4 +- 2 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 src/plugins/data/public/search/search_source/serialize_search_source.test.ts diff --git a/src/plugins/data/public/search/search_source/serialize_search_source.test.ts b/src/plugins/data/public/search/search_source/serialize_search_source.test.ts new file mode 100644 index 0000000000000..398da62f81a02 --- /dev/null +++ b/src/plugins/data/public/search/search_source/serialize_search_source.test.ts @@ -0,0 +1,104 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { IndexPattern } from '../../index_patterns/index_patterns'; +import { SearchSource } from './search_source'; +import { serializeSearchSource } from './serialize_search_source'; +import { SortDirection } from './types'; + +describe('serializeSearchSource', function() { + // const indexPatternMock: IIndexPattern = {} as IIndexPattern; + // let indexPatternContractMock: jest.Mocked; + + beforeEach(() => { + // indexPatternContractMock = ({ + // get: jest.fn().mockReturnValue(Promise.resolve(indexPatternMock)), + // } as unknown) as jest.Mocked; + }); + + it('should reference index patterns', () => { + const indexPattern = { id: '123' } as IndexPattern; + const searchSource = new SearchSource(); + searchSource.setField('index', indexPattern); + const { searchSourceJSON, references } = serializeSearchSource(searchSource); + expect(references[0].id).toEqual('123'); + expect(references[0].type).toEqual('index-pattern'); + expect(JSON.parse(searchSourceJSON).indexRefName).toEqual(references[0].name); + }); + + it('should add other fields', () => { + const searchSource = new SearchSource(); + searchSource.setField('highlightAll', true); + searchSource.setField('from', 123456); + const { searchSourceJSON } = serializeSearchSource(searchSource); + expect(JSON.parse(searchSourceJSON).highlightAll).toEqual(true); + expect(JSON.parse(searchSourceJSON).from).toEqual(123456); + }); + + it('should omit sort and size', () => { + const searchSource = new SearchSource(); + searchSource.setField('highlightAll', true); + searchSource.setField('from', 123456); + searchSource.setField('sort', { field: SortDirection.asc }); + searchSource.setField('size', 200); + const { searchSourceJSON } = serializeSearchSource(searchSource); + expect(Object.keys(JSON.parse(searchSourceJSON))).toEqual(['highlightAll', 'from']); + }); + + it('should serialize filters', () => { + const searchSource = new SearchSource(); + const filter = [ + { + query: 'query', + meta: { + alias: 'alias', + disabled: false, + negate: false, + }, + }, + ]; + searchSource.setField('filter', filter); + const { searchSourceJSON } = serializeSearchSource(searchSource); + expect(JSON.parse(searchSourceJSON).filter).toEqual(filter); + }); + + it('should reference index patterns in filters separately from index field', () => { + const searchSource = new SearchSource(); + const indexPattern = { id: '123' } as IndexPattern; + searchSource.setField('index', indexPattern); + const filter = [ + { + query: 'query', + meta: { + alias: 'alias', + disabled: false, + negate: false, + index: '456', + }, + }, + ]; + searchSource.setField('filter', filter); + const { searchSourceJSON, references } = serializeSearchSource(searchSource); + expect(references[0].id).toEqual('123'); + expect(references[0].type).toEqual('index-pattern'); + expect(JSON.parse(searchSourceJSON).indexRefName).toEqual(references[0].name); + expect(references[1].id).toEqual('456'); + expect(references[1].type).toEqual('index-pattern'); + expect(JSON.parse(searchSourceJSON).filter[0].meta.indexRefName).toEqual(references[1].name); + }); +}); diff --git a/src/plugins/data/public/search/search_source/serialize_search_source.ts b/src/plugins/data/public/search/search_source/serialize_search_source.ts index 8bc20f31157ad..4d69bbbb85ba3 100644 --- a/src/plugins/data/public/search/search_source/serialize_search_source.ts +++ b/src/plugins/data/public/search/search_source/serialize_search_source.ts @@ -58,7 +58,7 @@ export function serializeSearchSource(searchSource: ISearchSource) { id: indexId, }); serializedSearchSourceFields = { - ...searchSourceFields, + ...serializedSearchSourceFields, indexRefName: refName, index: undefined, }; @@ -66,7 +66,7 @@ export function serializeSearchSource(searchSource: ISearchSource) { if (originalFilters) { const filters = getFilters(originalFilters); serializedSearchSourceFields = { - ...searchSourceFields, + ...serializedSearchSourceFields, filter: filters.map((filterRow, i) => { if (!filterRow.meta || !filterRow.meta.index) { return filterRow; From e07546a8e1082608947f91404a465eda3e14c9b0 Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Sun, 29 Mar 2020 12:20:52 +0200 Subject: [PATCH 06/12] fix some problems --- .../core_plugins/kibana/public/visualize/np_ready/legacy_app.js | 1 + src/legacy/ui/public/new_platform/set_services.ts | 1 + src/plugins/data/public/search/types.ts | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/legacy/core_plugins/kibana/public/visualize/np_ready/legacy_app.js b/src/legacy/core_plugins/kibana/public/visualize/np_ready/legacy_app.js index d1bf4411cac2a..5aae08d49b4fa 100644 --- a/src/legacy/core_plugins/kibana/public/visualize/np_ready/legacy_app.js +++ b/src/legacy/core_plugins/kibana/public/visualize/np_ready/legacy_app.js @@ -71,6 +71,7 @@ const getResolvedResults = deps => { return createSavedSearchesLoader({ savedObjectsClient: core.savedObjects.client, indexPatterns: data.indexPatterns, + search: data.search, chrome: core.chrome, overlays: core.overlays, }).get(results.vis.data.savedSearchId); diff --git a/src/legacy/ui/public/new_platform/set_services.ts b/src/legacy/ui/public/new_platform/set_services.ts index 8cf015d5dff5c..e54a4c9bd6af9 100644 --- a/src/legacy/ui/public/new_platform/set_services.ts +++ b/src/legacy/ui/public/new_platform/set_services.ts @@ -75,6 +75,7 @@ export function setStartServices(npStart: NpStart) { const savedVisualizationsLoader = createSavedVisLoader({ savedObjectsClient: npStart.core.savedObjects.client, indexPatterns: npStart.plugins.data.indexPatterns, + search: npStart.plugins.data.search, chrome: npStart.core.chrome, overlays: npStart.core.overlays, visualizationTypes: visualizationsServices.getTypes(), diff --git a/src/plugins/data/public/search/types.ts b/src/plugins/data/public/search/types.ts index 7d91c1789a029..c30e5c92ee7c5 100644 --- a/src/plugins/data/public/search/types.ts +++ b/src/plugins/data/public/search/types.ts @@ -17,8 +17,8 @@ * under the License. */ -import { ISearchSource } from './search_source'; import { CoreStart, SavedObjectReference } from 'kibana/public'; +import { ISearchSource } from './search_source'; import { SearchAggsSetup, SearchAggsStart, SearchAggsStartLegacy } from './aggs'; import { ISearch, ISearchGeneric } from './i_search'; import { TStrategyTypes } from './strategy_types'; From aa57ebca481f97bdaec76528cb7214ed22499cd9 Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Sun, 29 Mar 2020 18:32:18 +0200 Subject: [PATCH 07/12] fix legacy imporrts --- .../objects_table/components/flyout/flyout.js | 3 +- .../objects/lib/resolve_saved_objects.ts | 59 +++++++++++++------ .../saved_object/helpers/apply_es_resp.ts | 19 +++++- .../helpers/serialize_saved_object.ts | 4 ++ src/plugins/saved_objects/public/types.ts | 1 + 5 files changed, 65 insertions(+), 21 deletions(-) diff --git a/src/legacy/core_plugins/kibana/public/management/sections/objects/components/objects_table/components/flyout/flyout.js b/src/legacy/core_plugins/kibana/public/management/sections/objects/components/objects_table/components/flyout/flyout.js index 105c279218375..da2221bb54203 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/objects/components/objects_table/components/flyout/flyout.js +++ b/src/legacy/core_plugins/kibana/public/management/sections/objects/components/objects_table/components/flyout/flyout.js @@ -358,7 +358,8 @@ export class Flyout extends Component { importCount += await resolveIndexPatternConflicts( resolutions, conflictedIndexPatterns, - isOverwriteAllChecked + isOverwriteAllChecked, + this.props.indexPatterns ); } this.setState({ diff --git a/src/legacy/core_plugins/kibana/public/management/sections/objects/lib/resolve_saved_objects.ts b/src/legacy/core_plugins/kibana/public/management/sections/objects/lib/resolve_saved_objects.ts index 902de654f5f85..19e0e4980b084 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/objects/lib/resolve_saved_objects.ts +++ b/src/legacy/core_plugins/kibana/public/management/sections/objects/lib/resolve_saved_objects.ts @@ -18,12 +18,17 @@ */ import { i18n } from '@kbn/i18n'; -import { OverlayStart } from 'src/core/public'; +import { cloneDeep } from 'lodash'; +import { OverlayStart, SavedObjectReference } from 'src/core/public'; import { SavedObject, SavedObjectLoader, } from '../../../../../../../../plugins/saved_objects/public'; -import { IndexPatternsContract, IIndexPattern } from '../../../../../../../../plugins/data/public'; +import { + IndexPatternsContract, + IIndexPattern, + parseSearchSource, +} from '../../../../../../../../plugins/data/public'; type SavedObjectsRawDoc = Record; @@ -126,7 +131,7 @@ async function importIndexPattern( async function importDocument(obj: SavedObject, doc: SavedObjectsRawDoc, overwriteAll: boolean) { await obj.applyESResp({ references: doc._references || [], - ...doc, + ...cloneDeep(doc), }); return await obj.save({ confirmOverwrite: !overwriteAll }); } @@ -160,41 +165,57 @@ async function awaitEachItemInParallel(list: T[], op: (item: T) => R) { export async function resolveIndexPatternConflicts( resolutions: Array<{ oldId: string; newId: string }>, conflictedIndexPatterns: any[], - overwriteAll: boolean + overwriteAll: boolean, + indexPatterns: IndexPatternsContract ) { let importCount = 0; - await awaitEachItemInParallel(conflictedIndexPatterns, async ({ obj }) => { - // Resolve search index reference: - let oldIndexId = obj.searchSource.getOwnField('index'); - // Depending on the object, this can either be the raw id or the actual index pattern object - if (typeof oldIndexId !== 'string') { - oldIndexId = oldIndexId.id; - } - let resolution = resolutions.find(({ oldId }) => oldId === oldIndexId); - if (resolution) { - const newIndexId = resolution.newId; - await obj.hydrateIndexPattern(newIndexId); + await awaitEachItemInParallel(conflictedIndexPatterns, async ({ obj, doc }) => { + const serializedSearchSource = JSON.parse( + doc._source.kibanaSavedObjectMeta?.searchSourceJSON || '{}' + ); + const oldIndexId = serializedSearchSource.index; + let allResolved = true; + const inlineResolution = resolutions.find(({ oldId }) => oldId === oldIndexId); + if (inlineResolution) { + serializedSearchSource.index = inlineResolution.newId; + } else { + allResolved = false; } // Resolve filter index reference: - const filter = (obj.searchSource.getOwnField('filter') || []).map((f: any) => { + const filter = (serializedSearchSource.filter || []).map((f: any) => { if (!(f.meta && f.meta.index)) { return f; } - resolution = resolutions.find(({ oldId }) => oldId === f.meta.index); + const resolution = resolutions.find(({ oldId }) => oldId === f.meta.index); return resolution ? { ...f, ...{ meta: { ...f.meta, index: resolution.newId } } } : f; }); if (filter.length > 0) { - obj.searchSource.setField('filter', filter); + serializedSearchSource.filter = filter; } - if (!resolution) { + const replacedReferences = (doc._references || []).map((reference: SavedObjectReference) => { + const resolution = resolutions.find(({ oldId }) => oldId === reference.id); + if (resolution) { + return { ...reference, id: resolution.newId }; + } else { + allResolved = false; + } + + return reference; + }); + + if (!allResolved) { // The user decided to skip this conflict so do nothing return; } + obj.searchSource = await parseSearchSource(indexPatterns)( + JSON.stringify(serializedSearchSource), + replacedReferences + ); if (await saveObject(obj, overwriteAll)) { importCount++; } diff --git a/src/plugins/saved_objects/public/saved_object/helpers/apply_es_resp.ts b/src/plugins/saved_objects/public/saved_object/helpers/apply_es_resp.ts index 574b0f7eda324..9b0476dd63299 100644 --- a/src/plugins/saved_objects/public/saved_object/helpers/apply_es_resp.ts +++ b/src/plugins/saved_objects/public/saved_object/helpers/apply_es_resp.ts @@ -64,7 +64,24 @@ export async function applyESResp( savedObject.lastSavedTitle = savedObject.title; if (config.searchSource) { - savedObject.searchSource = await parseSearchSource(meta.searchSourceJSON, resp.references); + try { + savedObject.searchSource = await parseSearchSource(meta.searchSourceJSON, resp.references); + } catch (error) { + if ( + error.constructor.name === 'SavedObjectNotFound' && + error.savedObjectType === 'index-pattern' + ) { + // if parsing the search source fails because the index pattern wasn't found, + // remember the reference - this is required for error handling on legacy imports + savedObject.unresolvedIndexPatternReference = { + name: 'kibanaSavedObjectMeta.searchSourceJSON.index', + id: JSON.parse(meta.searchSourceJSON).index, + type: 'index-pattern', + }; + } + + throw error; + } } if (injectReferences && resp.references && resp.references.length > 0) { diff --git a/src/plugins/saved_objects/public/saved_object/helpers/serialize_saved_object.ts b/src/plugins/saved_objects/public/saved_object/helpers/serialize_saved_object.ts index 7517613e4af8d..72b1130460506 100644 --- a/src/plugins/saved_objects/public/saved_object/helpers/serialize_saved_object.ts +++ b/src/plugins/saved_objects/public/saved_object/helpers/serialize_saved_object.ts @@ -52,5 +52,9 @@ export function serializeSavedObject( references.push(...searchSourceReferences); } + if (savedObject.unresolvedIndexPatternReference) { + references.push(savedObject.unresolvedIndexPatternReference); + } + return { attributes, references }; } diff --git a/src/plugins/saved_objects/public/types.ts b/src/plugins/saved_objects/public/types.ts index 5b550042e4fd0..b5a0b68ea2919 100644 --- a/src/plugins/saved_objects/public/types.ts +++ b/src/plugins/saved_objects/public/types.ts @@ -54,6 +54,7 @@ export interface SavedObject { searchSource?: ISearchSource; showInRecentlyAccessed: boolean; title: string; + unresolvedIndexPatternReference?: SavedObjectReference; } export interface SavedObjectSaveOpts { From 3360b1283afd069e129f684a68071baec7ac1450 Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Mon, 30 Mar 2020 08:42:35 +0200 Subject: [PATCH 08/12] finalize --- .../kibana-plugin-plugins-data-public.md | 2 + ...n-plugins-data-public.parsesearchsource.md | 13 +++ ...ugins-data-public.serializesearchsource.md | 34 ++++++++ .../objects/lib/resolve_saved_objects.test.ts | 87 +++++++++++-------- src/plugins/data/public/public.api.md | 40 +++++---- src/plugins/data/public/search/mocks.ts | 2 + .../search_source/parse_search_source.ts | 15 ++++ .../search_source/serialize_search_source.ts | 11 +++ src/plugins/data/public/search/types.ts | 30 +------ 9 files changed, 156 insertions(+), 78 deletions(-) create mode 100644 docs/development/plugins/data/public/kibana-plugin-plugins-data-public.parsesearchsource.md create mode 100644 docs/development/plugins/data/public/kibana-plugin-plugins-data-public.serializesearchsource.md diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md index 6964c070097c5..30f83c32bac28 100644 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md @@ -45,6 +45,7 @@ | [getSearchErrorType({ message })](./kibana-plugin-plugins-data-public.getsearcherrortype.md) | | | [getTime(indexPattern, timeRange, forceNow)](./kibana-plugin-plugins-data-public.gettime.md) | | | [plugin(initializerContext)](./kibana-plugin-plugins-data-public.plugin.md) | | +| [serializeSearchSource(searchSource)](./kibana-plugin-plugins-data-public.serializesearchsource.md) | Serializes a SearchSource instance to a JSON string and a set of referenced objects. Use this method to get a representation of the search source which can be stored in a saved object.The references returned by this function can be mixed with other references in the same object, however make sure there are no name-collisions. The references will be named kibanaSavedObjectMeta.searchSourceJSON.index and kibanaSavedObjectMeta.searchSourceJSON.filter[<number>].meta.index.Using parseSearchSource, the instance can be re-created. | ## Interfaces @@ -110,6 +111,7 @@ | [FilterBar](./kibana-plugin-plugins-data-public.filterbar.md) | | | [getKbnTypeNames](./kibana-plugin-plugins-data-public.getkbntypenames.md) | Get the esTypes known by all kbnFieldTypes {Array} | | [indexPatterns](./kibana-plugin-plugins-data-public.indexpatterns.md) | | +| [parseSearchSource](./kibana-plugin-plugins-data-public.parsesearchsource.md) | Deserializes a json string and a set of referenced objects to a SearchSource instance. Use this method to re-create the search source serialized using serializeSearchSource | | [QueryStringInput](./kibana-plugin-plugins-data-public.querystringinput.md) | | | [search](./kibana-plugin-plugins-data-public.search.md) | | | [SearchBar](./kibana-plugin-plugins-data-public.searchbar.md) | | diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.parsesearchsource.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.parsesearchsource.md new file mode 100644 index 0000000000000..d8958c6ebdc34 --- /dev/null +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.parsesearchsource.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) > [parseSearchSource](./kibana-plugin-plugins-data-public.parsesearchsource.md) + +## parseSearchSource variable + +Deserializes a json string and a set of referenced objects to a `SearchSource` instance. Use this method to re-create the search source serialized using `serializeSearchSource` + +Signature: + +```typescript +parseSearchSource: (indexPatterns: Pick) => (searchSourceJson: string, references: SavedObjectReference[]) => Promise +``` diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.serializesearchsource.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.serializesearchsource.md new file mode 100644 index 0000000000000..0798b0b4e9ad9 --- /dev/null +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.serializesearchsource.md @@ -0,0 +1,34 @@ + + +[Home](./index.md) > [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) > [serializeSearchSource](./kibana-plugin-plugins-data-public.serializesearchsource.md) + +## serializeSearchSource() function + +Serializes a `SearchSource` instance to a JSON string and a set of referenced objects. Use this method to get a representation of the search source which can be stored in a saved object. + +The references returned by this function can be mixed with other references in the same object, however make sure there are no name-collisions. The references will be named `kibanaSavedObjectMeta.searchSourceJSON.index` and `kibanaSavedObjectMeta.searchSourceJSON.filter[].meta.index`. + +Using `parseSearchSource`, the instance can be re-created. + +Signature: + +```typescript +export declare function serializeSearchSource(searchSource: ISearchSource): { + searchSourceJSON: string; + references: SavedObjectReference[]; +}; +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| searchSource | ISearchSource | | + +Returns: + +`{ + searchSourceJSON: string; + references: SavedObjectReference[]; +}` + diff --git a/src/legacy/core_plugins/kibana/public/management/sections/objects/lib/resolve_saved_objects.test.ts b/src/legacy/core_plugins/kibana/public/management/sections/objects/lib/resolve_saved_objects.test.ts index 8243aa69ac082..dc6d2643145ff 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/objects/lib/resolve_saved_objects.test.ts +++ b/src/legacy/core_plugins/kibana/public/management/sections/objects/lib/resolve_saved_objects.test.ts @@ -84,7 +84,7 @@ describe('resolveSavedObjects', () => { }, } as unknown) as IndexPatternsContract; - const services = [ + const services = ([ { type: 'search', get: async () => { @@ -124,7 +124,7 @@ describe('resolveSavedObjects', () => { }; }, }, - ] as SavedObjectLoader[]; + ] as unknown) as SavedObjectLoader[]; const overwriteAll = false; @@ -176,7 +176,7 @@ describe('resolveSavedObjects', () => { }, } as unknown) as IndexPatternsContract; - const services = [ + const services = ([ { type: 'search', get: async () => { @@ -217,7 +217,7 @@ describe('resolveSavedObjects', () => { }; }, }, - ] as SavedObjectLoader[]; + ] as unknown) as SavedObjectLoader[]; const overwriteAll = false; @@ -237,33 +237,38 @@ describe('resolveSavedObjects', () => { describe('resolveIndexPatternConflicts', () => { it('should resave resolutions', async () => { - const hydrateIndexPattern = jest.fn(); const save = jest.fn(); - const conflictedIndexPatterns = [ + const conflictedIndexPatterns = ([ { obj: { - searchSource: { - getOwnField: (field: string) => { - return field === 'index' ? '1' : undefined; + save, + }, + doc: { + _source: { + kibanaSavedObjectMeta: { + searchSourceJSON: JSON.stringify({ + index: '1', + }), }, }, - hydrateIndexPattern, - save, }, }, { obj: { - searchSource: { - getOwnField: (field: string) => { - return field === 'index' ? '3' : undefined; + save, + }, + doc: { + _source: { + kibanaSavedObjectMeta: { + searchSourceJSON: JSON.stringify({ + index: '3', + }), }, }, - hydrateIndexPattern, - save, }, }, - ]; + ] as unknown) as Array<{ obj: SavedObject; doc: any }>; const resolutions = [ { @@ -282,43 +287,49 @@ describe('resolveSavedObjects', () => { const overwriteAll = false; - await resolveIndexPatternConflicts(resolutions, conflictedIndexPatterns, overwriteAll); - expect(hydrateIndexPattern.mock.calls.length).toBe(2); + await resolveIndexPatternConflicts(resolutions, conflictedIndexPatterns, overwriteAll, ({ + get: (id: string) => Promise.resolve({ id }), + } as unknown) as IndexPatternsContract); + expect(conflictedIndexPatterns[0].obj.searchSource!.getField('index')!.id).toEqual('2'); + expect(conflictedIndexPatterns[1].obj.searchSource!.getField('index')!.id).toEqual('4'); expect(save.mock.calls.length).toBe(2); expect(save).toHaveBeenCalledWith({ confirmOverwrite: !overwriteAll }); - expect(hydrateIndexPattern).toHaveBeenCalledWith('2'); - expect(hydrateIndexPattern).toHaveBeenCalledWith('4'); }); it('should resolve filter index conflicts', async () => { - const hydrateIndexPattern = jest.fn(); const save = jest.fn(); - const conflictedIndexPatterns = [ + const conflictedIndexPatterns = ([ { obj: { - searchSource: { - getOwnField: (field: string) => { - return field === 'index' ? '1' : [{ meta: { index: 'filterIndex' } }]; + save, + }, + doc: { + _source: { + kibanaSavedObjectMeta: { + searchSourceJSON: JSON.stringify({ + index: '1', + filter: [{ meta: { index: 'filterIndex' } }], + }), }, - setField: jest.fn(), }, - hydrateIndexPattern, - save, }, }, { obj: { - searchSource: { - getOwnField: (field: string) => { - return field === 'index' ? '3' : undefined; + save, + }, + doc: { + _source: { + kibanaSavedObjectMeta: { + searchSourceJSON: JSON.stringify({ + index: '3', + }), }, }, - hydrateIndexPattern, - save, }, }, - ]; + ] as unknown) as Array<{ obj: SavedObject; doc: any }>; const resolutions = [ { @@ -337,9 +348,11 @@ describe('resolveSavedObjects', () => { const overwriteAll = false; - await resolveIndexPatternConflicts(resolutions, conflictedIndexPatterns, overwriteAll); + await resolveIndexPatternConflicts(resolutions, conflictedIndexPatterns, overwriteAll, ({ + get: (id: string) => Promise.resolve({ id }), + } as unknown) as IndexPatternsContract); - expect(conflictedIndexPatterns[0].obj.searchSource.setField).toHaveBeenCalledWith('filter', [ + expect(conflictedIndexPatterns[0].obj.searchSource!.getField('filter')).toEqual([ { meta: { index: 'newFilterIndex' } }, ]); expect(save.mock.calls.length).toBe(2); diff --git a/src/plugins/data/public/public.api.md b/src/plugins/data/public/public.api.md index e6e866e7284ef..42099c177dbbf 100644 --- a/src/plugins/data/public/public.api.md +++ b/src/plugins/data/public/public.api.md @@ -45,6 +45,7 @@ import * as React_2 from 'react'; import { Required } from '@kbn/utility-types'; import * as Rx from 'rxjs'; import { SavedObject as SavedObject_2 } from 'src/core/public'; +import { SavedObjectReference } from 'kibana/public'; import { SavedObjectsClientContract } from 'src/core/public'; import { SearchParams } from 'elasticsearch'; import { SearchResponse as SearchResponse_2 } from 'elasticsearch'; @@ -1274,6 +1275,9 @@ export interface OptionedValueProp { // @public (undocumented) export type ParsedInterval = ReturnType; +// @public +export const parseSearchSource: (indexPatterns: Pick) => (searchSourceJson: string, references: SavedObjectReference[]) => Promise; + // Warning: (ae-missing-release-tag) "PhraseFilter" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public (undocumented) @@ -1731,6 +1735,12 @@ export interface SearchStrategyProvider { search: (params: SearchStrategySearchParams) => SearchStrategyResponse; } +// @public +export function serializeSearchSource(searchSource: ISearchSource): { + searchSourceJSON: string; + references: SavedObjectReference[]; +}; + // Warning: (ae-missing-release-tag) "SortDirection" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public (undocumented) @@ -1881,21 +1891,21 @@ export type TSearchStrategyProvider = (context: ISearc // src/plugins/data/public/index.ts:234:27 - (ae-forgotten-export) The symbol "flattenHitWrapper" needs to be exported by the entry point index.d.ts // src/plugins/data/public/index.ts:234:27 - (ae-forgotten-export) The symbol "getRoutes" needs to be exported by the entry point index.d.ts // src/plugins/data/public/index.ts:234:27 - (ae-forgotten-export) The symbol "formatHitProvider" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:382:20 - (ae-forgotten-export) The symbol "getRequestInspectorStats" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:382:20 - (ae-forgotten-export) The symbol "getResponseInspectorStats" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:382:20 - (ae-forgotten-export) The symbol "tabifyAggResponse" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:382:20 - (ae-forgotten-export) The symbol "tabifyGetColumns" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:387:1 - (ae-forgotten-export) The symbol "CidrMask" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:388:1 - (ae-forgotten-export) The symbol "convertDateRangeToString" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:390:1 - (ae-forgotten-export) The symbol "dateHistogramInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:399:1 - (ae-forgotten-export) The symbol "InvalidEsCalendarIntervalError" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:400:1 - (ae-forgotten-export) The symbol "InvalidEsIntervalFormatError" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:401:1 - (ae-forgotten-export) The symbol "isDateHistogramBucketAggConfig" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:404:1 - (ae-forgotten-export) The symbol "isValidEsInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:405:1 - (ae-forgotten-export) The symbol "isValidInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:408:1 - (ae-forgotten-export) The symbol "parseInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:409:1 - (ae-forgotten-export) The symbol "propFilter" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:412:1 - (ae-forgotten-export) The symbol "toAbsoluteDates" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:384:20 - (ae-forgotten-export) The symbol "getRequestInspectorStats" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:384:20 - (ae-forgotten-export) The symbol "getResponseInspectorStats" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:384:20 - (ae-forgotten-export) The symbol "tabifyAggResponse" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:384:20 - (ae-forgotten-export) The symbol "tabifyGetColumns" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:389:1 - (ae-forgotten-export) The symbol "CidrMask" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:390:1 - (ae-forgotten-export) The symbol "convertDateRangeToString" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:392:1 - (ae-forgotten-export) The symbol "dateHistogramInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:401:1 - (ae-forgotten-export) The symbol "InvalidEsCalendarIntervalError" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:402:1 - (ae-forgotten-export) The symbol "InvalidEsIntervalFormatError" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:403:1 - (ae-forgotten-export) The symbol "isDateHistogramBucketAggConfig" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:406:1 - (ae-forgotten-export) The symbol "isValidEsInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:407:1 - (ae-forgotten-export) The symbol "isValidInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:410:1 - (ae-forgotten-export) The symbol "parseInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:411:1 - (ae-forgotten-export) The symbol "propFilter" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:414:1 - (ae-forgotten-export) The symbol "toAbsoluteDates" needs to be exported by the entry point index.d.ts // src/plugins/data/public/query/state_sync/connect_to_query_state.ts:33:33 - (ae-forgotten-export) The symbol "FilterStateStore" needs to be exported by the entry point index.d.ts // src/plugins/data/public/query/state_sync/connect_to_query_state.ts:37:1 - (ae-forgotten-export) The symbol "QueryStateChange" needs to be exported by the entry point index.d.ts // src/plugins/data/public/types.ts:52:5 - (ae-forgotten-export) The symbol "createFiltersFromEvent" needs to be exported by the entry point index.d.ts diff --git a/src/plugins/data/public/search/mocks.ts b/src/plugins/data/public/search/mocks.ts index b70e889066a45..993968282de2a 100644 --- a/src/plugins/data/public/search/mocks.ts +++ b/src/plugins/data/public/search/mocks.ts @@ -33,6 +33,8 @@ export const searchStartMock: jest.Mocked = { aggs: searchAggsStartMock(), setInterceptor: jest.fn(), search: jest.fn(), + parseSearchSource: jest.fn(), + serializeSearchSource: jest.fn(), __LEGACY: { AggConfig: jest.fn() as any, AggType: jest.fn(), diff --git a/src/plugins/data/public/search/search_source/parse_search_source.ts b/src/plugins/data/public/search/search_source/parse_search_source.ts index b2eb58fcb3ea5..d6c58a7f1301c 100644 --- a/src/plugins/data/public/search/search_source/parse_search_source.ts +++ b/src/plugins/data/public/search/search_source/parse_search_source.ts @@ -24,6 +24,21 @@ import { SearchSource } from './search_source'; import { IndexPatternsContract } from '../../index_patterns/index_patterns'; import { SearchSourceFields } from './types'; +/** + * Deserializes a json string and a set of referenced objects to a `SearchSource` instance. + * Use this method to re-create the search source serialized using `serializeSearchSource`. + * + * This function is a factory function that returns the actual utility when calling it with the + * required service dependency (index patterns contract). A pre-wired version is also exposed in + * the start contract of the data plugin as part of the search service + * + * @param indexPatterns The index patterns contract of the data plugin + * + * @return Wired utility function taking two parameters `searchSourceJson`, the json string + * returned by `serializeSearchSource` and `references`, a list of references including the ones + * returned by `serializeSearchSource`. + * + * @public */ export const parseSearchSource = (indexPatterns: IndexPatternsContract) => async ( searchSourceJson: string, references: SavedObjectReference[] diff --git a/src/plugins/data/public/search/search_source/serialize_search_source.ts b/src/plugins/data/public/search/search_source/serialize_search_source.ts index 4d69bbbb85ba3..7ecf2c4ae3932 100644 --- a/src/plugins/data/public/search/search_source/serialize_search_source.ts +++ b/src/plugins/data/public/search/search_source/serialize_search_source.ts @@ -38,6 +38,17 @@ function getFilters(filterField: SearchSourceFields['filter']): Filter[] { return [filterField]; } +/** + * Serializes a `SearchSource` instance to a JSON string and a set of referenced objects. + * Use this method to get a representation of the search source which can be stored in a saved object. + * + * The references returned by this function can be mixed with other references in the same object, + * however make sure there are no name-collisions. The references will be named `kibanaSavedObjectMeta.searchSourceJSON.index` + * and `kibanaSavedObjectMeta.searchSourceJSON.filter[].meta.index`. + * + * Using `parseSearchSource`, the instance can be re-created. + * @param searchSource The search source to serialize + * @public */ export function serializeSearchSource(searchSource: ISearchSource) { const references: SavedObjectReference[] = []; diff --git a/src/plugins/data/public/search/types.ts b/src/plugins/data/public/search/types.ts index c30e5c92ee7c5..ab12467d1dd01 100644 --- a/src/plugins/data/public/search/types.ts +++ b/src/plugins/data/public/search/types.ts @@ -17,8 +17,8 @@ * under the License. */ -import { CoreStart, SavedObjectReference } from 'kibana/public'; -import { ISearchSource } from './search_source'; +import { CoreStart } from 'kibana/public'; +import { serializeSearchSource, parseSearchSource } from './search_source'; import { SearchAggsSetup, SearchAggsStart, SearchAggsStartLegacy } from './aggs'; import { ISearch, ISearchGeneric } from './i_search'; import { TStrategyTypes } from './strategy_types'; @@ -90,29 +90,7 @@ export interface ISearchStart { aggs: SearchAggsStart; setInterceptor: (searchInterceptor: SearchInterceptor) => void; search: ISearchGeneric; - /** - * Serializes a `SearchSource` instance to a JSON string and a set of referenced objects. - * Use this method to get a representation of the search source which can be stored in a saved object. - * - * The references returned by this function can be mixed with other references in the same object, - * however make sure there are no name-collisions. The references will be named `kibanaSavedObjectMeta.searchSourceJSON.index` - * and `kibanaSavedObjectMeta.searchSourceJSON.filter[].meta.index`. - * - * Using `parseSearchSource`, the instance can be re-created. - * @param searchSource The search source to serialize - */ - serializeSearchSource: ( - searchSource: ISearchSource - ) => { searchSourceJSON: string; references: SavedObjectReference[] }; - /** - * Deserializes a json string and a set of referenced objects to a `SearchSource` instance. - * Use this method to re-create the search source serialized using `serializeSearchSource` - * @param searchSourceJson The json string returned by `serializeSearchSource` - * @param references A list of references including the ones returned by `serializeSearchSource` - */ - parseSearchSource: ( - searchSourceJson: string, - references: SavedObjectReference[] - ) => Promise; + serializeSearchSource: typeof serializeSearchSource; + parseSearchSource: ReturnType; __LEGACY: ISearchStartLegacy & SearchAggsStartLegacy; } From 40aededbb301f03bd83d35ca9fde63ebab04ba4b Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Mon, 30 Mar 2020 09:29:10 +0200 Subject: [PATCH 09/12] move serialize into search source --- .../kibana-plugin-plugins-data-public.md | 3 +- ...n-plugins-data-public.parsesearchsource.md | 4 +- ...plugin-plugins-data-public.searchsource.md | 1 + ...gins-data-public.searchsource.serialize.md | 27 +++++ ...ugins-data-public.serializesearchsource.md | 34 ------ src/plugins/data/public/index.ts | 1 - src/plugins/data/public/public.api.md | 40 ++++--- src/plugins/data/public/search/index.ts | 1 - src/plugins/data/public/search/mocks.ts | 1 - .../data/public/search/search_service.ts | 3 +- .../data/public/search/search_source/index.ts | 1 - .../data/public/search/search_source/mocks.ts | 1 + .../search_source/parse_search_source.ts | 2 +- .../search_source/search_source.test.ts | 75 ++++++++++++- .../search/search_source/search_source.ts | 82 ++++++++++++++ .../serialize_search_source.test.ts | 104 ------------------ .../search_source/serialize_search_source.ts | 104 ------------------ src/plugins/data/public/search/types.ts | 3 +- .../helpers/build_saved_object.ts | 3 +- .../helpers/serialize_saved_object.ts | 14 +-- 20 files changed, 217 insertions(+), 287 deletions(-) create mode 100644 docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchsource.serialize.md delete mode 100644 docs/development/plugins/data/public/kibana-plugin-plugins-data-public.serializesearchsource.md delete mode 100644 src/plugins/data/public/search/search_source/serialize_search_source.test.ts delete mode 100644 src/plugins/data/public/search/search_source/serialize_search_source.ts diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md index 30f83c32bac28..ac095ac74833d 100644 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md @@ -45,7 +45,6 @@ | [getSearchErrorType({ message })](./kibana-plugin-plugins-data-public.getsearcherrortype.md) | | | [getTime(indexPattern, timeRange, forceNow)](./kibana-plugin-plugins-data-public.gettime.md) | | | [plugin(initializerContext)](./kibana-plugin-plugins-data-public.plugin.md) | | -| [serializeSearchSource(searchSource)](./kibana-plugin-plugins-data-public.serializesearchsource.md) | Serializes a SearchSource instance to a JSON string and a set of referenced objects. Use this method to get a representation of the search source which can be stored in a saved object.The references returned by this function can be mixed with other references in the same object, however make sure there are no name-collisions. The references will be named kibanaSavedObjectMeta.searchSourceJSON.index and kibanaSavedObjectMeta.searchSourceJSON.filter[<number>].meta.index.Using parseSearchSource, the instance can be re-created. | ## Interfaces @@ -111,7 +110,7 @@ | [FilterBar](./kibana-plugin-plugins-data-public.filterbar.md) | | | [getKbnTypeNames](./kibana-plugin-plugins-data-public.getkbntypenames.md) | Get the esTypes known by all kbnFieldTypes {Array} | | [indexPatterns](./kibana-plugin-plugins-data-public.indexpatterns.md) | | -| [parseSearchSource](./kibana-plugin-plugins-data-public.parsesearchsource.md) | Deserializes a json string and a set of referenced objects to a SearchSource instance. Use this method to re-create the search source serialized using serializeSearchSource | +| [parseSearchSource](./kibana-plugin-plugins-data-public.parsesearchsource.md) | Deserializes a json string and a set of referenced objects to a SearchSource instance. Use this method to re-create the search source serialized using searchSource.serialize.This function is a factory function that returns the actual utility when calling it with the required service dependency (index patterns contract). A pre-wired version is also exposed in the start contract of the data plugin as part of the search service | | [QueryStringInput](./kibana-plugin-plugins-data-public.querystringinput.md) | | | [search](./kibana-plugin-plugins-data-public.search.md) | | | [SearchBar](./kibana-plugin-plugins-data-public.searchbar.md) | | diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.parsesearchsource.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.parsesearchsource.md index d8958c6ebdc34..74dabec5e982a 100644 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.parsesearchsource.md +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.parsesearchsource.md @@ -4,7 +4,9 @@ ## parseSearchSource variable -Deserializes a json string and a set of referenced objects to a `SearchSource` instance. Use this method to re-create the search source serialized using `serializeSearchSource` +Deserializes a json string and a set of referenced objects to a `SearchSource` instance. Use this method to re-create the search source serialized using `searchSource.serialize`. + +This function is a factory function that returns the actual utility when calling it with the required service dependency (index patterns contract). A pre-wired version is also exposed in the start contract of the data plugin as part of the search service Signature: diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchsource.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchsource.md index 8e1dbb6e2671d..99db432473075 100644 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchsource.md +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchsource.md @@ -38,6 +38,7 @@ export declare class SearchSource | [getParent()](./kibana-plugin-plugins-data-public.searchsource.getparent.md) | | Get the parent of this SearchSource {undefined\|searchSource} | | [getSearchRequestBody()](./kibana-plugin-plugins-data-public.searchsource.getsearchrequestbody.md) | | | | [onRequestStart(handler)](./kibana-plugin-plugins-data-public.searchsource.onrequeststart.md) | | Add a handler that will be notified whenever requests start | +| [serialize()](./kibana-plugin-plugins-data-public.searchsource.serialize.md) | | Serializes the instance to a JSON string and a set of referenced objects. Use this method to get a representation of the search source which can be stored in a saved object.The references returned by this function can be mixed with other references in the same object, however make sure there are no name-collisions. The references will be named kibanaSavedObjectMeta.searchSourceJSON.index and kibanaSavedObjectMeta.searchSourceJSON.filter[<number>].meta.index.Using parseSearchSource, the instance can be re-created. | | [setField(field, value)](./kibana-plugin-plugins-data-public.searchsource.setfield.md) | | | | [setFields(newFields)](./kibana-plugin-plugins-data-public.searchsource.setfields.md) | | | | [setParent(parent, options)](./kibana-plugin-plugins-data-public.searchsource.setparent.md) | | Set a searchSource that this source should inherit from | diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchsource.serialize.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchsource.serialize.md new file mode 100644 index 0000000000000..c47e7050e21c9 --- /dev/null +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchsource.serialize.md @@ -0,0 +1,27 @@ + + +[Home](./index.md) > [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) > [SearchSource](./kibana-plugin-plugins-data-public.searchsource.md) > [serialize](./kibana-plugin-plugins-data-public.searchsource.serialize.md) + +## SearchSource.serialize() method + +Serializes the instance to a JSON string and a set of referenced objects. Use this method to get a representation of the search source which can be stored in a saved object. + +The references returned by this function can be mixed with other references in the same object, however make sure there are no name-collisions. The references will be named `kibanaSavedObjectMeta.searchSourceJSON.index` and `kibanaSavedObjectMeta.searchSourceJSON.filter[].meta.index`. + +Using `parseSearchSource`, the instance can be re-created. + +Signature: + +```typescript +serialize(): { + searchSourceJSON: string; + references: SavedObjectReference[]; + }; +``` +Returns: + +`{ + searchSourceJSON: string; + references: SavedObjectReference[]; + }` + diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.serializesearchsource.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.serializesearchsource.md deleted file mode 100644 index 0798b0b4e9ad9..0000000000000 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.serializesearchsource.md +++ /dev/null @@ -1,34 +0,0 @@ - - -[Home](./index.md) > [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) > [serializeSearchSource](./kibana-plugin-plugins-data-public.serializesearchsource.md) - -## serializeSearchSource() function - -Serializes a `SearchSource` instance to a JSON string and a set of referenced objects. Use this method to get a representation of the search source which can be stored in a saved object. - -The references returned by this function can be mixed with other references in the same object, however make sure there are no name-collisions. The references will be named `kibanaSavedObjectMeta.searchSourceJSON.index` and `kibanaSavedObjectMeta.searchSourceJSON.filter[].meta.index`. - -Using `parseSearchSource`, the instance can be re-created. - -Signature: - -```typescript -export declare function serializeSearchSource(searchSource: ISearchSource): { - searchSourceJSON: string; - references: SavedObjectReference[]; -}; -``` - -## Parameters - -| Parameter | Type | Description | -| --- | --- | --- | -| searchSource | ISearchSource | | - -Returns: - -`{ - searchSourceJSON: string; - references: SavedObjectReference[]; -}` - diff --git a/src/plugins/data/public/index.ts b/src/plugins/data/public/index.ts index 55bea45df4f6e..efbfb4bb590fd 100644 --- a/src/plugins/data/public/index.ts +++ b/src/plugins/data/public/index.ts @@ -367,7 +367,6 @@ export { ISearchSource, SearchSource, parseSearchSource, - serializeSearchSource, SearchSourceFields, EsQuerySortValue, SortDirection, diff --git a/src/plugins/data/public/public.api.md b/src/plugins/data/public/public.api.md index 42099c177dbbf..aa408ca425321 100644 --- a/src/plugins/data/public/public.api.md +++ b/src/plugins/data/public/public.api.md @@ -1671,6 +1671,10 @@ export class SearchSource { // (undocumented) history: SearchRequest[]; onRequestStart(handler: (searchSource: ISearchSource, options?: FetchOptions) => Promise): void; + serialize(): { + searchSourceJSON: string; + references: SavedObjectReference[]; + }; // (undocumented) setField(field: K, value: SearchSourceFields[K]): this; // (undocumented) @@ -1735,12 +1739,6 @@ export interface SearchStrategyProvider { search: (params: SearchStrategySearchParams) => SearchStrategyResponse; } -// @public -export function serializeSearchSource(searchSource: ISearchSource): { - searchSourceJSON: string; - references: SavedObjectReference[]; -}; - // Warning: (ae-missing-release-tag) "SortDirection" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public (undocumented) @@ -1891,21 +1889,21 @@ export type TSearchStrategyProvider = (context: ISearc // src/plugins/data/public/index.ts:234:27 - (ae-forgotten-export) The symbol "flattenHitWrapper" needs to be exported by the entry point index.d.ts // src/plugins/data/public/index.ts:234:27 - (ae-forgotten-export) The symbol "getRoutes" needs to be exported by the entry point index.d.ts // src/plugins/data/public/index.ts:234:27 - (ae-forgotten-export) The symbol "formatHitProvider" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:384:20 - (ae-forgotten-export) The symbol "getRequestInspectorStats" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:384:20 - (ae-forgotten-export) The symbol "getResponseInspectorStats" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:384:20 - (ae-forgotten-export) The symbol "tabifyAggResponse" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:384:20 - (ae-forgotten-export) The symbol "tabifyGetColumns" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:389:1 - (ae-forgotten-export) The symbol "CidrMask" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:390:1 - (ae-forgotten-export) The symbol "convertDateRangeToString" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:392:1 - (ae-forgotten-export) The symbol "dateHistogramInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:401:1 - (ae-forgotten-export) The symbol "InvalidEsCalendarIntervalError" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:402:1 - (ae-forgotten-export) The symbol "InvalidEsIntervalFormatError" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:403:1 - (ae-forgotten-export) The symbol "isDateHistogramBucketAggConfig" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:406:1 - (ae-forgotten-export) The symbol "isValidEsInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:407:1 - (ae-forgotten-export) The symbol "isValidInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:410:1 - (ae-forgotten-export) The symbol "parseInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:411:1 - (ae-forgotten-export) The symbol "propFilter" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:414:1 - (ae-forgotten-export) The symbol "toAbsoluteDates" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:383:20 - (ae-forgotten-export) The symbol "getRequestInspectorStats" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:383:20 - (ae-forgotten-export) The symbol "getResponseInspectorStats" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:383:20 - (ae-forgotten-export) The symbol "tabifyAggResponse" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:383:20 - (ae-forgotten-export) The symbol "tabifyGetColumns" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:388:1 - (ae-forgotten-export) The symbol "CidrMask" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:389:1 - (ae-forgotten-export) The symbol "convertDateRangeToString" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:391:1 - (ae-forgotten-export) The symbol "dateHistogramInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:400:1 - (ae-forgotten-export) The symbol "InvalidEsCalendarIntervalError" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:401:1 - (ae-forgotten-export) The symbol "InvalidEsIntervalFormatError" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:402:1 - (ae-forgotten-export) The symbol "isDateHistogramBucketAggConfig" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:405:1 - (ae-forgotten-export) The symbol "isValidEsInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:406:1 - (ae-forgotten-export) The symbol "isValidInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:409:1 - (ae-forgotten-export) The symbol "parseInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:410:1 - (ae-forgotten-export) The symbol "propFilter" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:413:1 - (ae-forgotten-export) The symbol "toAbsoluteDates" needs to be exported by the entry point index.d.ts // src/plugins/data/public/query/state_sync/connect_to_query_state.ts:33:33 - (ae-forgotten-export) The symbol "FilterStateStore" needs to be exported by the entry point index.d.ts // src/plugins/data/public/query/state_sync/connect_to_query_state.ts:37:1 - (ae-forgotten-export) The symbol "QueryStateChange" needs to be exported by the entry point index.d.ts // src/plugins/data/public/types.ts:52:5 - (ae-forgotten-export) The symbol "createFiltersFromEvent" needs to be exported by the entry point index.d.ts diff --git a/src/plugins/data/public/search/index.ts b/src/plugins/data/public/search/index.ts index 8dc6c49c07646..36aff99668a2a 100644 --- a/src/plugins/data/public/search/index.ts +++ b/src/plugins/data/public/search/index.ts @@ -55,7 +55,6 @@ export { EsQuerySortValue, SortDirection, parseSearchSource, - serializeSearchSource, } from './search_source'; export { SearchInterceptor } from './search_interceptor'; diff --git a/src/plugins/data/public/search/mocks.ts b/src/plugins/data/public/search/mocks.ts index 993968282de2a..6966b7c3a2961 100644 --- a/src/plugins/data/public/search/mocks.ts +++ b/src/plugins/data/public/search/mocks.ts @@ -34,7 +34,6 @@ export const searchStartMock: jest.Mocked = { setInterceptor: jest.fn(), search: jest.fn(), parseSearchSource: jest.fn(), - serializeSearchSource: jest.fn(), __LEGACY: { AggConfig: jest.fn() as any, AggType: jest.fn(), diff --git a/src/plugins/data/public/search/search_service.ts b/src/plugins/data/public/search/search_service.ts index d2e108ec92dbd..e4620052be6f0 100644 --- a/src/plugins/data/public/search/search_service.ts +++ b/src/plugins/data/public/search/search_service.ts @@ -26,7 +26,7 @@ import { getEsClient, LegacyApiCaller } from './es_client'; import { ES_SEARCH_STRATEGY, DEFAULT_SEARCH_STRATEGY } from '../../common/search'; import { esSearchStrategyProvider } from './es_search/es_search_strategy'; import { IndexPatternsContract } from '../index_patterns/index_patterns'; -import { parseSearchSource, serializeSearchSource } from './search_source'; +import { parseSearchSource } from './search_source'; import { QuerySetup } from '../query/query_service'; import { SearchInterceptor } from './search_interceptor'; import { @@ -145,7 +145,6 @@ export class SearchService implements Plugin { // TODO: should an intercepror have a destroy method? this.searchInterceptor = searchInterceptor; }, - serializeSearchSource, parseSearchSource: parseSearchSource(indexPatterns), __LEGACY: { esClient: this.esClient!, diff --git a/src/plugins/data/public/search/search_source/index.ts b/src/plugins/data/public/search/search_source/index.ts index d17b2b657d302..6495bfb9188f2 100644 --- a/src/plugins/data/public/search/search_source/index.ts +++ b/src/plugins/data/public/search/search_source/index.ts @@ -19,5 +19,4 @@ export * from './search_source'; export { parseSearchSource } from './parse_search_source'; -export { serializeSearchSource } from './serialize_search_source'; export { SortDirection, EsQuerySortValue, SearchSourceFields } from './types'; diff --git a/src/plugins/data/public/search/search_source/mocks.ts b/src/plugins/data/public/search/search_source/mocks.ts index 700bea741bd6a..1ef7c1187a9e0 100644 --- a/src/plugins/data/public/search/search_source/mocks.ts +++ b/src/plugins/data/public/search/search_source/mocks.ts @@ -37,4 +37,5 @@ export const searchSourceMock: MockedKeys = { getSearchRequestBody: jest.fn(), destroy: jest.fn(), history: [], + serialize: jest.fn(), }; diff --git a/src/plugins/data/public/search/search_source/parse_search_source.ts b/src/plugins/data/public/search/search_source/parse_search_source.ts index d6c58a7f1301c..bd0ae949f7f62 100644 --- a/src/plugins/data/public/search/search_source/parse_search_source.ts +++ b/src/plugins/data/public/search/search_source/parse_search_source.ts @@ -26,7 +26,7 @@ import { SearchSourceFields } from './types'; /** * Deserializes a json string and a set of referenced objects to a `SearchSource` instance. - * Use this method to re-create the search source serialized using `serializeSearchSource`. + * Use this method to re-create the search source serialized using `searchSource.serialize`. * * This function is a factory function that returns the actual utility when calling it with the * required service dependency (index patterns contract). A pre-wired version is also exposed in diff --git a/src/plugins/data/public/search/search_source/search_source.test.ts b/src/plugins/data/public/search/search_source/search_source.test.ts index fcd116a3f4121..6bad093d31402 100644 --- a/src/plugins/data/public/search/search_source/search_source.test.ts +++ b/src/plugins/data/public/search/search_source/search_source.test.ts @@ -18,7 +18,7 @@ */ import { SearchSource } from './search_source'; -import { IndexPattern } from '../..'; +import { IndexPattern, SortDirection } from '../..'; import { mockDataServices } from '../aggs/test_helpers'; jest.mock('../fetch', () => ({ @@ -150,4 +150,77 @@ describe('SearchSource', function() { expect(parentFn).toBeCalledWith(searchSource, options); }); }); + + describe('#serialize', function() { + it('should reference index patterns', () => { + const indexPattern123 = { id: '123' } as IndexPattern; + const searchSource = new SearchSource(); + searchSource.setField('index', indexPattern123); + const { searchSourceJSON, references } = searchSource.serialize(); + expect(references[0].id).toEqual('123'); + expect(references[0].type).toEqual('index-pattern'); + expect(JSON.parse(searchSourceJSON).indexRefName).toEqual(references[0].name); + }); + + it('should add other fields', () => { + const searchSource = new SearchSource(); + searchSource.setField('highlightAll', true); + searchSource.setField('from', 123456); + const { searchSourceJSON } = searchSource.serialize(); + expect(JSON.parse(searchSourceJSON).highlightAll).toEqual(true); + expect(JSON.parse(searchSourceJSON).from).toEqual(123456); + }); + + it('should omit sort and size', () => { + const searchSource = new SearchSource(); + searchSource.setField('highlightAll', true); + searchSource.setField('from', 123456); + searchSource.setField('sort', { field: SortDirection.asc }); + searchSource.setField('size', 200); + const { searchSourceJSON } = searchSource.serialize(); + expect(Object.keys(JSON.parse(searchSourceJSON))).toEqual(['highlightAll', 'from']); + }); + + it('should serialize filters', () => { + const searchSource = new SearchSource(); + const filter = [ + { + query: 'query', + meta: { + alias: 'alias', + disabled: false, + negate: false, + }, + }, + ]; + searchSource.setField('filter', filter); + const { searchSourceJSON } = searchSource.serialize(); + expect(JSON.parse(searchSourceJSON).filter).toEqual(filter); + }); + + it('should reference index patterns in filters separately from index field', () => { + const searchSource = new SearchSource(); + const indexPattern123 = { id: '123' } as IndexPattern; + searchSource.setField('index', indexPattern123); + const filter = [ + { + query: 'query', + meta: { + alias: 'alias', + disabled: false, + negate: false, + index: '456', + }, + }, + ]; + searchSource.setField('filter', filter); + const { searchSourceJSON, references } = searchSource.serialize(); + expect(references[0].id).toEqual('123'); + expect(references[0].type).toEqual('index-pattern'); + expect(JSON.parse(searchSourceJSON).indexRefName).toEqual(references[0].name); + expect(references[1].id).toEqual('456'); + expect(references[1].type).toEqual('index-pattern'); + expect(JSON.parse(searchSourceJSON).filter[0].meta.indexRefName).toEqual(references[1].name); + }); + }); }); diff --git a/src/plugins/data/public/search/search_source/search_source.ts b/src/plugins/data/public/search/search_source/search_source.ts index 0c3321f03dabc..1e88b2aec7ff3 100644 --- a/src/plugins/data/public/search/search_source/search_source.ts +++ b/src/plugins/data/public/search/search_source/search_source.ts @@ -70,6 +70,7 @@ */ import _ from 'lodash'; +import { SavedObjectReference } from 'kibana/public'; import { normalizeSortRequest } from './normalize_sort_request'; import { filterDocvalueFields } from './filter_docvalue_fields'; import { fieldWildcardFilter } from '../../../../kibana_utils/public'; @@ -419,4 +420,85 @@ export class SearchSource { return searchRequest; } + + /** + * Serializes the instance to a JSON string and a set of referenced objects. + * Use this method to get a representation of the search source which can be stored in a saved object. + * + * The references returned by this function can be mixed with other references in the same object, + * however make sure there are no name-collisions. The references will be named `kibanaSavedObjectMeta.searchSourceJSON.index` + * and `kibanaSavedObjectMeta.searchSourceJSON.filter[].meta.index`. + * + * Using `parseSearchSource`, the instance can be re-created. + * @param searchSource The search source to serialize + * @public */ + public serialize() { + const references: SavedObjectReference[] = []; + + const { + filter: originalFilters, + ...searchSourceFields + }: Omit = _.omit(this.getFields(), ['sort', 'size']); + let serializedSearchSourceFields: Omit & { + indexRefName?: string; + filter?: Array & { meta: Filter['meta'] & { indexRefName?: string } }>; + } = searchSourceFields; + if (searchSourceFields.index) { + const indexId = searchSourceFields.index.id!; + const refName = 'kibanaSavedObjectMeta.searchSourceJSON.index'; + references.push({ + name: refName, + type: 'index-pattern', + id: indexId, + }); + serializedSearchSourceFields = { + ...serializedSearchSourceFields, + indexRefName: refName, + index: undefined, + }; + } + if (originalFilters) { + const filters = this.getFilters(originalFilters); + serializedSearchSourceFields = { + ...serializedSearchSourceFields, + filter: filters.map((filterRow, i) => { + if (!filterRow.meta || !filterRow.meta.index) { + return filterRow; + } + const refName = `kibanaSavedObjectMeta.searchSourceJSON.filter[${i}].meta.index`; + references.push({ + name: refName, + type: 'index-pattern', + id: filterRow.meta.index, + }); + return { + ...filterRow, + meta: { + ...filterRow.meta, + indexRefName: refName, + index: undefined, + }, + }; + }), + }; + } + + return { searchSourceJSON: JSON.stringify(serializedSearchSourceFields), references }; + } + + private getFilters(filterField: SearchSourceFields['filter']): Filter[] { + if (!filterField) { + return []; + } + + if (Array.isArray(filterField)) { + return filterField; + } + + if (_.isFunction(filterField)) { + return this.getFilters(filterField()); + } + + return [filterField]; + } } diff --git a/src/plugins/data/public/search/search_source/serialize_search_source.test.ts b/src/plugins/data/public/search/search_source/serialize_search_source.test.ts deleted file mode 100644 index 398da62f81a02..0000000000000 --- a/src/plugins/data/public/search/search_source/serialize_search_source.test.ts +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -import { IndexPattern } from '../../index_patterns/index_patterns'; -import { SearchSource } from './search_source'; -import { serializeSearchSource } from './serialize_search_source'; -import { SortDirection } from './types'; - -describe('serializeSearchSource', function() { - // const indexPatternMock: IIndexPattern = {} as IIndexPattern; - // let indexPatternContractMock: jest.Mocked; - - beforeEach(() => { - // indexPatternContractMock = ({ - // get: jest.fn().mockReturnValue(Promise.resolve(indexPatternMock)), - // } as unknown) as jest.Mocked; - }); - - it('should reference index patterns', () => { - const indexPattern = { id: '123' } as IndexPattern; - const searchSource = new SearchSource(); - searchSource.setField('index', indexPattern); - const { searchSourceJSON, references } = serializeSearchSource(searchSource); - expect(references[0].id).toEqual('123'); - expect(references[0].type).toEqual('index-pattern'); - expect(JSON.parse(searchSourceJSON).indexRefName).toEqual(references[0].name); - }); - - it('should add other fields', () => { - const searchSource = new SearchSource(); - searchSource.setField('highlightAll', true); - searchSource.setField('from', 123456); - const { searchSourceJSON } = serializeSearchSource(searchSource); - expect(JSON.parse(searchSourceJSON).highlightAll).toEqual(true); - expect(JSON.parse(searchSourceJSON).from).toEqual(123456); - }); - - it('should omit sort and size', () => { - const searchSource = new SearchSource(); - searchSource.setField('highlightAll', true); - searchSource.setField('from', 123456); - searchSource.setField('sort', { field: SortDirection.asc }); - searchSource.setField('size', 200); - const { searchSourceJSON } = serializeSearchSource(searchSource); - expect(Object.keys(JSON.parse(searchSourceJSON))).toEqual(['highlightAll', 'from']); - }); - - it('should serialize filters', () => { - const searchSource = new SearchSource(); - const filter = [ - { - query: 'query', - meta: { - alias: 'alias', - disabled: false, - negate: false, - }, - }, - ]; - searchSource.setField('filter', filter); - const { searchSourceJSON } = serializeSearchSource(searchSource); - expect(JSON.parse(searchSourceJSON).filter).toEqual(filter); - }); - - it('should reference index patterns in filters separately from index field', () => { - const searchSource = new SearchSource(); - const indexPattern = { id: '123' } as IndexPattern; - searchSource.setField('index', indexPattern); - const filter = [ - { - query: 'query', - meta: { - alias: 'alias', - disabled: false, - negate: false, - index: '456', - }, - }, - ]; - searchSource.setField('filter', filter); - const { searchSourceJSON, references } = serializeSearchSource(searchSource); - expect(references[0].id).toEqual('123'); - expect(references[0].type).toEqual('index-pattern'); - expect(JSON.parse(searchSourceJSON).indexRefName).toEqual(references[0].name); - expect(references[1].id).toEqual('456'); - expect(references[1].type).toEqual('index-pattern'); - expect(JSON.parse(searchSourceJSON).filter[0].meta.indexRefName).toEqual(references[1].name); - }); -}); diff --git a/src/plugins/data/public/search/search_source/serialize_search_source.ts b/src/plugins/data/public/search/search_source/serialize_search_source.ts deleted file mode 100644 index 7ecf2c4ae3932..0000000000000 --- a/src/plugins/data/public/search/search_source/serialize_search_source.ts +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -import _ from 'lodash'; -import { SavedObjectReference } from 'kibana/public'; -import { ISearchSource } from './search_source'; -import { SearchSourceFields } from './types'; -import { Filter } from '../../../common/es_query/filters'; - -function getFilters(filterField: SearchSourceFields['filter']): Filter[] { - if (!filterField) { - return []; - } - - if (Array.isArray(filterField)) { - return filterField; - } - - if (_.isFunction(filterField)) { - return getFilters(filterField()); - } - - return [filterField]; -} - -/** - * Serializes a `SearchSource` instance to a JSON string and a set of referenced objects. - * Use this method to get a representation of the search source which can be stored in a saved object. - * - * The references returned by this function can be mixed with other references in the same object, - * however make sure there are no name-collisions. The references will be named `kibanaSavedObjectMeta.searchSourceJSON.index` - * and `kibanaSavedObjectMeta.searchSourceJSON.filter[].meta.index`. - * - * Using `parseSearchSource`, the instance can be re-created. - * @param searchSource The search source to serialize - * @public */ -export function serializeSearchSource(searchSource: ISearchSource) { - const references: SavedObjectReference[] = []; - - const { - filter: originalFilters, - ...searchSourceFields - }: Omit = _.omit(searchSource.getFields(), ['sort', 'size']); - let serializedSearchSourceFields: Omit & { - indexRefName?: string; - filter?: Array & { meta: Filter['meta'] & { indexRefName?: string } }>; - } = searchSourceFields; - if (searchSourceFields.index) { - const indexId = searchSourceFields.index.id!; - const refName = 'kibanaSavedObjectMeta.searchSourceJSON.index'; - references.push({ - name: refName, - type: 'index-pattern', - id: indexId, - }); - serializedSearchSourceFields = { - ...serializedSearchSourceFields, - indexRefName: refName, - index: undefined, - }; - } - if (originalFilters) { - const filters = getFilters(originalFilters); - serializedSearchSourceFields = { - ...serializedSearchSourceFields, - filter: filters.map((filterRow, i) => { - if (!filterRow.meta || !filterRow.meta.index) { - return filterRow; - } - const refName = `kibanaSavedObjectMeta.searchSourceJSON.filter[${i}].meta.index`; - references.push({ - name: refName, - type: 'index-pattern', - id: filterRow.meta.index, - }); - return { - ...filterRow, - meta: { - ...filterRow.meta, - indexRefName: refName, - index: undefined, - }, - }; - }), - }; - } - - return { searchSourceJSON: JSON.stringify(serializedSearchSourceFields), references }; -} diff --git a/src/plugins/data/public/search/types.ts b/src/plugins/data/public/search/types.ts index ab12467d1dd01..4d0b867ae1435 100644 --- a/src/plugins/data/public/search/types.ts +++ b/src/plugins/data/public/search/types.ts @@ -18,7 +18,7 @@ */ import { CoreStart } from 'kibana/public'; -import { serializeSearchSource, parseSearchSource } from './search_source'; +import { parseSearchSource } from './search_source'; import { SearchAggsSetup, SearchAggsStart, SearchAggsStartLegacy } from './aggs'; import { ISearch, ISearchGeneric } from './i_search'; import { TStrategyTypes } from './strategy_types'; @@ -90,7 +90,6 @@ export interface ISearchStart { aggs: SearchAggsStart; setInterceptor: (searchInterceptor: SearchInterceptor) => void; search: ISearchGeneric; - serializeSearchSource: typeof serializeSearchSource; parseSearchSource: ReturnType; __LEGACY: ISearchStartLegacy & SearchAggsStartLegacy; } diff --git a/src/plugins/saved_objects/public/saved_object/helpers/build_saved_object.ts b/src/plugins/saved_objects/public/saved_object/helpers/build_saved_object.ts index 64bd6aa3b1c1a..41f0582a38823 100644 --- a/src/plugins/saved_objects/public/saved_object/helpers/build_saved_object.ts +++ b/src/plugins/saved_objects/public/saved_object/helpers/build_saved_object.ts @@ -88,8 +88,7 @@ export function buildSavedObject( * Serialize this object * @return {Object} */ - savedObject._serialize = () => - serializeSavedObject(savedObject, config, services.search.serializeSearchSource); + savedObject._serialize = () => serializeSavedObject(savedObject, config); /** * Returns true if the object's original title has been changed. New objects return false. diff --git a/src/plugins/saved_objects/public/saved_object/helpers/serialize_saved_object.ts b/src/plugins/saved_objects/public/saved_object/helpers/serialize_saved_object.ts index 72b1130460506..78f9eeb8b5fb1 100644 --- a/src/plugins/saved_objects/public/saved_object/helpers/serialize_saved_object.ts +++ b/src/plugins/saved_objects/public/saved_object/helpers/serialize_saved_object.ts @@ -19,13 +19,8 @@ import _ from 'lodash'; import { SavedObject, SavedObjectConfig } from '../../types'; import { expandShorthand } from '../../../../kibana_utils/public'; -import { DataPublicPluginStart } from '../../../../data/public'; -export function serializeSavedObject( - savedObject: SavedObject, - config: SavedObjectConfig, - serializeSearchSource: DataPublicPluginStart['search']['serializeSearchSource'] -) { +export function serializeSavedObject(savedObject: SavedObject, config: SavedObjectConfig) { // mapping definition for the fields that this object will expose const mapping = expandShorthand(config.mapping); const attributes = {} as Record; @@ -45,9 +40,10 @@ export function serializeSavedObject( }); if (savedObject.searchSource) { - const { searchSourceJSON, references: searchSourceReferences } = serializeSearchSource( - savedObject.searchSource - ); + const { + searchSourceJSON, + references: searchSourceReferences, + } = savedObject.searchSource.serialize(); attributes.kibanaSavedObjectMeta = { searchSourceJSON }; references.push(...searchSourceReferences); } From 732e990a1c73950c12c3179337738dc7ffe53a2d Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Mon, 6 Apr 2020 10:26:03 +0200 Subject: [PATCH 10/12] fix tests --- .../components/flyout/__jest__/flyout.test.js | 3 +- .../ui/public/new_platform/set_services.ts | 1 + .../public/saved_object/saved_object.test.ts | 52 +++++++++---------- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/legacy/core_plugins/kibana/public/management/sections/objects/components/objects_table/components/flyout/__jest__/flyout.test.js b/src/legacy/core_plugins/kibana/public/management/sections/objects/components/objects_table/components/flyout/__jest__/flyout.test.js index 97c0d5b89d657..9c8e12cc9090a 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/objects/components/objects_table/components/flyout/__jest__/flyout.test.js +++ b/src/legacy/core_plugins/kibana/public/management/sections/objects/components/objects_table/components/flyout/__jest__/flyout.test.js @@ -519,7 +519,8 @@ describe('Flyout', () => { expect(resolveIndexPatternConflicts).toHaveBeenCalledWith( component.instance().resolutions, mockConflictedIndexPatterns, - true + true, + defaultProps.indexPatterns ); expect(saveObjects).toHaveBeenCalledWith( mockConflictedSavedObjectsLinkedToSavedSearches, diff --git a/src/legacy/ui/public/new_platform/set_services.ts b/src/legacy/ui/public/new_platform/set_services.ts index e54a4c9bd6af9..400f31e73ffa1 100644 --- a/src/legacy/ui/public/new_platform/set_services.ts +++ b/src/legacy/ui/public/new_platform/set_services.ts @@ -72,6 +72,7 @@ export function setStartServices(npStart: NpStart) { visualizationsServices.setAggs(npStart.plugins.data.search.aggs); visualizationsServices.setOverlays(npStart.core.overlays); visualizationsServices.setChrome(npStart.core.chrome); + visualizationsServices.setSearch(npStart.plugins.data.search); const savedVisualizationsLoader = createSavedVisLoader({ savedObjectsClient: npStart.core.savedObjects.client, indexPatterns: npStart.plugins.data.indexPatterns, diff --git a/src/plugins/saved_objects/public/saved_object/saved_object.test.ts b/src/plugins/saved_objects/public/saved_object/saved_object.test.ts index 08389e9e3c97f..d3da3b58dd91a 100644 --- a/src/plugins/saved_objects/public/saved_object/saved_object.test.ts +++ b/src/plugins/saved_objects/public/saved_object/saved_object.test.ts @@ -103,9 +103,11 @@ describe('Saved Object', () => { } beforeEach(() => { + (dataStartMock.search.parseSearchSource as jest.Mock).mockReset(); SavedObjectClass = createSavedObjectClass({ savedObjectsClient: savedObjectsClientStub, indexPatterns: dataStartMock.indexPatterns, + search: dataStartMock.search, } as SavedObjectKibanaServices); }); @@ -269,7 +271,7 @@ describe('Saved Object', () => { ); }); - it('when index exists in searchSourceJSON', () => { + it('when search source references saved object', () => { const id = '123'; stubESResponse(getMockedDocResponse(id)); return createInitializedSavedObject({ type: 'dashboard', searchSource: true }).then( @@ -409,18 +411,17 @@ describe('Saved Object', () => { }); }); - it('throws error invalid JSON is detected', async () => { + it('forwards thrown exceptions from parseSearchSource', async () => { + (dataStartMock.search.parseSearchSource as jest.Mock).mockImplementation(() => { + throw new InvalidJSONProperty(''); + }); const savedObject = await createInitializedSavedObject({ type: 'dashboard', searchSource: true, }); const response = { found: true, - _source: { - kibanaSavedObjectMeta: { - searchSourceJSON: '"{\\n \\"filter\\": []\\n}"', - }, - }, + _source: {}, }; try { @@ -586,23 +587,24 @@ describe('Saved Object', () => { }); }); - it('injects references from searchSourceJSON', async () => { + it('passes references to search source parsing function', async () => { const savedObject = new SavedObjectClass({ type: 'dashboard', searchSource: true }); return savedObject.init!().then(() => { + const searchSourceJSON = JSON.stringify({ + indexRefName: 'kibanaSavedObjectMeta.searchSourceJSON.index', + filter: [ + { + meta: { + indexRefName: 'kibanaSavedObjectMeta.searchSourceJSON.filter[0].meta.index', + }, + }, + ], + }); const response = { found: true, _source: { kibanaSavedObjectMeta: { - searchSourceJSON: JSON.stringify({ - indexRefName: 'kibanaSavedObjectMeta.searchSourceJSON.index', - filter: [ - { - meta: { - indexRefName: 'kibanaSavedObjectMeta.searchSourceJSON.filter[0].meta.index', - }, - }, - ], - }), + searchSourceJSON, }, }, references: [ @@ -619,16 +621,10 @@ describe('Saved Object', () => { ], }; savedObject.applyESResp(response); - expect(savedObject.searchSource!.getFields()).toEqual({ - index: 'my-index-1', - filter: [ - { - meta: { - index: 'my-index-2', - }, - }, - ], - }); + expect(dataStartMock.search.parseSearchSource).toBeCalledWith( + searchSourceJSON, + response.references + ); }); }); }); From 8a5fc1b92956c8592c11147e41a9cbf69cfa756e Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Tue, 7 Apr 2020 13:55:48 +0200 Subject: [PATCH 11/12] rename parseSearchSource to createSearchSource --- ...plugins-data-public.createsearchsource.md} | 6 ++--- .../kibana-plugin-plugins-data-public.md | 2 +- ...plugin-plugins-data-public.searchsource.md | 2 +- ...gins-data-public.searchsource.serialize.md | 2 +- .../objects/lib/resolve_saved_objects.ts | 4 ++-- src/plugins/data/public/index.ts | 2 +- src/plugins/data/public/public.api.md | 6 ++--- src/plugins/data/public/search/index.ts | 2 +- src/plugins/data/public/search/mocks.ts | 2 +- .../data/public/search/search_service.ts | 4 ++-- ...e.test.ts => create_search_source.test.ts} | 22 +++++++++---------- ...arch_source.ts => create_search_source.ts} | 2 +- .../data/public/search/search_source/index.ts | 2 +- .../search/search_source/search_source.ts | 2 +- src/plugins/data/public/search/types.ts | 4 ++-- .../saved_object/helpers/apply_es_resp.ts | 4 ++-- .../helpers/build_saved_object.ts | 2 +- .../public/saved_object/saved_object.test.ts | 8 +++---- 18 files changed, 39 insertions(+), 39 deletions(-) rename docs/development/plugins/data/public/{kibana-plugin-plugins-data-public.parsesearchsource.md => kibana-plugin-plugins-data-public.createsearchsource.md} (55%) rename src/plugins/data/public/search/search_source/{parse_search_source.test.ts => create_search_source.test.ts} (85%) rename src/plugins/data/public/search/search_source/{parse_search_source.ts => create_search_source.ts} (98%) diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.parsesearchsource.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.createsearchsource.md similarity index 55% rename from docs/development/plugins/data/public/kibana-plugin-plugins-data-public.parsesearchsource.md rename to docs/development/plugins/data/public/kibana-plugin-plugins-data-public.createsearchsource.md index 74dabec5e982a..5c5aa348eecdf 100644 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.parsesearchsource.md +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.createsearchsource.md @@ -1,8 +1,8 @@ -[Home](./index.md) > [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) > [parseSearchSource](./kibana-plugin-plugins-data-public.parsesearchsource.md) +[Home](./index.md) > [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) > [createSearchSource](./kibana-plugin-plugins-data-public.createsearchsource.md) -## parseSearchSource variable +## createSearchSource variable Deserializes a json string and a set of referenced objects to a `SearchSource` instance. Use this method to re-create the search source serialized using `searchSource.serialize`. @@ -11,5 +11,5 @@ This function is a factory function that returns the actual utility when calling Signature: ```typescript -parseSearchSource: (indexPatterns: Pick) => (searchSourceJson: string, references: SavedObjectReference[]) => Promise +createSearchSource: (indexPatterns: Pick) => (searchSourceJson: string, references: SavedObjectReference[]) => Promise ``` diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md index ac095ac74833d..fc0dab94a0f65 100644 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md @@ -102,6 +102,7 @@ | [castEsToKbnFieldTypeName](./kibana-plugin-plugins-data-public.castestokbnfieldtypename.md) | Get the KbnFieldType name for an esType string | | [connectToQueryState](./kibana-plugin-plugins-data-public.connecttoquerystate.md) | Helper to setup two-way syncing of global data and a state container | | [createSavedQueryService](./kibana-plugin-plugins-data-public.createsavedqueryservice.md) | | +| [createSearchSource](./kibana-plugin-plugins-data-public.createsearchsource.md) | Deserializes a json string and a set of referenced objects to a SearchSource instance. Use this method to re-create the search source serialized using searchSource.serialize.This function is a factory function that returns the actual utility when calling it with the required service dependency (index patterns contract). A pre-wired version is also exposed in the start contract of the data plugin as part of the search service | | [ES\_SEARCH\_STRATEGY](./kibana-plugin-plugins-data-public.es_search_strategy.md) | | | [esFilters](./kibana-plugin-plugins-data-public.esfilters.md) | | | [esKuery](./kibana-plugin-plugins-data-public.eskuery.md) | | @@ -110,7 +111,6 @@ | [FilterBar](./kibana-plugin-plugins-data-public.filterbar.md) | | | [getKbnTypeNames](./kibana-plugin-plugins-data-public.getkbntypenames.md) | Get the esTypes known by all kbnFieldTypes {Array} | | [indexPatterns](./kibana-plugin-plugins-data-public.indexpatterns.md) | | -| [parseSearchSource](./kibana-plugin-plugins-data-public.parsesearchsource.md) | Deserializes a json string and a set of referenced objects to a SearchSource instance. Use this method to re-create the search source serialized using searchSource.serialize.This function is a factory function that returns the actual utility when calling it with the required service dependency (index patterns contract). A pre-wired version is also exposed in the start contract of the data plugin as part of the search service | | [QueryStringInput](./kibana-plugin-plugins-data-public.querystringinput.md) | | | [search](./kibana-plugin-plugins-data-public.search.md) | | | [SearchBar](./kibana-plugin-plugins-data-public.searchbar.md) | | diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchsource.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchsource.md index 99db432473075..5f2fc809a5590 100644 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchsource.md +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchsource.md @@ -38,7 +38,7 @@ export declare class SearchSource | [getParent()](./kibana-plugin-plugins-data-public.searchsource.getparent.md) | | Get the parent of this SearchSource {undefined\|searchSource} | | [getSearchRequestBody()](./kibana-plugin-plugins-data-public.searchsource.getsearchrequestbody.md) | | | | [onRequestStart(handler)](./kibana-plugin-plugins-data-public.searchsource.onrequeststart.md) | | Add a handler that will be notified whenever requests start | -| [serialize()](./kibana-plugin-plugins-data-public.searchsource.serialize.md) | | Serializes the instance to a JSON string and a set of referenced objects. Use this method to get a representation of the search source which can be stored in a saved object.The references returned by this function can be mixed with other references in the same object, however make sure there are no name-collisions. The references will be named kibanaSavedObjectMeta.searchSourceJSON.index and kibanaSavedObjectMeta.searchSourceJSON.filter[<number>].meta.index.Using parseSearchSource, the instance can be re-created. | +| [serialize()](./kibana-plugin-plugins-data-public.searchsource.serialize.md) | | Serializes the instance to a JSON string and a set of referenced objects. Use this method to get a representation of the search source which can be stored in a saved object.The references returned by this function can be mixed with other references in the same object, however make sure there are no name-collisions. The references will be named kibanaSavedObjectMeta.searchSourceJSON.index and kibanaSavedObjectMeta.searchSourceJSON.filter[<number>].meta.index.Using createSearchSource, the instance can be re-created. | | [setField(field, value)](./kibana-plugin-plugins-data-public.searchsource.setfield.md) | | | | [setFields(newFields)](./kibana-plugin-plugins-data-public.searchsource.setfields.md) | | | | [setParent(parent, options)](./kibana-plugin-plugins-data-public.searchsource.setparent.md) | | Set a searchSource that this source should inherit from | diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchsource.serialize.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchsource.serialize.md index c47e7050e21c9..52d25dec01dfd 100644 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchsource.serialize.md +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchsource.serialize.md @@ -8,7 +8,7 @@ Serializes the instance to a JSON string and a set of referenced objects. Use th The references returned by this function can be mixed with other references in the same object, however make sure there are no name-collisions. The references will be named `kibanaSavedObjectMeta.searchSourceJSON.index` and `kibanaSavedObjectMeta.searchSourceJSON.filter[].meta.index`. -Using `parseSearchSource`, the instance can be re-created. +Using `createSearchSource`, the instance can be re-created. Signature: diff --git a/src/legacy/core_plugins/kibana/public/management/sections/objects/lib/resolve_saved_objects.ts b/src/legacy/core_plugins/kibana/public/management/sections/objects/lib/resolve_saved_objects.ts index 19e0e4980b084..d9473367f7502 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/objects/lib/resolve_saved_objects.ts +++ b/src/legacy/core_plugins/kibana/public/management/sections/objects/lib/resolve_saved_objects.ts @@ -27,7 +27,7 @@ import { import { IndexPatternsContract, IIndexPattern, - parseSearchSource, + createSearchSource, } from '../../../../../../../../plugins/data/public'; type SavedObjectsRawDoc = Record; @@ -212,7 +212,7 @@ export async function resolveIndexPatternConflicts( // The user decided to skip this conflict so do nothing return; } - obj.searchSource = await parseSearchSource(indexPatterns)( + obj.searchSource = await createSearchSource(indexPatterns)( JSON.stringify(serializedSearchSource), replacedReferences ); diff --git a/src/plugins/data/public/index.ts b/src/plugins/data/public/index.ts index efbfb4bb590fd..06a46065baa84 100644 --- a/src/plugins/data/public/index.ts +++ b/src/plugins/data/public/index.ts @@ -366,7 +366,7 @@ export { SearchStrategyProvider, ISearchSource, SearchSource, - parseSearchSource, + createSearchSource, SearchSourceFields, EsQuerySortValue, SortDirection, diff --git a/src/plugins/data/public/public.api.md b/src/plugins/data/public/public.api.md index aa408ca425321..0ae1b5a6b0d54 100644 --- a/src/plugins/data/public/public.api.md +++ b/src/plugins/data/public/public.api.md @@ -210,6 +210,9 @@ export const connectToQueryState: ({ timefilter: { timefil // @public (undocumented) export const createSavedQueryService: (savedObjectsClient: Pick) => SavedQueryService; +// @public +export const createSearchSource: (indexPatterns: Pick) => (searchSourceJson: string, references: SavedObjectReference[]) => Promise; + // Warning: (ae-missing-release-tag) "CustomFilter" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public (undocumented) @@ -1275,9 +1278,6 @@ export interface OptionedValueProp { // @public (undocumented) export type ParsedInterval = ReturnType; -// @public -export const parseSearchSource: (indexPatterns: Pick) => (searchSourceJson: string, references: SavedObjectReference[]) => Promise; - // Warning: (ae-missing-release-tag) "PhraseFilter" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public (undocumented) diff --git a/src/plugins/data/public/search/index.ts b/src/plugins/data/public/search/index.ts index 36aff99668a2a..cce973d632f41 100644 --- a/src/plugins/data/public/search/index.ts +++ b/src/plugins/data/public/search/index.ts @@ -54,7 +54,7 @@ export { SearchSourceFields, EsQuerySortValue, SortDirection, - parseSearchSource, + createSearchSource, } from './search_source'; export { SearchInterceptor } from './search_interceptor'; diff --git a/src/plugins/data/public/search/mocks.ts b/src/plugins/data/public/search/mocks.ts index 6966b7c3a2961..cb1c625a72959 100644 --- a/src/plugins/data/public/search/mocks.ts +++ b/src/plugins/data/public/search/mocks.ts @@ -33,7 +33,7 @@ export const searchStartMock: jest.Mocked = { aggs: searchAggsStartMock(), setInterceptor: jest.fn(), search: jest.fn(), - parseSearchSource: jest.fn(), + createSearchSource: jest.fn(), __LEGACY: { AggConfig: jest.fn() as any, AggType: jest.fn(), diff --git a/src/plugins/data/public/search/search_service.ts b/src/plugins/data/public/search/search_service.ts index e4620052be6f0..8999343e9d083 100644 --- a/src/plugins/data/public/search/search_service.ts +++ b/src/plugins/data/public/search/search_service.ts @@ -26,7 +26,7 @@ import { getEsClient, LegacyApiCaller } from './es_client'; import { ES_SEARCH_STRATEGY, DEFAULT_SEARCH_STRATEGY } from '../../common/search'; import { esSearchStrategyProvider } from './es_search/es_search_strategy'; import { IndexPatternsContract } from '../index_patterns/index_patterns'; -import { parseSearchSource } from './search_source'; +import { createSearchSource } from './search_source'; import { QuerySetup } from '../query/query_service'; import { SearchInterceptor } from './search_interceptor'; import { @@ -145,7 +145,7 @@ export class SearchService implements Plugin { // TODO: should an intercepror have a destroy method? this.searchInterceptor = searchInterceptor; }, - parseSearchSource: parseSearchSource(indexPatterns), + createSearchSource: createSearchSource(indexPatterns), __LEGACY: { esClient: this.esClient!, AggConfig, diff --git a/src/plugins/data/public/search/search_source/parse_search_source.test.ts b/src/plugins/data/public/search/search_source/create_search_source.test.ts similarity index 85% rename from src/plugins/data/public/search/search_source/parse_search_source.test.ts rename to src/plugins/data/public/search/search_source/create_search_source.test.ts index d2c9da1d40acd..d49ce5a0d11f8 100644 --- a/src/plugins/data/public/search/search_source/parse_search_source.test.ts +++ b/src/plugins/data/public/search/search_source/create_search_source.test.ts @@ -16,13 +16,13 @@ * specific language governing permissions and limitations * under the License. */ -import { parseSearchSource as parseSearchSourceFactory } from './parse_search_source'; +import { createSearchSource as createSearchSourceFactory } from './create_search_source'; import { IIndexPattern } from '../../../common/index_patterns'; import { IndexPatternsContract } from '../../index_patterns/index_patterns'; import { Filter } from '../../../common/es_query/filters'; -describe('parseSearchSource', function() { - let parseSearchSource: ReturnType; +describe('createSearchSource', function() { + let createSearchSource: ReturnType; const indexPatternMock: IIndexPattern = {} as IIndexPattern; let indexPatternContractMock: jest.Mocked; @@ -30,17 +30,17 @@ describe('parseSearchSource', function() { indexPatternContractMock = ({ get: jest.fn().mockReturnValue(Promise.resolve(indexPatternMock)), } as unknown) as jest.Mocked; - parseSearchSource = parseSearchSourceFactory(indexPatternContractMock); + createSearchSource = createSearchSourceFactory(indexPatternContractMock); }); it('should fail if JSON is invalid', () => { - expect(parseSearchSource('{', [])).rejects.toThrow(); - expect(parseSearchSource('0', [])).rejects.toThrow(); - expect(parseSearchSource('"abcdefg"', [])).rejects.toThrow(); + expect(createSearchSource('{', [])).rejects.toThrow(); + expect(createSearchSource('0', [])).rejects.toThrow(); + expect(createSearchSource('"abcdefg"', [])).rejects.toThrow(); }); it('should set fields', async () => { - const searchSource = await parseSearchSource( + const searchSource = await createSearchSource( JSON.stringify({ highlightAll: true, query: { @@ -58,7 +58,7 @@ describe('parseSearchSource', function() { }); it('should resolve referenced index pattern', async () => { - const searchSource = await parseSearchSource( + const searchSource = await createSearchSource( JSON.stringify({ indexRefName: 'kibanaSavedObjectMeta.searchSourceJSON.index', }), @@ -75,7 +75,7 @@ describe('parseSearchSource', function() { }); it('should set filters and resolve referenced index patterns', async () => { - const searchSource = await parseSearchSource( + const searchSource = await createSearchSource( JSON.stringify({ filter: [ { @@ -136,7 +136,7 @@ describe('parseSearchSource', function() { }); it('should migrate legacy queries on the fly', async () => { - const searchSource = await parseSearchSource( + const searchSource = await createSearchSource( JSON.stringify({ highlightAll: true, query: 'a:b', diff --git a/src/plugins/data/public/search/search_source/parse_search_source.ts b/src/plugins/data/public/search/search_source/create_search_source.ts similarity index 98% rename from src/plugins/data/public/search/search_source/parse_search_source.ts rename to src/plugins/data/public/search/search_source/create_search_source.ts index bd0ae949f7f62..35b7ac4eb9762 100644 --- a/src/plugins/data/public/search/search_source/parse_search_source.ts +++ b/src/plugins/data/public/search/search_source/create_search_source.ts @@ -39,7 +39,7 @@ import { SearchSourceFields } from './types'; * returned by `serializeSearchSource`. * * @public */ -export const parseSearchSource = (indexPatterns: IndexPatternsContract) => async ( +export const createSearchSource = (indexPatterns: IndexPatternsContract) => async ( searchSourceJson: string, references: SavedObjectReference[] ) => { diff --git a/src/plugins/data/public/search/search_source/index.ts b/src/plugins/data/public/search/search_source/index.ts index 6495bfb9188f2..0e9f530d0968a 100644 --- a/src/plugins/data/public/search/search_source/index.ts +++ b/src/plugins/data/public/search/search_source/index.ts @@ -18,5 +18,5 @@ */ export * from './search_source'; -export { parseSearchSource } from './parse_search_source'; +export { createSearchSource } from './create_search_source'; export { SortDirection, EsQuerySortValue, SearchSourceFields } from './types'; diff --git a/src/plugins/data/public/search/search_source/search_source.ts b/src/plugins/data/public/search/search_source/search_source.ts index 1e88b2aec7ff3..c70db7bb82ef7 100644 --- a/src/plugins/data/public/search/search_source/search_source.ts +++ b/src/plugins/data/public/search/search_source/search_source.ts @@ -429,7 +429,7 @@ export class SearchSource { * however make sure there are no name-collisions. The references will be named `kibanaSavedObjectMeta.searchSourceJSON.index` * and `kibanaSavedObjectMeta.searchSourceJSON.filter[].meta.index`. * - * Using `parseSearchSource`, the instance can be re-created. + * Using `createSearchSource`, the instance can be re-created. * @param searchSource The search source to serialize * @public */ public serialize() { diff --git a/src/plugins/data/public/search/types.ts b/src/plugins/data/public/search/types.ts index 4d0b867ae1435..ba6e44f47b75e 100644 --- a/src/plugins/data/public/search/types.ts +++ b/src/plugins/data/public/search/types.ts @@ -18,7 +18,7 @@ */ import { CoreStart } from 'kibana/public'; -import { parseSearchSource } from './search_source'; +import { createSearchSource } from './search_source'; import { SearchAggsSetup, SearchAggsStart, SearchAggsStartLegacy } from './aggs'; import { ISearch, ISearchGeneric } from './i_search'; import { TStrategyTypes } from './strategy_types'; @@ -90,6 +90,6 @@ export interface ISearchStart { aggs: SearchAggsStart; setInterceptor: (searchInterceptor: SearchInterceptor) => void; search: ISearchGeneric; - parseSearchSource: ReturnType; + createSearchSource: ReturnType; __LEGACY: ISearchStartLegacy & SearchAggsStartLegacy; } diff --git a/src/plugins/saved_objects/public/saved_object/helpers/apply_es_resp.ts b/src/plugins/saved_objects/public/saved_object/helpers/apply_es_resp.ts index 9b0476dd63299..9776887b6d741 100644 --- a/src/plugins/saved_objects/public/saved_object/helpers/apply_es_resp.ts +++ b/src/plugins/saved_objects/public/saved_object/helpers/apply_es_resp.ts @@ -29,7 +29,7 @@ export async function applyESResp( resp: EsResponse, savedObject: SavedObject, config: SavedObjectConfig, - parseSearchSource: DataPublicPluginStart['search']['parseSearchSource'] + createSearchSource: DataPublicPluginStart['search']['createSearchSource'] ) { const mapping = expandShorthand(config.mapping); const esType = config.type || ''; @@ -65,7 +65,7 @@ export async function applyESResp( if (config.searchSource) { try { - savedObject.searchSource = await parseSearchSource(meta.searchSourceJSON, resp.references); + savedObject.searchSource = await createSearchSource(meta.searchSourceJSON, resp.references); } catch (error) { if ( error.constructor.name === 'SavedObjectNotFound' && diff --git a/src/plugins/saved_objects/public/saved_object/helpers/build_saved_object.ts b/src/plugins/saved_objects/public/saved_object/helpers/build_saved_object.ts index 41f0582a38823..e8faef4e9e040 100644 --- a/src/plugins/saved_objects/public/saved_object/helpers/build_saved_object.ts +++ b/src/plugins/saved_objects/public/saved_object/helpers/build_saved_object.ts @@ -82,7 +82,7 @@ export function buildSavedObject( savedObject.init = _.once(() => intializeSavedObject(savedObject, savedObjectsClient, config)); savedObject.applyESResp = (resp: EsResponse) => - applyESResp(resp, savedObject, config, services.search.parseSearchSource); + applyESResp(resp, savedObject, config, services.search.createSearchSource); /** * Serialize this object diff --git a/src/plugins/saved_objects/public/saved_object/saved_object.test.ts b/src/plugins/saved_objects/public/saved_object/saved_object.test.ts index d3da3b58dd91a..60c66f84080b2 100644 --- a/src/plugins/saved_objects/public/saved_object/saved_object.test.ts +++ b/src/plugins/saved_objects/public/saved_object/saved_object.test.ts @@ -103,7 +103,7 @@ describe('Saved Object', () => { } beforeEach(() => { - (dataStartMock.search.parseSearchSource as jest.Mock).mockReset(); + (dataStartMock.search.createSearchSource as jest.Mock).mockReset(); SavedObjectClass = createSavedObjectClass({ savedObjectsClient: savedObjectsClientStub, indexPatterns: dataStartMock.indexPatterns, @@ -411,8 +411,8 @@ describe('Saved Object', () => { }); }); - it('forwards thrown exceptions from parseSearchSource', async () => { - (dataStartMock.search.parseSearchSource as jest.Mock).mockImplementation(() => { + it('forwards thrown exceptions from createSearchSource', async () => { + (dataStartMock.search.createSearchSource as jest.Mock).mockImplementation(() => { throw new InvalidJSONProperty(''); }); const savedObject = await createInitializedSavedObject({ @@ -621,7 +621,7 @@ describe('Saved Object', () => { ], }; savedObject.applyESResp(response); - expect(dataStartMock.search.parseSearchSource).toBeCalledWith( + expect(dataStartMock.search.createSearchSource).toBeCalledWith( searchSourceJSON, response.references ); From 40a4e978ae308ca7476a862be29c5629df227448 Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Thu, 9 Apr 2020 09:43:06 +0200 Subject: [PATCH 12/12] remove unused property from type --- src/plugins/saved_objects/public/types.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/plugins/saved_objects/public/types.ts b/src/plugins/saved_objects/public/types.ts index b5a0b68ea2919..3184038040952 100644 --- a/src/plugins/saved_objects/public/types.ts +++ b/src/plugins/saved_objects/public/types.ts @@ -79,7 +79,6 @@ export interface SavedObjectKibanaServices { export interface SavedObjectConfig { // is only used by visualize afterESResp?: (savedObject: SavedObject) => Promise; - clearSavedIndexPattern?: boolean; defaults?: any; extractReferences?: (opts: { attributes: SavedObjectAttributes;