Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: fix aspect_ratios in ortb -> legacy native asset conversion #9923

Merged
merged 1 commit into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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