From f2ce5ee129f44b71997680d89f78dd9cf3387fa2 Mon Sep 17 00:00:00 2001 From: Demetrio Girardi Date: Mon, 22 May 2023 10:45:45 -0700 Subject: [PATCH] Core: fix in ortb -> legacy native asset conversion (#9923) --- src/native.js | 24 ++++++++++++++++++++---- test/spec/native_spec.js | 24 ++++++++++++------------ 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/native.js b/src/native.js index 79a972371da..927423c8d72 100644 --- a/src/native.js +++ b/src/native.js @@ -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 @@ -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) { diff --git a/test/spec/native_spec.js b/test/spec/native_spec.js index 9150329ff60..dee177d4b9b 100644 --- a/test/spec/native_spec.js +++ b/test/spec/native_spec.js @@ -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, @@ -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,