Skip to content

Commit

Permalink
Fix Bidder Aliases Validation
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoxaAntoxic committed Jan 23, 2025
1 parent 6e46054 commit d188ada
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 16 deletions.
45 changes: 29 additions & 16 deletions src/main/java/org/prebid/server/validation/ImpValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.prebid.server.auction.BidderAliases;
import org.prebid.server.bidder.BidderCatalog;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.proto.openrtb.ext.request.ExtImpPrebid;
Expand Down Expand Up @@ -365,9 +366,10 @@ private void validateImpExt(ObjectNode ext, Map<String, String> aliases, int imp
validateImpExtPrebid(ext != null ? ext.get(PREBID_EXT) : null, aliases, impIndex, warnings);
}

private void validateImpExtPrebid(JsonNode extPrebidNode, Map<String, String> aliases, int impIndex,
List<String> warnings)
throws ValidationException {
private void validateImpExtPrebid(JsonNode extPrebidNode,
Map<String, String> requestAliases,
int impIndex,
List<String> warnings) throws ValidationException {

if (extPrebidNode == null) {
throw new ValidationException(
Expand All @@ -387,15 +389,21 @@ private void validateImpExtPrebid(JsonNode extPrebidNode, Map<String, String> al
}
final ExtImpPrebid extPrebid = parseExtImpPrebid((ObjectNode) extPrebidNode, impIndex);

validateImpExtPrebidBidder(extPrebidBidderNode, extPrebid.getStoredAuctionResponse(),
aliases, impIndex, warnings);
validateImpExtPrebidStoredResponses(extPrebid, aliases, impIndex, warnings);
final BidderAliases aliases = BidderAliases.of(requestAliases, null, bidderCatalog);

validateImpExtPrebidBidder(
extPrebidBidderNode,
extPrebid.getStoredAuctionResponse(),
aliases,
impIndex,
warnings);

validateImpExtPrebidStoredResponses(extPrebid, aliases, impIndex, warnings);
validateImpExtPrebidImp(extPrebidNode.get(IMP_EXT), aliases, impIndex, warnings);
}

private void validateImpExtPrebidImp(JsonNode imp,
Map<String, String> aliases,
BidderAliases aliases,
int impIndex,
List<String> warnings) {
if (imp == null) {
Expand All @@ -406,7 +414,7 @@ private void validateImpExtPrebidImp(JsonNode imp,
while (bidders.hasNext()) {
final Map.Entry<String, JsonNode> bidder = bidders.next();
final String bidderName = bidder.getKey();
final String resolvedBidderName = aliases.getOrDefault(bidderName, bidderName);
final String resolvedBidderName = aliases.resolveBidder(bidderName);
if (!bidderCatalog.isValidName(resolvedBidderName) && !bidderCatalog.isDeprecatedName(resolvedBidderName)) {
bidders.remove();
warnings.add("WARNING: request.imp[%d].ext.prebid.imp.%s was dropped with the reason: invalid bidder"
Expand All @@ -417,7 +425,7 @@ private void validateImpExtPrebidImp(JsonNode imp,

private void validateImpExtPrebidBidder(JsonNode extPrebidBidder,
ExtStoredAuctionResponse storedAuctionResponse,
Map<String, String> aliases,
BidderAliases aliases,
int impIndex,
List<String> warnings) throws ValidationException {
if (extPrebidBidder == null) {
Expand All @@ -433,7 +441,7 @@ private void validateImpExtPrebidBidder(JsonNode extPrebidBidder,
final Map.Entry<String, JsonNode> bidderExtension = bidderExtensions.next();
final String bidder = bidderExtension.getKey();
try {
validateImpBidderExtName(impIndex, bidderExtension, aliases.getOrDefault(bidder, bidder));
validateImpBidderExtName(impIndex, bidderExtension, aliases.resolveBidder(bidder));
} catch (ValidationException ex) {
bidderExtensions.remove();
warnings.add("WARNING: request.imp[%d].ext.prebid.bidder.%s was dropped with a reason: %s"
Expand All @@ -447,7 +455,7 @@ private void validateImpExtPrebidBidder(JsonNode extPrebidBidder,
}

private void validateImpExtPrebidStoredResponses(ExtImpPrebid extPrebid,
Map<String, String> aliases,
BidderAliases aliases,
int impIndex,
List<String> warnings) throws ValidationException {
final ExtStoredAuctionResponse extStoredAuctionResponse = extPrebid.getStoredAuctionResponse();
Expand Down Expand Up @@ -479,8 +487,11 @@ private void validateImpExtPrebidStoredResponses(ExtImpPrebid extPrebid,
}
}

private void validateStoredBidResponse(ExtStoredBidResponse extStoredBidResponse, ObjectNode bidderNode,
Map<String, String> aliases, int impIndex) throws ValidationException {
private void validateStoredBidResponse(ExtStoredBidResponse extStoredBidResponse,
ObjectNode bidderNode,
BidderAliases aliases,
int impIndex) throws ValidationException {

final String bidder = extStoredBidResponse.getBidder();
final String id = extStoredBidResponse.getId();
if (StringUtils.isEmpty(bidder)) {
Expand All @@ -493,7 +504,7 @@ private void validateStoredBidResponse(ExtStoredBidResponse extStoredBidResponse
"Id was not defined for request.imp[%d].ext.prebid.storedbidresponse.id".formatted(impIndex));
}

final String resolvedBidder = aliases.getOrDefault(bidder, bidder);
final String resolvedBidder = aliases.resolveBidder(bidder);

if (!bidderCatalog.isValidName(resolvedBidder)) {
throw new ValidationException(
Expand All @@ -518,8 +529,10 @@ private ExtImpPrebid parseExtImpPrebid(ObjectNode extImpPrebid, int impIndex) th
}
}

private void validateImpBidderExtName(int impIndex, Map.Entry<String, JsonNode> bidderExtension, String bidderName)
throws ValidationException {
private void validateImpBidderExtName(int impIndex,
Map.Entry<String, JsonNode> bidderExtension,
String bidderName) throws ValidationException {

if (bidderCatalog.isValidName(bidderName)) {
final Set<String> messages = bidderParamValidator.validate(bidderName, bidderExtension.getValue());
if (!messages.isEmpty()) {
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/org/prebid/server/validation/ImpValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mock.Strictness.LENIENT;
import static org.mockito.Mockito.verify;

@ExtendWith(MockitoExtension.class)
public class ImpValidatorTest extends VertxTest {
Expand Down Expand Up @@ -1516,6 +1517,42 @@ public void validateImpsShouldReturnWarningMessageAndDropBidderWhenBidderExtIsIn
.containsOnly(mapper.createObjectNode());
}

@Test
public void validateImpsShouldReturnWarningMessageAndDropBidderWhenBidderExtIsInvalidAndBidderIsHardcodedAlias()
throws ValidationException {

// given
final List<Imp> givenImps = singletonList(validImpBuilder()
.ext(mapper.valueToTree(singletonMap("prebid", singletonMap("bidder", singletonMap("someAlias", 0)))))
.build());
given(bidderParamValidator.validate(any(), any()))
.willReturn(new LinkedHashSet<>(asList("errorMessage1", "errorMessage2")));
given(bidderCatalog.isValidName("someAlias")).willReturn(true);

final List<String> debugMessages = new ArrayList<>();

// when
target.validateImps(givenImps, Map.of("someAlias", "rubicon"), debugMessages);

// then
assertThat(debugMessages)
.containsExactly(
"""
WARNING: request.imp[0].ext.prebid.bidder.someAlias was dropped with a reason: \
request.imp[0].ext.prebid.bidder.someAlias failed validation.
errorMessage1
errorMessage2""",
"WARNING: request.imp[0].ext must contain at least one valid bidder");

assertThat(givenImps)
.extracting(Imp::getExt)
.extracting(impExt -> impExt.get("prebid"))
.extracting(prebid -> prebid.get("bidder"))
.containsOnly(mapper.createObjectNode());

verify(bidderParamValidator).validate(eq("someAlias"), any());
}

@Test
public void validateImpsShouldReturnWarningMessageAndDropBidderWhenImpExtPrebidImpBidderIsUnknown()
throws ValidationException {
Expand Down

0 comments on commit d188ada

Please sign in to comment.