From ae7acd35807a1c3213ef2618c00e7fbdc67e4ef8 Mon Sep 17 00:00:00 2001 From: Alexander Clouter Date: Sat, 19 Jun 2021 14:08:09 +0100 Subject: [PATCH] targeting: allow non-string (eg. numeric) targeting segments 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 --- src/targeting.js | 4 ++- test/spec/unit/core/targeting_spec.js | 47 ++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/targeting.js b/src/targeting.js index edf9521c251..4bbed7bb758 100644 --- a/src/targeting.js +++ b/src/targeting.js @@ -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] }; }); } diff --git a/test/spec/unit/core/targeting_spec.js b/test/spec/unit/core/targeting_spec.js index c82aac1acf9..f83bd2f6635 100644 --- a/test/spec/unit/core/targeting_spec.js +++ b/test/spec/unit/core/targeting_spec.js @@ -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'; @@ -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;