Skip to content

Commit 7d16f93

Browse files
Jiaming Youmeta-codesync[bot]
authored andcommitted
Enable ParamBuilder.processRequestFromContext now that 1.3.0 is pinned
Summary: # Problem `Event#setRequestContext` constructed a `ParamBuilder` but deliberately did NOT call `paramBuilder.processRequestFromContext(context)`, because that method was not in the Maven Central release the SDK pinned (`1.1.1`) — calling it would have been a `NoSuchMethodError`. With the call deferred, `ParamBuilder` was never given the request context, so `applyParamBuilderDefaults()` was a runtime no-op: fbc / fbp / event_source_url / referrer_url were never extracted from the request, even though the wiring exists. The `1.3.0` bump (D106968654) now pins a Maven Central release that publishes `processRequestFromContext`, `getEventSourceUrl`, and `getReferrerUrl`, so the call can be enabled. # Solution - `Event#setRequestContext` now calls `this.paramBuilder.processRequestFromContext(context)` (the deferral NOTE is removed). Extraction into `UserData` / `Event` still happens in `applyParamBuilderDefaults()` at send time, so call order with `setUserData` is unchanged. - The real-builder guardrail `testRealParamBuilderExposesMethodsTheSdkCalls` now also asserts `processRequestFromContext(Object)`, `getEventSourceUrl()`, and `getReferrerUrl()` exist on the pinned artifact (in addition to `getFbc`/`getFbp`), so a future upstream API shift fails CI loudly. - Replaced the deferred no-op test `testSetRequestContextStoresParamBuilderWithoutContextProcessing` with an end-to-end test `testSetRequestContextPopulatesFbpFromCookieEndToEnd` that drives a real (non-mocked) `ParamBuilder` and asserts `fbp` is auto-populated from the `_fbp` cookie. The new test uses the Java `RequestContextAdaptor` Map strategy's flat CGI/environ-style keys (`HTTP_HOST`, `HTTP_COOKIE`) — NOT the nested `{"headers": {...}}` shape the old deferred test used, which the adaptor does not read. `ParamBuilder` appends an appendix token to a 4-segment fbp, so the assertion uses `startsWith` rather than exact equality. The mocked tests are unaffected: `MockedConstruction` intercepts `new ParamBuilder()`, and `processRequestFromContext` on the mock is a no-op, so the stubbed `getFbc` / `getFbp` / `getEventSourceUrl` / `getReferrerUrl` values still drive `applyParamBuilderDefaults`. Differential Revision: D106975772 fbshipit-source-id: e52ec5576def6a4356bee2508ad16f7bd2d56c35
1 parent 5441573 commit 7d16f93

2 files changed

Lines changed: 29 additions & 40 deletions

File tree

src/main/java/com/facebook/ads/sdk/serverside/Event.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -661,13 +661,7 @@ public Event setRequestContext(Object context, Preference preference) {
661661
this.requestContext = context;
662662
this.preference = preference != null ? preference : new Preference();
663663
this.paramBuilder = new ParamBuilder();
664-
// NOTE: paramBuilder.processRequestFromContext(context) will be invoked
665-
// here once the Maven Central release of com.facebook.capi.sdk:capi-param-builder
666-
// includes that method. It exists in fbsource HEAD but is not yet published
667-
// to Maven Central — calling it now breaks the build (NoSuchMethodError at
668-
// compile time). Until the upstream artifact is republished and the version
669-
// pin in pom.xml is bumped, ParamBuilder remains uninitialized with context
670-
// and applyParamBuilderDefaults() is effectively a no-op.
664+
this.paramBuilder.processRequestFromContext(context);
671665
return this;
672666
}
673667

