Skip to content

Commit 7f87dd2

Browse files
jaiminpanchal27dluxemburg
authored andcommitted
Aliasbidder fix (prebid#1652)
* aliasBidder did not work for bidderFactory * added function to get adapter spec * Freezing spec and moved unit tests * Updated test case to not import adapter
1 parent 7543a0b commit 7f87dd2

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

src/adaptermanager.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { flatten, getBidderCodes, getDefinedParams, shuffle } from './utils';
44
import { mapSizes } from './sizeMapping';
55
import { processNativeAdUnitParams, nativeAdapters } from './native';
6+
import { newBidder } from './adapters/bidderFactory';
67

78
var utils = require('./utils.js');
89
var CONSTANTS = require('./constants.json');
@@ -194,6 +195,13 @@ function transformHeightWidth(adUnit) {
194195
return sizesObj;
195196
}
196197

198+
function getSupportedMediaTypes(bidderCode) {
199+
let result = [];
200+
if (exports.videoAdapters.includes(bidderCode)) result.push('video');
201+
if (nativeAdapters.includes(bidderCode)) result.push('native');
202+
return result;
203+
}
204+
197205
exports.videoAdapters = []; // added by adapterLoader for now
198206

199207
exports.registerBidAdapter = function (bidAdaptor, bidderCode, {supportedMediaTypes = []} = {}) {
@@ -220,14 +228,24 @@ exports.aliasBidAdapter = function (bidderCode, alias) {
220228

221229
if (typeof existingAlias === 'undefined') {
222230
var bidAdaptor = _bidderRegistry[bidderCode];
223-
224231
if (typeof bidAdaptor === 'undefined') {
225232
utils.logError('bidderCode "' + bidderCode + '" is not an existing bidder.', 'adaptermanager.aliasBidAdapter');
226233
} else {
227234
try {
228-
let newAdapter = new bidAdaptor.constructor();
229-
newAdapter.setBidderCode(alias);
230-
this.registerBidAdapter(newAdapter, alias);
235+
let newAdapter;
236+
let supportedMediaTypes = getSupportedMediaTypes(bidderCode);
237+
// Have kept old code to support backward compatibilitiy.
238+
// Remove this if loop when all adapters are supporting bidderFactory. i.e When Prebid.js is 1.0
239+
if (bidAdaptor.constructor.prototype != Object.prototype) {
240+
newAdapter = new bidAdaptor.constructor();
241+
newAdapter.setBidderCode(alias);
242+
} else {
243+
let spec = bidAdaptor.getSpec();
244+
newAdapter = newBidder(Object.assign({}, spec, { code: alias }));
245+
}
246+
this.registerBidAdapter(newAdapter, alias, {
247+
supportedMediaTypes
248+
});
231249
} catch (e) {
232250
utils.logError(bidderCode + ' bidder does not currently support aliasing.', 'adaptermanager.aliasBidAdapter');
233251
}

src/adapters/bidderFactory.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ export function registerBidder(spec) {
138138
*/
139139
export function newBidder(spec) {
140140
return Object.assign(new Adapter(spec.code), {
141+
getSpec: function() {
142+
return Object.freeze(spec);
143+
},
141144
callBids: function(bidderRequest) {
142145
if (!Array.isArray(bidderRequest.bids)) {
143146
return;

test/spec/unit/core/adapterManager_spec.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import AdapterManager from 'src/adaptermanager';
33
import { getAdUnits } from 'test/fixtures/fixtures';
44
import CONSTANTS from 'src/constants.json';
55
import * as utils from 'src/utils';
6+
import { registerBidder } from 'src/adapters/bidderFactory';
67

78
const CONFIG = {
89
enabled: true,
@@ -81,4 +82,58 @@ describe('adapterManager tests', () => {
8182
expect(spy.called).to.equal(false);
8283
});
8384
})
85+
86+
describe('aliasBidderAdaptor', function() {
87+
const CODE = 'sampleBidder';
88+
89+
// Note: remove this describe once Prebid is 1.0
90+
describe('old way', function() {
91+
let originalRegistry;
92+
93+
function SampleAdapter() {
94+
return Object.assign(this, {
95+
callBids: sinon.stub(),
96+
setBidderCode: sinon.stub()
97+
});
98+
}
99+
100+
before(() => {
101+
originalRegistry = AdapterManager.bidderRegistry;
102+
AdapterManager.bidderRegistry[CODE] = new SampleAdapter();
103+
});
104+
105+
after(() => {
106+
AdapterManager.bidderRegistry = originalRegistry;
107+
});
108+
109+
it('should add alias to registry', () => {
110+
const alias = 'testalias';
111+
AdapterManager.aliasBidAdapter(CODE, alias);
112+
expect(AdapterManager.bidderRegistry).to.have.property(alias);
113+
});
114+
});
115+
116+
describe('using bidderFactory', function() {
117+
let spec;
118+
119+
beforeEach(() => {
120+
spec = {
121+
code: CODE,
122+
isBidRequestValid: () => {},
123+
buildRequests: () => {},
124+
interpretResponse: () => {},
125+
getUserSyncs: () => {}
126+
};
127+
});
128+
129+
it('should add alias to registry when original adapter is using bidderFactory', function() {
130+
let thisSpec = Object.assign(spec, { supportedMediaTypes: ['video'] });
131+
registerBidder(thisSpec);
132+
const alias = 'aliasBidder';
133+
AdapterManager.aliasBidAdapter(CODE, alias);
134+
expect(AdapterManager.bidderRegistry).to.have.property(alias);
135+
expect(AdapterManager.videoAdapters).to.include(alias);
136+
});
137+
});
138+
});
84139
});

0 commit comments

Comments
 (0)