Skip to content

Commit

Permalink
Core: Redefine logic regarding imp.ext.<bidder> (prebid#3318)
Browse files Browse the repository at this point in the history
  • Loading branch information
CTMBNara authored Aug 6, 2024
1 parent 03fc557 commit 5753a03
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.prebid.server.auction.BidderAliases;
import org.prebid.server.auction.ImplicitParametersExtractor;
import org.prebid.server.auction.IpAddressHelper;
import org.prebid.server.auction.PriceGranularity;
Expand All @@ -33,6 +34,7 @@
import org.prebid.server.auction.model.Endpoint;
import org.prebid.server.auction.model.IpAddress;
import org.prebid.server.auction.model.SecBrowsingTopic;
import org.prebid.server.bidder.BidderCatalog;
import org.prebid.server.exception.BlocklistedAppException;
import org.prebid.server.exception.InvalidRequestException;
import org.prebid.server.exception.PreBidException;
Expand Down Expand Up @@ -93,6 +95,7 @@ public class Ortb2ImplicitParametersResolver {
private final String adServerCurrency;
private final List<String> blocklistedApps;
private final ExtRequestPrebidServer serverInfo;
private final BidderCatalog bidderCatalog;
private final ImplicitParametersExtractor paramsExtractor;
private final TimeoutResolver timeoutResolver;
private final IpAddressHelper ipAddressHelper;
Expand All @@ -108,6 +111,7 @@ public Ortb2ImplicitParametersResolver(boolean shouldCacheOnlyWinningBids,
String externalUrl,
Integer hostVendorId,
String datacenterRegion,
BidderCatalog bidderCatalog,
ImplicitParametersExtractor paramsExtractor,
TimeoutResolver timeoutResolver,
IpAddressHelper ipAddressHelper,
Expand All @@ -121,6 +125,7 @@ public Ortb2ImplicitParametersResolver(boolean shouldCacheOnlyWinningBids,
this.adServerCurrency = validateCurrency(Objects.requireNonNull(adServerCurrency));
this.blocklistedApps = Objects.requireNonNull(blocklistedApps);
this.serverInfo = ExtRequestPrebidServer.of(externalUrl, hostVendorId, datacenterRegion, null);
this.bidderCatalog = Objects.requireNonNull(bidderCatalog);
this.paramsExtractor = Objects.requireNonNull(paramsExtractor);
this.timeoutResolver = Objects.requireNonNull(timeoutResolver);
this.ipAddressHelper = Objects.requireNonNull(ipAddressHelper);
Expand Down Expand Up @@ -638,13 +643,16 @@ private List<Imp> populateImps(BidRequest bidRequest,
return null;
}

final BidderAliases aliases = aliases(bidRequest);

final ObjectNode globalBidderParams = extractGlobalBidderParams(bidRequest);

final boolean isUniqueIds = isUniqueIds(imps);
final List<ImpPopulationContext> impPopulationContexts = IntStream
.range(0, imps.size())
.mapToObj(index -> new ImpPopulationContext(
imps.get(index),
aliases,
globalBidderParams,
generateBidRequestId,
hasStoredBidRequest,
Expand All @@ -666,6 +674,14 @@ private List<Imp> populateImps(BidRequest bidRequest,
.toList();
}

private BidderAliases aliases(BidRequest bidRequest) {
final ExtRequest extRequest = bidRequest.getExt();
final ExtRequestPrebid prebid = extRequest != null ? extRequest.getPrebid() : null;
final Map<String, String> aliases = prebid != null ? prebid.getAliases() : null;
final Map<String, Integer> aliasgvlids = prebid != null ? prebid.getAliasgvlids() : null;
return BidderAliases.of(aliases, aliasgvlids, bidderCatalog);
}

private static ObjectNode extractGlobalBidderParams(BidRequest bidRequest) {
final ExtRequest extRequest = bidRequest.getExt();
final ExtRequestPrebid extBidPrebid = extRequest != null ? extRequest.getPrebid() : null;
Expand Down Expand Up @@ -925,7 +941,7 @@ private Long resolveTmax(Long requestTimeout) {
}

@Value
private static class ImpPopulationContext {
private class ImpPopulationContext {

private static final String DEALS_ONLY = "dealsonly";
private static final String PG_DEALS_ONLY = "pgdealsonly";
Expand All @@ -936,6 +952,7 @@ private static class ImpPopulationContext {
Imp populatedImp;

ImpPopulationContext(Imp imp,
BidderAliases aliases,
ObjectNode globalBidderParams,
boolean generateBidRequestId,
boolean hasStoredBidRequest,
Expand All @@ -947,6 +964,7 @@ private static class ImpPopulationContext {
this.imp = imp;
populatedImp = populateImp(
imp,
aliases,
globalBidderParams,
generateBidRequestId,
hasStoredBidRequest,
Expand All @@ -960,14 +978,15 @@ public Imp getPopulationResult() {
return populatedImp != null ? populatedImp : imp;
}

private static Imp populateImp(Imp imp,
ObjectNode globalBidderParams,
boolean generateBidRequestId,
boolean hasStoredBidRequest,
String impIdOverride,
JacksonMapper mapper,
IdGenerator tidGenerator,
JsonMerger jsonMerger) {
private Imp populateImp(Imp imp,
BidderAliases aliases,
ObjectNode globalBidderParams,
boolean generateBidRequestId,
boolean hasStoredBidRequest,
String impIdOverride,
JacksonMapper mapper,
IdGenerator tidGenerator,
JsonMerger jsonMerger) {

final String impId = imp.getId();
final String populatedImpId = populateImpId(impId, impIdOverride);
Expand All @@ -978,6 +997,7 @@ private static Imp populateImp(Imp imp,
final ObjectNode impExt = imp.getExt();
final ObjectNode populatedImpExt = populateImpExt(
impExt,
aliases,
globalBidderParams,
generateBidRequestId,
hasStoredBidRequest,
Expand Down Expand Up @@ -1011,16 +1031,17 @@ private static Integer populateImpSecure(Integer impSecure) {
return impSecure == null ? 1 : null;
}

private static ObjectNode populateImpExt(ObjectNode impExt,
ObjectNode globalBidderParams,
boolean generateBidRequestId,
boolean hasStoredBidRequest,
JacksonMapper mapper,
IdGenerator tidGenerator,
JsonMerger jsonMerger) {
private ObjectNode populateImpExt(ObjectNode impExt,
BidderAliases aliases,
ObjectNode globalBidderParams,
boolean generateBidRequestId,
boolean hasStoredBidRequest,
JacksonMapper mapper,
IdGenerator tidGenerator,
JsonMerger jsonMerger) {

final ObjectNode modifiedImpExt = prepareValidImpExtCopy(impExt, mapper);
final boolean isMoved = moveBidderParamsToPrebid(modifiedImpExt);
final boolean isMoved = moveBidderParamsToPrebid(modifiedImpExt, aliases);
final boolean isMerged = mergeGlobalBidderParamsToImpExt(modifiedImpExt, globalBidderParams, jsonMerger);
final boolean isDealsOnlyModified = modifyDealsOnly(modifiedImpExt);
final boolean isNonBidderFieldsModified = modifyNonBidderFields(
Expand Down Expand Up @@ -1048,8 +1069,11 @@ private static ObjectNode getOrCreateChildObjectNode(ObjectNode parentNode, Stri
return isObjectNode(childNode) ? (ObjectNode) childNode : parentNode.putObject(fieldName);
}

private static boolean moveBidderParamsToPrebid(ObjectNode impExt) {
private boolean moveBidderParamsToPrebid(ObjectNode impExt, BidderAliases aliases) {
final ObjectNode extPrebidBidder = bidderParamsFromImpExt(impExt);
if (!extPrebidBidder.isEmpty()) {
return false;
}

final Set<String> bidders = StreamUtil.asStream(impExt.fieldNames())
.filter(Ortb2ImplicitParametersResolver::isImpExtBidder)
Expand All @@ -1059,16 +1083,22 @@ private static boolean moveBidderParamsToPrebid(ObjectNode impExt) {
return false;
}

boolean modified = false;
for (String bidder : bidders) {
final ObjectNode bidderNode = getOrCreateChildObjectNode(extPrebidBidder, bidder);
final JsonNode impExtBidderNode = impExt.get(bidder);
if (!isObjectNode(impExtBidderNode)) {
continue;
}

final JsonNode impExtBidderNode = impExt.remove(bidder);
if (isObjectNode(impExtBidderNode)) {
bidderNode.setAll((ObjectNode) impExtBidderNode);
final ObjectNode bidderNode = getOrCreateChildObjectNode(extPrebidBidder, bidder);
bidderNode.setAll((ObjectNode) impExtBidderNode);
if (bidderCatalog.isValidName(aliases.resolveBidder(bidder))) {
impExt.remove(bidder);
}
modified = true;
}

return true;
return modified;
}

private static ObjectNode bidderParamsFromImpExt(ObjectNode ext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@
import org.prebid.server.log.CriteriaLogManager;
import org.prebid.server.log.CriteriaManager;
import org.prebid.server.log.HttpInteractionLogger;
import org.prebid.server.log.Logger;
import org.prebid.server.log.LoggerControlKnob;
import org.prebid.server.log.LoggerFactory;
import org.prebid.server.metric.Metrics;
import org.prebid.server.optout.GoogleRecaptchaVerifier;
import org.prebid.server.privacy.HostVendorTcfDefinerService;
Expand Down Expand Up @@ -149,8 +147,6 @@
@Configuration
public class ServiceConfiguration {

private static final Logger logger = LoggerFactory.getLogger(ServiceConfiguration.class);

@Value("${logging.sampling-rate:0.01}")
private double logSamplingRate;

Expand Down Expand Up @@ -293,6 +289,7 @@ Ortb2ImplicitParametersResolver ortb2ImplicitParametersResolver(
@Value("${external-url}") String externalUrl,
@Value("${gdpr.host-vendor-id:#{null}}") Integer hostVendorId,
@Value("${datacenter-region}") String datacenterRegion,
BidderCatalog bidderCatalog,
ImplicitParametersExtractor implicitParametersExtractor,
TimeoutResolver timeoutResolver,
IpAddressHelper ipAddressHelper,
Expand All @@ -309,6 +306,7 @@ Ortb2ImplicitParametersResolver ortb2ImplicitParametersResolver(
externalUrl,
hostVendorId,
datacenterRegion,
bidderCatalog,
implicitParametersExtractor,
timeoutResolver,
ipAddressHelper,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.prebid.server.functional.model.bidder

import com.fasterxml.jackson.annotation.JsonProperty
import groovy.transform.EqualsAndHashCode

@EqualsAndHashCode
class Generic implements BidderAdapter {

Object exampleProperty
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.prebid.server.functional.model.bidderspecific

import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString
import org.prebid.server.functional.model.request.auction.Imp

@ToString(includeNames = true, ignoreNulls = true)
@EqualsAndHashCode
class BidderImp extends Imp {

BidderImpExt ext
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.prebid.server.functional.model.request.auction

import groovy.transform.EqualsAndHashCode

@EqualsAndHashCode
class AnyUnsupportedBidder {

String anyUnsupportedField
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.prebid.server.functional.model.request.auction

import com.fasterxml.jackson.annotation.JsonProperty
import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString
import org.prebid.server.functional.model.bidder.AppNexus
import org.prebid.server.functional.model.bidder.Generic
import org.prebid.server.functional.model.bidder.Rubicon

@ToString(includeNames = true, ignoreNulls = true)
@EqualsAndHashCode
class ImpExt {

ImpExtPrebid prebid
Expand All @@ -21,6 +23,10 @@ class ImpExt {
String tid
String gpid
Integer ae
String all
String skadn
String general
AnyUnsupportedBidder anyUnsupportedBidder

static ImpExt getDefaultImpExt() {
new ImpExt().tap {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.prebid.server.functional.model.request.auction

import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString

@ToString(includeNames = true, ignoreNulls = true)
@EqualsAndHashCode
class ImpExtContext {

ImpExtContextData data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package org.prebid.server.functional.model.request.auction

import com.fasterxml.jackson.databind.PropertyNamingStrategies
import com.fasterxml.jackson.databind.annotation.JsonNaming
import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString

@JsonNaming(PropertyNamingStrategies.LowerCaseStrategy)
@ToString(includeNames = true, ignoreNulls = true)
@EqualsAndHashCode
class ImpExtContextData {

String language
Expand Down
Empty file.
Loading

0 comments on commit 5753a03

Please sign in to comment.