Skip to content

Commit

Permalink
Fixing issue prebid#4950
Browse files Browse the repository at this point in the history
Use a deepmerge function to merge both globa level config & bidder specific config
  • Loading branch information
leonardlabat committed Mar 26, 2020
1 parent 7c45626 commit d7424a5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import find from 'core-js/library/fn/array/find.js';
import includes from 'core-js/library/fn/array/includes.js';
import Set from 'core-js/library/fn/set.js';
import { parseQS } from './url.js';
import { mergeDeep } from './utils.js';

const from = require('core-js/library/fn/array/from.js');
const utils = require('./utils.js');
Expand Down Expand Up @@ -255,7 +256,7 @@ export function newConfig() {
memo[topic] = currBidderConfig[topic];
} else {
if (utils.isPlainObject(currBidderConfig[topic])) {
memo[topic] = Object.assign({}, config[topic], currBidderConfig[topic]);
memo[topic] = mergeDeep({}, config[topic], currBidderConfig[topic]);
} else {
memo[topic] = currBidderConfig[topic];
}
Expand Down
22 changes: 22 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1348,3 +1348,25 @@ export function compareOn(property) {
return 0;
}
}

export function isObject(item) {
return (item && typeof item === 'object' && !Array.isArray(item));
}

export function mergeDeep(target, ...sources) {
if (!sources.length) return target;
const source = sources.shift();

if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
mergeDeep(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}

return mergeDeep(target, ...sources);
}
8 changes: 4 additions & 4 deletions test/spec/unit/core/adapterManager_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ describe('adapterManager tests', function () {
buildRequests: {
data: 1
},
test1: { speedy: true },
test1: { speedy: true, fun: { test: true } },
interpretResponse: 'baseInterpret',
afterInterpretResponse: 'anotherBaseInterpret'
});
Expand Down Expand Up @@ -212,7 +212,7 @@ describe('adapterManager tests', function () {
data: 1,
test: 2
},
{ fun: { safe: true, cheap: false }, speedy: true },
{ fun: { safe: true, cheap: false, test: true }, speedy: true },
{ amazing: true },
'appnexusInterpret',
'anotherBaseInterpret'
Expand All @@ -221,14 +221,14 @@ describe('adapterManager tests', function () {
{
data: 1
},
{ speedy: true },
{ fun: { test: true }, speedy: true },
undefined,
'baseInterpret',
'anotherBaseInterpret'
],
'rubicon': [
'rubiconBuild',
{ speedy: true },
{ fun: { test: true }, speedy: true },
{ amazing: true },
null,
'anotherBaseInterpret'
Expand Down

0 comments on commit d7424a5

Please sign in to comment.