Skip to content

Commit

Permalink
appnexus adapter support empty keyvalues in bidder params (#3257)
Browse files Browse the repository at this point in the history
* appnexus adapter support empty keyvalues in bidder params

* create function to check if array is populated

* refactor forEach functions
  • Loading branch information
jsnellbaker authored and mkendall07 committed Nov 6, 2018
1 parent ec47ed0 commit 7a5ca11
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
21 changes: 20 additions & 1 deletion modules/appnexusBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ export const spec = {
params.use_pmt_rule = (typeof params.usePaymentRule === 'boolean') ? params.usePaymentRule : false;
if (params.usePaymentRule) { delete params.usePaymentRule; }

if (isPopulatedArray(params.keywords)) {
params.keywords.forEach(deleteValues);
}

Object.keys(params).forEach(paramKey => {
let convertedKey = utils.convertCamelToUnderscore(paramKey);
if (convertedKey !== paramKey) {
Expand All @@ -191,6 +195,16 @@ export const spec = {
}
}

function isPopulatedArray(arr) {
return !!(utils.isArray(arr) && arr.length > 0);
}

function deleteValues(keyPairObj) {
if (isPopulatedArray(keyPairObj.value) && keyPairObj.value[0] === '') {
delete keyPairObj.value;
}
}

function newRenderer(adUnitCode, rtbBid, rendererOptions = {}) {
const renderer = Renderer.install({
id: rtbBid.renderer_id,
Expand Down Expand Up @@ -345,7 +359,12 @@ function bidToTag(bid) {
tag.external_imp_id = bid.params.externalImpId;
}
if (!utils.isEmpty(bid.params.keywords)) {
tag.keywords = utils.transformBidderParamKeywords(bid.params.keywords);
let keywords = utils.transformBidderParamKeywords(bid.params.keywords);

if (keywords.length > 0) {
keywords.forEach(deleteValues);
}
tag.keywords = keywords;
}

if (bid.mediaType === NATIVE || utils.deepAccess(bid, `mediaTypes.${NATIVE}`)) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ export function transformBidderParamKeywords(keywords, paramName = 'keywords') {
let values = [];
exports._each(v, (val) => {
val = exports.getValueString(paramName + '.' + k, val);
if (val) { values.push(val); }
if (val || val === '') { values.push(val); }
});
v = values;
} else {
Expand Down
6 changes: 6 additions & 0 deletions test/spec/modules/appnexusBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ describe('AppNexusAdapter', function () {
singleArrNum: [5],
multiValMixed: ['value1', 2, 'value3'],
singleValNum: 123,
emptyStr: '',
emptyArr: [''],
badValue: {'foo': 'bar'} // should be dropped
}
}
Expand All @@ -285,6 +287,10 @@ describe('AppNexusAdapter', function () {
}, {
'key': 'singleValNum',
'value': ['123']
}, {
'key': 'emptyStr'
}, {
'key': 'emptyArr'
}]);
});

Expand Down
69 changes: 69 additions & 0 deletions test/spec/utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -846,4 +846,73 @@ describe('Utils', function () {
expect(sizes).to.deep.equal([[300, 250], [300, 600]]);
});
});

describe('transformBidderParamKeywords', function () {
it('returns an array of objects when keyvalue is an array', function () {
let keywords = {
genre: ['rock', 'pop']
};
let result = utils.transformBidderParamKeywords(keywords);
expect(result).to.deep.equal([{
key: 'genre',
value: ['rock', 'pop']
}]);
});

it('returns an array of objects when keyvalue is a string', function () {
let keywords = {
genre: 'opera'
};
let result = utils.transformBidderParamKeywords(keywords);
expect(result).to.deep.equal([{
key: 'genre',
value: ['opera']
}]);
});

it('returns an array of objects when keyvalue is a number', function () {
let keywords = {
age: 15
};
let result = utils.transformBidderParamKeywords(keywords);
expect(result).to.deep.equal([{
key: 'age',
value: ['15']
}]);
});

it('returns an array of objects when using multiple keys with values of differing types', function () {
let keywords = {
genre: 'classical',
mix: ['1', 2, '3', 4],
age: 10
};
let result = utils.transformBidderParamKeywords(keywords);
expect(result).to.deep.equal([{
key: 'genre',
value: ['classical']
}, {
key: 'mix',
value: ['1', '2', '3', '4']
}, {
key: 'age',
value: ['10']
}]);
});

it('returns an array of objects when the keyvalue uses an empty string', function() {
let keywords = {
test: [''],
test2: ''
};
let result = utils.transformBidderParamKeywords(keywords);
expect(result).to.deep.equal([{
key: 'test',
value: ['']
}, {
key: 'test2',
value: ['']
}]);
});
});
});

0 comments on commit 7a5ca11

Please sign in to comment.