[Ops] Change dev-cli's config merging logic (#163928)

## Summary
Change config merging behaviour, so that arrays are not
merged/concatenated but replaced.

Closes: #162842 

Related to: https://github.com/elastic/kibana/pull/161884

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Alex Szabo 2023-08-15 16:58:51 +02:00 committed by GitHub
parent 53d2275c9e
commit 819d304210
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 97 additions and 4 deletions

View file

@ -55,11 +55,11 @@ function pathCollector() {
const configPathCollector = pathCollector();
const pluginPathCollector = pathCollector();
function applyConfigOverrides(rawConfig, opts, extraCliOptions) {
export function applyConfigOverrides(rawConfig, opts, extraCliOptions) {
const set = _.partial(lodashSet, rawConfig);
const get = _.partial(_.get, rawConfig);
const has = _.partial(_.has, rawConfig);
const merge = _.partial(_.merge, rawConfig);
if (opts.oss) {
delete rawConfig.xpack;
}
@ -135,8 +135,8 @@ function applyConfigOverrides(rawConfig, opts, extraCliOptions) {
set('plugins.paths', _.compact([].concat(get('plugins.paths'), opts.pluginPath)));
merge(extraCliOptions);
merge(readKeystore());
_.mergeWith(rawConfig, extraCliOptions, mergeAndReplaceArrays);
_.merge(rawConfig, readKeystore());
return rawConfig;
}
@ -257,3 +257,15 @@ export default function (program) {
});
});
}
function mergeAndReplaceArrays(objValue, srcValue) {
if (typeof srcValue === 'undefined') {
return objValue;
} else if (Array.isArray(srcValue)) {
// do not merge arrays, use new value instead
return srcValue;
} else {
// default to default merging
return undefined;
}
}

View file

@ -0,0 +1,81 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { applyConfigOverrides } from './serve';
describe('applyConfigOverrides', () => {
it('merges empty objects to an empty config', () => {
const output = applyConfigOverrides({}, {}, {});
const defaultEmptyConfig = {
plugins: {
paths: [],
},
};
expect(output).toEqual(defaultEmptyConfig);
});
it('merges objects', () => {
const output = applyConfigOverrides(
{
tomato: {
size: 40,
color: 'red',
},
},
{},
{
tomato: {
weight: 100,
},
}
);
expect(output).toEqual({
tomato: {
weight: 100,
color: 'red',
size: 40,
},
plugins: {
paths: [],
},
});
});
it('merges objects, but not arrays', () => {
const output = applyConfigOverrides(
{
tomato: {
color: 'red',
arr: [1, 2, 3],
},
},
{},
{
xyz: 40,
tomato: {
weight: 100,
arr: [4, 5],
},
}
);
expect(output).toEqual({
xyz: 40,
tomato: {
weight: 100,
color: 'red',
arr: [4, 5],
},
plugins: {
paths: [],
},
});
});
});