Repository navigation
Add fetchDemandBidInfo to the Mediation API ad units - #1047
Conversation
Banner, interstitial, rewarded and native mediation ad units can now return the full BidInfo; fetchDemand(OnFetchCompleteListener) is unchanged.
There was a problem hiding this comment.
Thanks, this matches the iOS version, and the two points from prebid/prebid-mobile-ios#1426 don't come up here: Android's mediation setup has no failure result, and the keywords in BidInfo are the same set handleKeywordsUpdate receives.
How I checked: I read the four units side by side with BidInfo.create and AdUnit.fetchDemandBidInfo. The new unit tests ran and passed in "Run Unit Tests - Prebid Mobile".
Test app not built. "Build Internal Test App" runs only on release branches or with the run-full-tests label, so it was skipped here and none of the nine PrebidInternalTestApp changes were compiled. Could we add the label once?
YuriyVelichkoPI
left a comment
There was a problem hiding this comment.
@OlenaPostindustria please address the review comments.
mdanylov-sigma
left a comment
There was a problem hiding this comment.
Re-reviewed at 9232000. Thanks for the follow-ups: the null-listener check, the shared toResultCode(), the final rewarded override and the real-NativeAdUnit test are all in, and with run-full-tests the Internal Test App now builds.
One thing I missed in my first pass, and it's the main point here: the native path caches the winning bid twice (inline on MediationNativeAdUnit). The test I asked for doesn't catch it. That one's on my suggestion, see the comment on MediationNativeAdUnitTest. The rest are smaller: the lastBidResponse field (threading, design, retention), the result-code enum, and test hygiene.
Outside the diff: the description says the MAX mediation screens use fetchDemandBidInfo, but MaxNativeFragment still calls nativeAdUnit.fetchDemand(nativeAdLoader), since it goes through NativeAdUnit directly. prebid/prebid-mobile-ios#1426 migrated PrebidMAXNativeController. Either switch it to fetchDemandBidInfo(nativeAdLoader) { ... } or adjust the description. As it stands, the MAX native cache-id path never runs through the new API in the test app.
How I checked: I ran the mediation test classes plus FetchDemandResultTest, AdUnitTest and NativeAdUnitTest locally (testReleaseUnitTest, all 58 pass). I confirmed the double save with a throwaway copy of MediationNativeAdUnitTest that counts CacheManager entries after one fetch: 2, expected 1.
| public void fetchDemandBidInfo( | ||
| @NonNull OnFetchDemandResult listener | ||
| ) { | ||
| nativeAdUnit.fetchDemandBidInfo(adObject, listener); |
There was a problem hiding this comment.
This caches the native winning bid twice. NativeAdUnit's bid listener saves it (CacheManager.save gives id 1, written to the Bundle). Then the wrapper in AdUnit.fetchDemandBidInfo calls BidInfo.create, whose native branch saves it again (id 2), and Util.saveCacheId overwrites the Bundle value. Id 1 is never read and stays in the static CacheManager map until exp or the 5-minute default. fetchDemand saves once.
I confirmed it with a copy of the new MediationNativeAdUnitTest that counts CacheManager.savedValues after one fetch: 2 entries, expected 1. With NativeFeedAdapter, that's one orphaned native JSON per feed slot.
The cause is in the original-API path (AdUnit.fetchDemandBidInfo and fetchDemand(OnFetchDemandResult) do the same for native), so I'd fix it there rather than here. For example, NativeAdUnit could keep the cache id it saved and BidInfo could reuse it instead of caching again.
|
|
||
| /** | ||
| * Loads ad and applies mediation delegate. Like {@link #fetchDemand(OnFetchCompleteListener)}, | ||
| * but returns the full {@link org.prebid.mobile.api.data.BidInfo} (including the price, |
There was a problem hiding this comment.
Related, about the targeting this returns: BidInfo.create replaces hb_cache_id in getTargetingKeywords() with the local Prebid_<uuid> id. But the AdMob Bundle got the server's hb_cache_id from Util.apply, and for a Bundle Util.saveCacheId only writes NativeAdUnitCacheId (same for the MAX loader). So for native, the returned keywords aren't the ones applied to the ad object. My first review said the keywords match; that holds for banner, interstitial and rewarded, not for native. GAM is fine because setCacheIdToGamManager rewrites hb_cache_id there.
Either keep the server value in targetingKeywords for the mediation objects, or document the difference.
| } | ||
|
|
||
| protected void fetchDemand(@NonNull OnFetchCompleteListener listener) { | ||
| lastBidResponse = null; |
There was a problem hiding this comment.
Minor, and only matters if this is called off the main thread. lastBidResponse is a plain field written and read in the BidLoader callbacks on the main thread, and reset here on the caller's thread. Say a banner refresh is in flight and fetchDemandBidInfo is called from a background thread: this reset can land between lastBidResponse = response and the read in the lambda. The app then gets SUCCESS with no price or targeting, even though the keywords were applied. If the API is meant to be main-thread only, @MainThread would say so.
| } | ||
|
|
||
| fetchDemand(result -> listener.onComplete( | ||
| BidInfo.create(result.toResultCode(), lastBidResponse, adUnitConfig) |
There was a problem hiding this comment.
Design point: the BidInfo is built from a field that's read when the FetchDemandResult callback fires, so a success and its bid are tied together only by the call order in bidRequesterListener. Any path that completes with SUCCESS without going through onFetchCompleted first gets an empty or stale BidInfo. That could be an onResponseReceived override (rewarded already has one), a future cached-bid path, or a test.
prebid/prebid-mobile-ios#1426 went the other way: fetchDemand(completion:) is built on fetchDemandBidInfo and reports bidInfo.resultCode. Passing the BidResponse through an internal completion would remove the field, the reset in fetchDemand and this ordering dependency.
| protected BidLoader bidLoader; | ||
|
|
||
| @Nullable | ||
| private BidResponse lastBidResponse; |
There was a problem hiding this comment.
This also keeps the last full BidResponse (seatbids, adm, VAST XML) for as long as the ad unit is reachable, including after the BidInfo is delivered and after destroy(). If the field stays, clearing it in destroy() (and after building the BidInfo) would be enough.
| * Its result code is the {@link ResultCode} matching the {@link FetchDemandResult} that | ||
| * {@code fetchDemand} reports. | ||
| */ | ||
| protected void fetchDemandBidInfo(@NonNull OnFetchDemandResult listener) { |
There was a problem hiding this comment.
Nit on the public surface: the mediation API now takes org.prebid.mobile.api.original.OnFetchDemandResult, whose Javadoc still calls it the listener for the original API's PrebidAdUnit. It reports org.prebid.mobile.ResultCode, while fetchDemand on the same unit reports FetchDemandResult (SERVER_ERROR vs PREBID_SERVER_ERROR). In Java, bidInfo.getResultCode().equals(FetchDemandResult.SUCCESS) compiles and is always false.
Using ResultCode to match iOS is fine with me. Updating the OnFetchDemandResult Javadoc and stating in the mediation docs (the PR has needs_docs) which enum this returns would cover it.
| /** | ||
| * Returns the {@link ResultCode} the original API reports for the same outcome. | ||
| */ | ||
| public ResultCode toResultCode() { |
There was a problem hiding this comment.
Thanks for pulling this out. The inverse mapping (ResultCode to FetchDemandResult) is still a private switch in MediationNativeAdUnit.convertResultCode, so the two directions now live in different files. A FetchDemandResult.fromResultCode(ResultCode) next to this method, covered by a round-trip test, would keep them in sync. Fine as a follow-up.
| assertEquals(Double.valueOf(0.15), bidInfo.getPrice()); | ||
| assertEquals("crid", bidInfo.getCreativeId()); | ||
| assertNotNull(bidInfo.getNativeCacheId()); | ||
| assertEquals(bidInfo.getNativeCacheId(), adObject.getString(NativeAdUnit.BUNDLE_KEY_CACHE_ID)); |
There was a problem hiding this comment.
This is the check I asked for, and I was wrong that it would be enough, sorry. It passes because the second save overwrites the first, so the double caching in MediationNativeAdUnit.fetchDemandBidInfo goes unnoticed. Asserting that CacheManager holds exactly one entry after the fetch (or, once fixed, that the reported id is the one NativeAdUnit's listener saved) would catch it.
The doAnswer stub also skips the account, config and host checks in AdUnit.fetchDemand, so a validation failure such as an empty account id isn't covered for native.
| } No newline at end of file | ||
| @Test | ||
| public void fetchDemandBidInfo_ResponseReceived_ReturnsBidInfo() throws IOException { | ||
| PrebidMobileReflection.setHost("https://test.com"); |
There was a problem hiding this comment.
Nit: setHost mutates the global Host.CUSTOM URL (createCustomHost sets it on the enum constant), but @After only resets the account id. So https://test.com carries over into whatever runs next in the same Robolectric sandbox. The same happens in MediationInterstitialAdUnitTest and MediationRewardedVideoAdUnitTest. MediationBaseAdUnitTest already did this before the PR; restoring the host in @After would keep the outcome independent of test order.
| } | ||
|
|
||
| @Test | ||
| public void fetchDemandBidInfo_ErrorAfterResponse_DoesNotReportPreviousBid() throws IOException { |
There was a problem hiding this comment.
The banner Javadoc says the listener runs again on every refresh, but here the refresh is simulated by calling the captured BidRequesterListener twice. A banner test that fires the real BidLoader refresh timer, with a stubbed BidRequester, would cover the documented behavior. It would also catch a refresh path that skips onFetchCompleted.
mdanylov-sigma
left a comment
There was a problem hiding this comment.
Re-reviewed at 8b70273. Thanks, all points from my last review are addressed. The native bid is cached once, lastBidResponse is gone and the BidInfo now comes from the response that produced it. The docs, tests and MaxNativeFragment are updated too. The related unit tests pass locally (90).
What I found this time is mostly older than this PR: a failed fetch on a reused unit reports the previous bid, plus a few related points. So I opened #1056 for it instead of holding this up.
- AdUnit kept the previous response until the next network callback, so a fetch that failed validation (no network, empty account id) returned the earlier response's keywords, price and request id with the failure code. fetchDemand now clears it first. - BidInfo.getRequestId documented the id as set whenever a response was received, but NO_BIDS and NO_CACHED_BIDS are reported without the response, including a parsed one that filterOutUncachedBids emptied. The doc now states it is set for SUCCESS and null otherwise. Follow-up to #1035. The native double cache save this branch also fixed landed on master with #1047. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Android counterpart of prebid/prebid-mobile-ios#1426.
Changes
MediationBannerAdUnit,MediationInterstitialAdUnit,MediationRewardedVideoAdUnitandMediationNativeAdUnit: new publicfetchDemandBidInfo(OnFetchDemandResult). It sets up the ad object likefetchDemand(OnFetchCompleteListener)and passes the fullBidInfoto the listener.MediationBaseAdUnitkeeps the latest bid response and builds theBidInfofrom it. Its result code is theResultCodematching theFetchDemandResultthatfetchDemandreports.fetchDemand(OnFetchCompleteListener)is unchanged.BidInfocarries only the result code. An error after an earlier success doesn't report the previous bid. Banner auto-refresh calls the listener again on every refresh.MediationNativeAdUnitforwards toNativeAdUnit.fetchDemandBidInfo(adObject, listener).MediationBaseAdUnitTest,MediationBannerAdUnitTest,MediationInterstitialAdUnitTestandMediationRewardedVideoAdUnitTest; newMediationNativeAdUnitTest.PpmNativeFragmentandNativeFeedAdapterusefetchDemandBidInfo.