Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adapt ObjectToConfigAdapter.getFlattenedPaths to consider arrays as final values #56105

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/core/server/config/object_to_config_adapter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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 = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a nested layer to make sure it works with flattened keys?

      const data = {
        system: {
          name: 'Elastic',
        },
        components: [{ name: 'Kibana' }, { name: 'Elasticsearch' }],
      };
      const config = new ObjectToConfigAdapter(data);

      expect(config.getFlattenedPaths()).toEqual(['system.name', 'components']);

string: 'string',
array: ['an', 'array'],
};
const config = new ObjectToConfigAdapter(data);

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',
]);
});
});
});
17 changes: 2 additions & 15 deletions src/core/server/config/object_to_config_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import { cloneDeep, get, has, set } from 'lodash';

import { getFlattenedObject } from '../../utils';
import { Config, ConfigPath } from './';

/**
Expand All @@ -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<string> {
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);
}
}
}
18 changes: 18 additions & 0 deletions src/core/server/legacy/config/get_unused_config_keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,24 @@ describe('getUnusedConfigKeys', () => {
).toEqual(['foo.dolly']);
});

it('handles array values', async () => {
expect(
await getUnusedConfigKeys({
coreHandledConfigPaths: ['core', 'array'],
pluginSpecs: [],
disabledPluginSpecs: [],
settings: {
core: {
prop: 'value',
array: [1, 2, 3],
},
array: ['some', 'values'],
},
legacyConfig: getConfig({}),
})
).toEqual([]);
});

describe('using deprecation', () => {
it('should use the plugin deprecations provider', async () => {
expect(
Expand Down