src/test/java/com/facebook/ads/sdk/serverside/EventRequestContextTest.java

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -402,57 +402,52 @@ public void testApplyParamBuilderDefaultsIsIdempotent() {
402402
@Test
403403
public void testRealParamBuilderExposesMethodsTheSdkCalls() {
404404
// Guardrail against the SDK <-> upstream Maven artifact drifting apart.
405-
// Only checks the methods that Event#applyParamBuilderDefaults currently
406-
// calls today — getFbc + getFbp. processRequestFromContext is NOT
407-
// checked yet because the Maven Central release lags fbsource HEAD for
408-
// that method; Event#setRequestContext therefore does not invoke it yet
409-
// (see the NOTE in Event#setRequestContext). Add the
410-
// processRequestFromContext assertion in the follow-up diff that bumps
411-
// the pom pin and re-introduces the call.
405+
// Checks every ParamBuilder method the SDK calls against the pinned 1.3.0
406+
// artifact: processRequestFromContext (Event#setRequestContext) and
407+
// getFbc / getFbp / getEventSourceUrl / getReferrerUrl
408+
// (Event#applyParamBuilderDefaults). If the upstream API shifts, bump the
409+
// pinned Maven version in pom.xml.
412410
ParamBuilder pb = new ParamBuilder();
413411
try {
412+
ParamBuilder.class.getMethod("processRequestFromContext", Object.class);
414413
ParamBuilder.class.getMethod("getFbc");
415414
ParamBuilder.class.getMethod("getFbp");
415+
ParamBuilder.class.getMethod("getEventSourceUrl");
416+
ParamBuilder.class.getMethod("getReferrerUrl");
416417
} catch (NoSuchMethodException e) {
417-
fail("capi-param-builder must expose getFbc / getFbp; "
418-
+ "Event.applyParamBuilderDefaults calls them. Bump the pinned "
419-
+ "Maven version in pom.xml if the upstream API has shifted. Missing: "
418+
fail("capi-param-builder must expose processRequestFromContext / getFbc / "
419+
+ "getFbp / getEventSourceUrl / getReferrerUrl; Event calls them. Bump the "
420+
+ "pinned Maven version in pom.xml if the upstream API has shifted. Missing: "
420421
+ e.getMessage());
421422
}
422423
assertNotNull(pb);
423424
}
424425

425426
@Test
426-
public void testSetRequestContextStoresParamBuilderWithoutContextProcessing() {
427-
// Real ParamBuilder, no mocking. The SDK currently defers calling
428-
// ParamBuilder.processRequestFromContext until the Maven Central release
429-
// catches up with fbsource HEAD (see Event#setRequestContext). Until
430-
// then, applyParamBuilderDefaults is effectively a no-op for fbc/fbp
431-
// because the builder was never given any context to extract from —
432-
// getFbc / getFbp return null.
427+
public void testSetRequestContextPopulatesFbpFromCookieEndToEnd() {
428+
// Real ParamBuilder, NO mocking. Event#setRequestContext now invokes
429+
// paramBuilder.processRequestFromContext(context), so a real request
430+
// context flows end-to-end: the _fbp cookie is extracted by the upstream
431+
// 1.3.0 ParamBuilder and lands on UserData during applyParamBuilderDefaults.
433432
//
434-
// When the upstream pin is bumped and the call is re-added in a
435-
// follow-up diff, this test should be replaced with the end-to-end
436-
// populate-from-cookie assertion that's currently captured in the
437-
// RealParamBuilderSmoke companion script (see test plan for D105523324).
438-
Map<String, Object> headers = new HashMap<>();
439-
headers.put("host", "shop.example.com");
440-
headers.put("cookie", "_fbp=fb.1.1700000000000.987654321");
441-
Map<String, Object> request = new HashMap<>();
442-
request.put("headers", headers);
443-
request.put("url", "/");
433+
// The Java RequestContextAdaptor's Map strategy reads flat CGI/environ-style
434+
// keys (HTTP_HOST, HTTP_COOKIE, ...), NOT a nested "headers" map.
435+
Map<String, Object> context = new HashMap<>();
436+
context.put("HTTP_HOST", "shop.example.com");
437+
context.put("HTTP_COOKIE", "_fbp=fb.1.1700000000000.987654321");
444438

445439
Event event = new Event()
446440
.eventName("PageView")
447441
.eventTime(1700000000L)
448-
.setRequestContext(request);
442+
.setRequestContext(context);
449443

450444
event.applyParamBuilderDefaults();
451-
// No fbc/fbp expected — the SDK isn't yet calling processRequestFromContext.
452445
UserData ud = event.getUserData();
453-
if (ud != null) {
454-
assertNull("fbc must not be populated until the SDK calls processRequestFromContext", ud.getFbc());
455-
assertNull("fbp must not be populated until the SDK calls processRequestFromContext", ud.getFbp());
456-
}
446+
assertNotNull(ud);
447+
// ParamBuilder appends an appendix token to a 4-segment fbp, so assert on
448+
// the prefix rather than exact equality.
449+
assertNotNull("fbp should be auto-populated from the _fbp cookie", ud.getFbp());
450+
assertTrue("fbp should start with the cookie value",
451+
ud.getFbp().startsWith("fb.1.1700000000000.987654321"));
457452
}
458453
}

0 commit comments

Comments
 (0)