Skip to content

Commit

Permalink
targeting: allow non-string (eg. numeric) targeting segments (#7160)
Browse files Browse the repository at this point in the history
Documentation[1] shows a numeric example which causes an exception as we
try to call .split(',').

[1] https://docs.prebid.org/dev-docs/add-rtd-submodule.html#gettargetingdata
  • Loading branch information
jimdigriz authored Aug 12, 2021
1 parent 3d8ed53 commit 3b59a2b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/targeting.js
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,9 @@ export function newTargeting(auctionManager) {

return Object.keys(aut)
.map(function(key) {
return {[key]: utils.isArray(aut[key]) ? aut[key] : aut[key].split(',')};
if (utils.isStr(aut[key])) aut[key] = aut[key].split(',');
if (!utils.isArray(aut[key])) aut[key] = [ aut[key] ];
return { [key]: aut[key] };
});
}

Expand Down
47 changes: 46 additions & 1 deletion test/spec/unit/core/targeting_spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai';
import { targeting as targetingInstance, filters, getHighestCpmBidsFromBidPool, sortByDealAndPriceBucketOrCpm } from 'src/targeting.js';
import { config } from 'src/config.js';
import { getAdUnits, createBidReceived } from 'test/fixtures/fixtures.js';
import { createBidReceived } from 'test/fixtures/fixtures.js';
import CONSTANTS from 'src/constants.json';
import { auctionManager } from 'src/auctionManager.js';
import * as utils from 'src/utils.js';
Expand Down Expand Up @@ -280,6 +280,51 @@ describe('targeting tests', function () {
bidExpiryStub.restore();
});

describe('when handling different adunit targeting value types', function () {
const adUnitCode = '/123456/header-bid-tag-0';
const adServerTargeting = {};

let getAdUnitsStub;

before(function() {
getAdUnitsStub = sandbox.stub(auctionManager, 'getAdUnits').callsFake(function() {
return [
{
'code': adUnitCode,
[CONSTANTS.JSON_MAPPING.ADSERVER_TARGETING]: adServerTargeting
}
];
});
});

after(function() {
getAdUnitsStub.restore();
});

afterEach(function() {
delete adServerTargeting.test_type;
});

const pairs = [
['string', '2.3', '2.3'],
['number', 2.3, '2.3'],
['boolean', true, 'true'],
['string-separated', '2.3,4.5', '2.3, 4.5'],
['array-of-string', ['2.3', '4.5'], '2.3, 4.5'],
['array-of-number', [2.3, 4.5], '2.3, 4.5'],
['array-of-boolean', [true, false], 'true, false']
];
pairs.forEach(([type, value, result]) => {
it(`accepts ${type}`, function() {
adServerTargeting.test_type = value;

const targeting = targetingInstance.getAllTargeting([adUnitCode]);

expect(targeting[adUnitCode].test_type).is.equal(result);
});
});
});

describe('when hb_deal is present in bid.adserverTargeting', function () {
let bid4;

Expand Down

0 comments on commit 3b59a2b

Please sign in to comment.