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

Prebid Server adapter: fix bug with disabling some of multiple instances #12727

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
95 changes: 49 additions & 46 deletions modules/prebidServerBidAdapter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,45 +105,45 @@ config.setDefaults({
});

/**
* @param {S2SConfig} option
* @param {S2SConfig} s2sConfig
* @return {boolean}
*/
function updateConfigDefaultVendor(option) {
if (option.defaultVendor) {
let vendor = option.defaultVendor;
let optionKeys = Object.keys(option);
function updateConfigDefaults(s2sConfig) {
if (s2sConfig.defaultVendor) {
let vendor = s2sConfig.defaultVendor;
let optionKeys = Object.keys(s2sConfig);
if (S2S_VENDORS[vendor]) {
// vendor keys will be set if either: the key was not specified by user
// or if the user did not set their own distinct value (ie using the system default) to override the vendor
Object.keys(S2S_VENDORS[vendor]).forEach((vendorKey) => {
if (s2sDefaultConfig[vendorKey] === option[vendorKey] || !includes(optionKeys, vendorKey)) {
option[vendorKey] = S2S_VENDORS[vendor][vendorKey];
if (s2sDefaultConfig[vendorKey] === s2sConfig[vendorKey] || !includes(optionKeys, vendorKey)) {
s2sConfig[vendorKey] = S2S_VENDORS[vendor][vendorKey];
}
});
} else {
logError('Incorrect or unavailable prebid server default vendor option: ' + vendor);
return false;
}
} else {
if (s2sConfig.adapter == null) {
s2sConfig.adapter = 'prebidServer';
}
}
// this is how we can know if user / defaultVendor has set it, or if we should default to false
return option.enabled = typeof option.enabled === 'boolean' ? option.enabled : false;
return true;
}

/**
* @param {S2SConfig} option
* @param {S2SConfig} s2sConfig
* @return {boolean}
*/
function validateConfigRequiredProps(option) {
const keys = Object.keys(option);
if (['accountId', 'endpoint'].filter(key => {
if (!includes(keys, key)) {
function validateConfigRequiredProps(s2sConfig) {
['accountId', 'endpoint'].forEach(key => {
if (s2sConfig[key] == null) {
logError(key + ' missing in server to server config');
return true;
return false;
}
return false;
}).length > 0) {
return false;
}
})
return true;
}

// temporary change to modify the s2sConfig for new format used for endpoint URLs;
Expand All @@ -164,40 +164,43 @@ function formatUrlParams(option) {
});
}

/**
* @param {(S2SConfig[]|S2SConfig)} options
*/
function setS2sConfig(options) {
export function validateConfig(options) {
if (!options) {
return;
}
const normalizedOptions = Array.isArray(options) ? options : [options];

const activeBidders = [];
const optionsValid = normalizedOptions.every((option, i, array) => {
formatUrlParams(option);
const updateSuccess = updateConfigDefaultVendor(option);
if (updateSuccess !== false) {
const valid = validateConfigRequiredProps(option);
if (valid !== false) {
if (Array.isArray(option['bidders'])) {
array[i]['bidders'] = option['bidders'].filter(bidder => {
if (activeBidders.indexOf(bidder) === -1) {
activeBidders.push(bidder);
return true;
}
options = Array.isArray(options) ? options : [options];
const activeBidders = new Set();
return options.filter(s2sConfig => {
formatUrlParams(s2sConfig);
if (
updateConfigDefaults(s2sConfig) &&
validateConfigRequiredProps(s2sConfig) &&
s2sConfig.enabled
) {
if (Array.isArray(s2sConfig.bidders)) {
s2sConfig.bidders = s2sConfig.bidders.filter(bidder => {
if (activeBidders.has(bidder)) {
return false;
});
}
return true;
} else {
activeBidders.add(bidder);
return true;
}
})
}
return true;
} else {
logWarn('prebidServer: s2s config is disabled', s2sConfig);
}
logWarn('prebidServer: s2s config is disabled');
return false;
});
})
}

if (optionsValid) {
return _s2sConfigs = normalizedOptions;
/**
* @param {(S2SConfig[]|S2SConfig)} options
*/
function setS2sConfig(options) {
options = validateConfig(options);
if (options.length) {
_s2sConfigs = options;
}
}
getConfig('s2sConfig', ({s2sConfig}) => setS2sConfig(s2sConfig));
Expand Down
30 changes: 30 additions & 0 deletions test/spec/modules/prebidServerBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
PrebidServer as Adapter,
resetSyncedStatus,
resetWurlMap,
validateConfig,
s2sDefaultConfig
} from 'modules/prebidServerBidAdapter/index.js';
import adapterManager, {PBS_ADAPTER_NAME} from 'src/adapterManager.js';
Expand Down Expand Up @@ -564,6 +565,35 @@ function addFpdEnrichmentsToS2SRequest(s2sReq, bidderRequests) {
}
}

describe('s2s configuration', () => {
let cfg1, cfg2;
beforeEach(() => {
cfg1 = {
enabled: true,
bidders: ['bidderB'],
accountId: '123456',
endpoint: {
p1Consent: 'first.endpoint'
}
};
cfg2 = {
enabled: true,
bidders: ['bidderA'],
accountId: '123456',
endpoint: {
p1Consent: 'second.endpoint',
}
};
})
it('sets prebid server adapter by default', () => {
expect(validateConfig(cfg1)[0].adapter).to.eql('prebidServer');
});
it('filters out disabled configs', () => {
cfg1.enabled = false;
expect(validateConfig([cfg1, cfg2])).to.eql([cfg2]);
})
});

describe('S2S Adapter', function () {
let adapter,
addBidResponse = sinon.spy(),
Expand Down