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

Allows selected injected default vars to be merged instead of overwritten #25318

Merged
merged 5 commits into from
Nov 16, 2018
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
20 changes: 20 additions & 0 deletions src/ui/ui_render/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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.
*/

export { mergeVariables } from './merge_variables';
206 changes: 206 additions & 0 deletions src/ui/ui_render/lib/merge_variables.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/*
* 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 { mergeVariables } from './merge_variables';

describe('mergeVariables', () => {
it('merges two objects together', () => {
const someVariables = {
name: 'value',
canFoo: true,
nested: {
anotherVariable: 'ok',
},
};

const otherVariables = {
otherName: 'value',
otherCanFoo: true,
otherNested: {
otherAnotherVariable: 'ok',
},
};

const result = mergeVariables(someVariables, otherVariables);

expect(result).toEqual({
name: 'value',
canFoo: true,
nested: {
anotherVariable: 'ok',
},
otherName: 'value',
otherCanFoo: true,
otherNested: {
otherAnotherVariable: 'ok',
},
});
});

it('does not mutate the source objects', () => {
const original = {
var1: 'original',
};

const set1 = {
var1: 'value1',
var2: 'value1',
};

const set2 = {
var1: 'value2',
var2: 'value2',
var3: 'value2',
};

const set3 = {
var1: 'value3',
var2: 'value3',
var3: 'value3',
var4: 'value3',
};

mergeVariables(original, set1, set2, set3);

expect(original).toEqual({ var1: 'original' });
expect(set1).toEqual({ var1: 'value1', var2: 'value1' });
expect(set2).toEqual({ var1: 'value2', var2: 'value2', var3: 'value2' });
expect(set3).toEqual({ var1: 'value3', var2: 'value3', var3: 'value3', var4: 'value3' });
});

it('merges multiple objects together, preferring the leftmost values', () => {
const original = {
var1: 'original',
};

const set1 = {
var1: 'value1',
var2: 'value1',
};

const set2 = {
var1: 'value2',
var2: 'value2',
var3: 'value2',
};

const set3 = {
var1: 'value3',
var2: 'value3',
var3: 'value3',
var4: 'value3',
};

const result = mergeVariables(original, set1, set2, set3);

expect(result).toEqual({
var1: 'original',
var2: 'value1',
var3: 'value2',
var4: 'value3',
});
});

it('retains the original variable value if a duplicate entry is found', () => {
const someVariables = {
name: 'value',
canFoo: true,
nested: {
anotherVariable: 'ok',
},
};

const otherVariables = {
nested: {
otherAnotherVariable: 'ok',
},
};

const result = mergeVariables(someVariables, otherVariables);
expect(result).toEqual({
name: 'value',
canFoo: true,
nested: {
anotherVariable: 'ok',
},
});
});

it('combines entries within "uiCapabilities"', () => {
const someVariables = {
name: 'value',
canFoo: true,
uiCapabilities: {
firstCapability: 'ok',
},
};

const otherVariables = {
uiCapabilities: {
secondCapability: 'ok',
},
};

const result = mergeVariables(someVariables, otherVariables);

expect(result).toEqual({
name: 'value',
canFoo: true,
uiCapabilities: {
firstCapability: 'ok',
secondCapability: 'ok',
},
});
});

it('does not deeply combine entries within "uiCapabilities"', () => {
const someVariables = {
name: 'value',
canFoo: true,
uiCapabilities: {
firstCapability: 'ok',
nestedCapability: {
nestedProp: 'nestedValue',
},
},
};

const otherVariables = {
uiCapabilities: {
secondCapability: 'ok',
nestedCapability: {
otherNestedProp: 'otherNestedValue',
},
},
};

const result = mergeVariables(someVariables, otherVariables);
expect(result).toEqual({
name: 'value',
canFoo: true,
uiCapabilities: {
firstCapability: 'ok',
secondCapability: 'ok',
nestedCapability: {
nestedProp: 'nestedValue',
},
},
});
});
});
39 changes: 39 additions & 0 deletions src/ui/ui_render/lib/merge_variables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.
*/

const ELIGIBLE_FLAT_MERGE_KEYS = ['uiCapabilities'];

export function mergeVariables(...sources: Array<Record<string, any>>) {
const result: Record<string, any> = {};

for (const source of sources) {
Object.entries(source).forEach(([key, value]) => {
if (ELIGIBLE_FLAT_MERGE_KEYS.includes(key)) {
result[key] = {
...value,
...result[key],
};
} else if (!result.hasOwnProperty(key)) {
result[key] = value;
}
});
}

return result;
}
6 changes: 3 additions & 3 deletions src/ui/ui_render/ui_render_mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
* under the License.
*/

import { defaults } from 'lodash';
import { props, reduce as reduceAsync } from 'bluebird';
import Boom from 'boom';
import { resolve } from 'path';
import { i18n } from '@kbn/i18n';
import { AppBootstrap } from './bootstrap';
import { mergeVariables } from './lib';

export function uiRenderMixin(kbnServer, server, config) {
function replaceInjectedVars(request, injectedVars) {
Expand All @@ -40,7 +40,7 @@ export function uiRenderMixin(kbnServer, server, config) {
const { defaultInjectedVarProviders = [] } = kbnServer.uiExports;
defaultInjectedVars = defaultInjectedVarProviders
.reduce((allDefaults, { fn, pluginSpec }) => (
defaults(
mergeVariables(
allDefaults,
fn(kbnServer.server, pluginSpec.readConfigValue(kbnServer.config, []))
)
Expand Down Expand Up @@ -145,7 +145,7 @@ export function uiRenderMixin(kbnServer, server, config) {
basePath,
vars: await replaceInjectedVars(
request,
defaults(
mergeVariables(
injectedVarsOverrides,
await server.getInjectedUiAppVars(app.getId()),
defaultInjectedVars,
Expand Down