Skip to content

Commit

Permalink
[Ops] Change dev-cli's config merging logic (elastic#163928)
Browse files Browse the repository at this point in the history
## Summary
Change config merging behaviour, so that arrays are not
merged/concatenated but replaced.

Closes: elastic#162842

Related to: elastic#161884

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
(cherry picked from commit 819d304)
  • Loading branch information
delanni committed Aug 15, 2023
1 parent 21e122e commit 930f13a
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/cli/serve/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ const configPathCollector = pathCollector();
const pluginDirCollector = 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;
}
Expand Down Expand Up @@ -143,8 +143,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;
}
Expand Down Expand Up @@ -264,3 +264,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;
}
}
81 changes: 81 additions & 0 deletions src/cli/serve/serve.test.js
Original file line number Diff line number Diff line change
@@ -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: [],
},
});
});
});

0 comments on commit 930f13a

Please sign in to comment.