Skip to content

Commit

Permalink
Core: fix in ortb -> legacy native asset conversion (prebid#9923)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgirardi authored and Michele Nasti committed Aug 25, 2023
1 parent 824e22e commit f2ce5ee
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
24 changes: 20 additions & 4 deletions src/native.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,21 @@ export function toOrtbNativeRequest(legacyNativeAssets) {
return ortb;
}

/**
* Greatest common divisor between two positive integers
* https://en.wikipedia.org/wiki/Euclidean_algorithm
*/
function gcd(a, b) {
while (a && b && a !== b) {
if (a > b) {
a = a - b;
} else {
b = b - a;
}
}
return a || b;
}

/**
* This function converts an OpenRTB native request object to Prebid proprietary
* format. The purpose of this function is to help adapters to handle the
Expand Down Expand Up @@ -584,12 +599,13 @@ export function fromOrtbNativeRequest(openRTBRequest) {
if (asset.img.w && asset.img.h) {
image.sizes = [asset.img.w, asset.img.h];
} else if (asset.img.wmin && asset.img.hmin) {
image.aspect_ratios = {
const scale = gcd(asset.img.wmin, asset.img.hmin)
image.aspect_ratios = [{
min_width: asset.img.wmin,
min_height: asset.img.hmin,
ratio_width: asset.img.wmin,
ratio_height: asset.img.hmin
}
ratio_width: asset.img.wmin / scale,
ratio_height: asset.img.hmin / scale
}]
}

if (asset.img.type === NATIVE_IMAGE_TYPES.MAIN) {
Expand Down
24 changes: 12 additions & 12 deletions test/spec/native_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1002,22 +1002,22 @@ describe('validate native', function () {

expect(oldNativeRequest.image).to.deep.include({
required: false,
aspect_ratios: {
aspect_ratios: [{
min_width: 836,
min_height: 627,
ratio_width: 836,
ratio_height: 627
}
ratio_width: 4,
ratio_height: 3
}]
});

expect(oldNativeRequest.icon).to.deep.include({
required: true,
aspect_ratios: {
aspect_ratios: [{
min_width: 50,
min_height: 50,
ratio_width: 50,
ratio_height: 50
}
ratio_width: 1,
ratio_height: 1
}]
});
expect(oldNativeRequest.sponsoredBy).to.include({
required: true,
Expand Down Expand Up @@ -1119,12 +1119,12 @@ describe('validate native', function () {
},
icon: {
required: true,
aspect_ratios: {
aspect_ratios: [{
min_width: 50,
min_height: 50,
ratio_width: 50,
ratio_height: 50
}
ratio_width: 1,
ratio_height: 1
}]
},
sponsoredBy: {
required: true,
Expand Down

0 comments on commit f2ce5ee

Please sign in to comment.