From 4de85cd1c6483c52f80f46095f57d97a9f13ee1f Mon Sep 17 00:00:00 2001 From: pgayvallet Date: Tue, 28 Jan 2020 09:15:25 +0100 Subject: [PATCH 1/3] adapt getFlattenedPaths to consider arrays as final values --- .../config/object_to_config_adapter.test.ts | 34 +++++++++++++++++++ .../server/config/object_to_config_adapter.ts | 17 ++-------- 2 files changed, 36 insertions(+), 15 deletions(-) create mode 100644 src/core/server/config/object_to_config_adapter.test.ts diff --git a/src/core/server/config/object_to_config_adapter.test.ts b/src/core/server/config/object_to_config_adapter.test.ts new file mode 100644 index 0000000000000..75d6a40f5f630 --- /dev/null +++ b/src/core/server/config/object_to_config_adapter.test.ts @@ -0,0 +1,34 @@ +/* + * 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 { ObjectToConfigAdapter } from './object_to_config_adapter'; + +describe('ObjectToConfigAdapter', () => { + describe('#getFlattenedPaths()', () => { + it('considers arrays as final values', () => { + const data = { + a: 'string', + b: ['an', 'array'], + }; + const config = new ObjectToConfigAdapter(data); + + expect(config.getFlattenedPaths()).toEqual(['a', 'b']); + }); + }); +}); diff --git a/src/core/server/config/object_to_config_adapter.ts b/src/core/server/config/object_to_config_adapter.ts index b6ec772603565..d4c2f73364060 100644 --- a/src/core/server/config/object_to_config_adapter.ts +++ b/src/core/server/config/object_to_config_adapter.ts @@ -19,6 +19,7 @@ import { cloneDeep, get, has, set } from 'lodash'; +import { getFlattenedObject } from '../../utils'; import { Config, ConfigPath } from './'; /** @@ -41,24 +42,10 @@ export class ObjectToConfigAdapter implements Config { } public getFlattenedPaths() { - return [...flattenObjectKeys(this.rawConfig)]; + return Object.keys(getFlattenedObject(this.rawConfig)); } public toRaw() { return cloneDeep(this.rawConfig); } } - -function* flattenObjectKeys( - obj: { [key: string]: any }, - path: string = '' -): IterableIterator { - if (typeof obj !== 'object' || obj === null) { - yield path; - } else { - for (const [key, value] of Object.entries(obj)) { - const newPath = path !== '' ? `${path}.${key}` : key; - yield* flattenObjectKeys(value, newPath); - } - } -} From 5af2e9f3794b3b0c7f875a746b8778a22546217b Mon Sep 17 00:00:00 2001 From: pgayvallet Date: Tue, 28 Jan 2020 09:24:56 +0100 Subject: [PATCH 2/3] add getUnusedConfigKeys test --- .../config/get_unused_config_keys.test.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/core/server/legacy/config/get_unused_config_keys.test.ts b/src/core/server/legacy/config/get_unused_config_keys.test.ts index c4452fc6a1209..6ca1c87ceab39 100644 --- a/src/core/server/legacy/config/get_unused_config_keys.test.ts +++ b/src/core/server/legacy/config/get_unused_config_keys.test.ts @@ -200,6 +200,23 @@ describe('getUnusedConfigKeys', () => { ).toEqual(['foo.dolly']); }); + it('handles array values', async () => { + expect( + await getUnusedConfigKeys({ + coreHandledConfigPaths: ['core', 'array'], + pluginSpecs: [], + disabledPluginSpecs: [], + settings: { + core: { + prop: 'value', + }, + array: ['some', 'values'], + }, + legacyConfig: getConfig({}), + }) + ).toEqual([]); + }); + describe('using deprecation', () => { it('should use the plugin deprecations provider', async () => { expect( From caa6e6e48dddbee9c71ad05da4155800a95f4ca4 Mon Sep 17 00:00:00 2001 From: pgayvallet Date: Tue, 28 Jan 2020 12:17:17 +0100 Subject: [PATCH 3/3] improve tests --- .../config/object_to_config_adapter.test.ts | 25 ++++++++++++++++--- .../config/get_unused_config_keys.test.ts | 1 + 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/core/server/config/object_to_config_adapter.test.ts b/src/core/server/config/object_to_config_adapter.test.ts index 75d6a40f5f630..af41741e6208a 100644 --- a/src/core/server/config/object_to_config_adapter.test.ts +++ b/src/core/server/config/object_to_config_adapter.test.ts @@ -23,12 +23,31 @@ describe('ObjectToConfigAdapter', () => { describe('#getFlattenedPaths()', () => { it('considers arrays as final values', () => { const data = { - a: 'string', - b: ['an', 'array'], + string: 'string', + array: ['an', 'array'], }; const config = new ObjectToConfigAdapter(data); - expect(config.getFlattenedPaths()).toEqual(['a', 'b']); + expect(config.getFlattenedPaths()).toEqual(['string', 'array']); + }); + + it('handles nested arrays', () => { + const data = { + string: 'string', + array: ['an', 'array'], + nested: { + number: 12, + array: [{ key: 1 }, { key: 2 }], + }, + }; + const config = new ObjectToConfigAdapter(data); + + expect(config.getFlattenedPaths()).toEqual([ + 'string', + 'array', + 'nested.number', + 'nested.array', + ]); }); }); }); diff --git a/src/core/server/legacy/config/get_unused_config_keys.test.ts b/src/core/server/legacy/config/get_unused_config_keys.test.ts index 6ca1c87ceab39..2106a0748d814 100644 --- a/src/core/server/legacy/config/get_unused_config_keys.test.ts +++ b/src/core/server/legacy/config/get_unused_config_keys.test.ts @@ -209,6 +209,7 @@ describe('getUnusedConfigKeys', () => { settings: { core: { prop: 'value', + array: [1, 2, 3], }, array: ['some', 'values'], },