Skip to content

Commit

Permalink
Fix asset missing id resolving (#1777)
Browse files Browse the repository at this point in the history
  • Loading branch information
Compile-Ninja authored and Serhii Nahornyi committed Mar 22, 2022
1 parent fdfebf1 commit 055a80d
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1335,8 +1335,9 @@ private static void setAssetTypes(Asset responseAsset, List<com.iab.openrtb.requ
if (type != null) {
responseAsset.getImg().setType(type);
} else {
throw new PreBidException(String.format("Response has an Image asset with ID:%s present that doesn't "
+ "exist in the request", responseAsset.getId()));
final Integer assetId = responseAsset.getId();
throw new PreBidException(String.format("Response has an Image asset with ID:'%s' present that doesn't "
+ "exist in the request", assetId != null ? assetId : StringUtils.EMPTY));
}
}
if (responseAsset.getData() != null) {
Expand All @@ -1351,11 +1352,11 @@ private static void setAssetTypes(Asset responseAsset, List<com.iab.openrtb.requ
}
}

private static com.iab.openrtb.request.Asset getAssetById(int assetId,
private static com.iab.openrtb.request.Asset getAssetById(Integer assetId,
List<com.iab.openrtb.request.Asset> requestAssets) {

return requestAssets.stream()
.filter(asset -> asset.getId() == assetId)
.filter(asset -> Objects.equals(assetId, asset.getId()))
.findFirst()
.orElse(com.iab.openrtb.request.Asset.EMPTY);
}
Expand Down
120 changes: 120 additions & 0 deletions src/test/java/org/prebid/server/auction/BidResponseCreatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,126 @@ public void shouldReturnEmptyAssetIfImageTypeIsEmpty() throws JsonProcessingExce
.isEmpty();
}

@Test
public void shouldReturnEmptyAssetIfNoRelatedNativeAssetFound() throws JsonProcessingException {
// given
final Request nativeRequest = Request.builder()
.assets(singletonList(Asset.builder()
.id(null)
.img(ImageObject.builder().type(null).build())
.data(DataObject.builder().type(2).build())
.build()))
.build();

final BidRequest bidRequest = BidRequest.builder()
.cur(singletonList("USD"))
.tmax(1000L)
.app(App.builder().build())
.imp(singletonList(Imp.builder()
.id(IMP_ID)
.xNative(Native.builder().request(mapper.writeValueAsString(nativeRequest)).build())
.build()))
.build();

final AuctionContext auctionContext = givenAuctionContext(bidRequest);

final Response responseAdm = Response.builder()
.assets(singletonList(com.iab.openrtb.response.Asset.builder()
.id(123)
.img(com.iab.openrtb.response.ImageObject.builder().type(null).build())
.data(com.iab.openrtb.response.DataObject.builder().build())
.build()))
.build();

final Bid bid = Bid.builder()
.id("bidId")
.price(BigDecimal.ONE)
.impid(IMP_ID)
.adm(mapper.writeValueAsString(responseAdm))
.ext(mapper.valueToTree(singletonMap("bidExt", 1)))
.build();
final List<BidderResponse> bidderResponses = singletonList(BidderResponse.of("bidder1",
givenSeatBid(BidderBid.of(bid, xNative, "USD")), 100));

// when
final BidResponse bidResponse =
bidResponseCreator.create(toAuctionParticipant(bidderResponses), auctionContext, CACHE_INFO, MULTI_BIDS)
.result();

// then

assertThat(bidResponse.getSeatbid()).hasSize(1)
.flatExtracting(SeatBid::getBid)
.extracting(Bid::getAdm)
.extracting(adm -> mapper.readValue(adm, Response.class))
.flatExtracting(Response::getAssets)
.isEmpty();
assertThat(bidResponse.getExt())
.extracting(ExtBidResponse::getErrors)
.isEqualTo(Map.of("bidder1", singletonList(ExtBidderError.of(3,
"Response has an Image asset with ID:'123' present that doesn't exist in the request"))));
}

@Test
public void shouldReturnEmptyAssetIfIdIsNotPresentRelatedNativeAssetFound() throws JsonProcessingException {
// given
final Request nativeRequest = Request.builder()
.assets(singletonList(Asset.builder()
.id(123)
.img(ImageObject.builder().type(null).build())
.data(DataObject.builder().type(2).build())
.build()))
.build();

final BidRequest bidRequest = BidRequest.builder()
.cur(singletonList("USD"))
.tmax(1000L)
.app(App.builder().build())
.imp(singletonList(Imp.builder()
.id(IMP_ID)
.xNative(Native.builder().request(mapper.writeValueAsString(nativeRequest)).build())
.build()))
.build();

final AuctionContext auctionContext = givenAuctionContext(bidRequest);

final Response responseAdm = Response.builder()
.assets(singletonList(com.iab.openrtb.response.Asset.builder()
.id(null)
.img(com.iab.openrtb.response.ImageObject.builder().type(null).build())
.data(com.iab.openrtb.response.DataObject.builder().build())
.build()))
.build();

final Bid bid = Bid.builder()
.id("bidId")
.price(BigDecimal.ONE)
.impid(IMP_ID)
.adm(mapper.writeValueAsString(responseAdm))
.ext(mapper.valueToTree(singletonMap("bidExt", 1)))
.build();
final List<BidderResponse> bidderResponses = singletonList(BidderResponse.of("bidder1",
givenSeatBid(BidderBid.of(bid, xNative, "USD")), 100));

// when
final BidResponse bidResponse =
bidResponseCreator.create(toAuctionParticipant(bidderResponses), auctionContext, CACHE_INFO, MULTI_BIDS)
.result();

// then

assertThat(bidResponse.getSeatbid()).hasSize(1)
.flatExtracting(SeatBid::getBid)
.extracting(Bid::getAdm)
.extracting(adm -> mapper.readValue(adm, Response.class))
.flatExtracting(Response::getAssets)
.isEmpty();
assertThat(bidResponse.getExt())
.extracting(ExtBidResponse::getErrors)
.isEqualTo(Map.of("bidder1", singletonList(ExtBidderError.of(3,
"Response has an Image asset with ID:'' present that doesn't exist in the request"))));
}

@Test
public void shouldReturnEmptyAssetIfDataTypeIsEmpty() throws JsonProcessingException {
// given
Expand Down

0 comments on commit 055a80d

Please sign in to comment.