You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "lhotari (via GitHub)" <gi...@apache.org> on 2026/08/31 10:08:31 UTC

Re: [PR] [fix][admin] Map empty content to null [pulsar]

lhotari commented on code in PR #22008:
URL: https://github.com/apache/pulsar/pull/22008#discussion_r3893491350


##########
pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/BaseResource.java:
##########
@@ -365,6 +381,10 @@ abstract static class FutureCallback<T> implements InvocationCallback<T> {
 
         @Override
         public void completed(T value) {
+            if ("".equals(value)) {

Review Comment:
   **[BUG] the empty->null rule sits below the layer that can tell "no body" from "an empty value", and turns it into a blanket rule over every String admin getter**
   
   This is the only hunk in the PR that fixes the reported symptom. `getNamespaceResourceGroup` reaches the client through `NamespacesImpl:1965` -> `asyncGetNamespaceParts(new FutureCallback<String>(){})` -> `BaseResource:304` -> `request(target).async().get(callback)`, so hunks 1-3 never run for it. I confirmed that by applying your test to master, where it fails with `expected [null] but found []`.
   
   The objection is placement and breadth, not intent.
   
   **Placement.** By the time `completed(T value)` runs the `Response` is gone, and with it `hasEntity()` — the one predicate that separates "the server sent no body" from "the server sent an empty string". `"".equals(value)` collapses both. On master `GET .../resourcegroup` answers `200` with `hasEntity=false` and `Content-Length: 0` (measured against a live broker), so the distinction is available here and is being discarded.
   
   **Breadth.** `FutureCallback` is only ever passed to `asyncGetRequest`, and `"".equals` can only fire for `String`, so the surface is the eleven `FutureCallback<String>` sites — but that still includes `BrokerStatsImpl` (`getMetrics`, `getMBeans`, `getTopics`, `getPendingBookieOpsStats`), `BrokersImpl.getVersion`, `LookupImpl.getBundleRange` and `TopicsImpl.getInternalInfo`, where an empty body means *malformed*, not *absent*. Against an in-tree broker those always carry a body, but a proxy or a third-party server that answers a bodiless 200 would now hand the caller `null` where it used to hand back `""`.
   
   **Suggested shape — the one the codebase already uses.** `getPropertyAsync` solves exactly this problem per call site at `NamespacesImpl:1949`:
   
   ```java
   return asyncGetNamespaceParts(new FutureCallback<String>() { }, namespace, "resourcegroup")
           .thenApply(value -> StringUtils.isEmpty(value) ? null : value);
   ```
   
   Applying that to `getNamespaceResourceGroupAsync` (and, if you want them, `getNamespaceAntiAffinityGroupAsync` and `getReplicationConfigVersionAsync`) fixes the reported bug, keeps the change to a list you choose deliberately, and leaves every other String API untouched. If you would rather fix it centrally, do it where the `Response` is still in scope — give the `FutureCallback` family the same `Response`-aware handling the `Function<Response,T>` family has — but that is the version that needs the compatibility note.
   
   **Alternative worth considering,** since it removes the ambiguity at the source: have the resource emit a real no-content response. `Response.noContent().build()` is already idiomatic here (`AdminResource:654`, `PackagesBase:89/134/143`, `TenantsBase:156/201`). `asyncResponse.resume(<a null value>)` is what produces `200 + Content-Length: 0`; a *synchronous* JAX-RS method returning `null` would have produced `204`. That asymmetry is the root cause, and it is exactly the Jersey code your description links to.



##########
pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/BaseResource.java:
##########
@@ -206,7 +218,11 @@ public void completed(Response response) {
                             future.completeExceptionally(getApiException(response));
                         } else {
                             try {
-                                future.complete(readResponse.apply(response));
+                                if (response.hasEntity()) {

Review Comment:
   **[QUALITY] this hunk collides with #25124's 204 branch — keep hasEntity(), it strictly dominates the status check**
   
   Master now reads, in this exact block (from your later #25124, `bf98773a2d3`):
   
   ```java
   int status = response.getStatus();
   if (status != 200 && status != 204) { future.completeExceptionally(getApiException(response)); }
   else if (status == 204) { future.complete(null); }
   else { future.complete(readResponse.apply(response)); }
   ```
   
   so this hunk conflicts. Worth being explicit about which side survives: **`hasEntity()` strictly dominates the 204 check.** Measured on master with WireMock, `asyncGetRequest(…, String.class)` returns `null` for a 204 but still `""` for a `200` with an empty body — and `200 + Content-Length: 0` is what the namespace endpoints actually send (verified against a live broker). #25124 fixed the narrower shape and left the one that occurs. Please keep both conditions rather than reverting this block to the PR's original 200-only form.
   
   Two things to weigh while you are here:
   
   - `hasEntity()` is safe to call before `readEntity()` in Jersey — it peeks and pushes back rather than consuming, and returns false for both `Content-Length: 0` and a completed empty chunked response. But JAX-RS itself permits a provider to answer `true` for zero-length content when content headers are present, so this is provider-specific behaviour worth a comment at the call site.
   - `ScalableTopicsImpl:384` does `asyncGetRequest(path, Long.class)` for `CompletableFuture<Long>`. Master already completes that with `null` on a 204; `hasEntity()` widens it to `200 + Content-Length: 0`, so a caller unboxing the result gets an NPE where a malformed response previously surfaced as a `PulsarAdminException`. Pre-existing rather than introduced here, but it argues for the null mapping being a deliberate per-type decision.



##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminRestTest.java:
##########
@@ -101,6 +103,12 @@ private String parseResponseEntity(Object entity) throws Exception {
         return stringBuilder.toString();
     }
 
+    @Test
+    protected void testMapEmptyContentToNull() throws PulsarAdminException {

Review Comment:
   **[QUALITY] one assertion in a broker integration test, where a WireMock unit test for exactly this now exists**
   
   The test is correct and it does fail on master today — I ran it there to confirm the bug is still live. Two notes on where it belongs and what it should cover.
   
   Master now ships `pulsar-client-admin/src/test/java/org/apache/pulsar/client/admin/internal/AsyncGetRequestTest.java` (WireMock, added by your #25124). That is the natural home: it can pin, in milliseconds and for **both** request families, the shapes that actually matter — `204`, `200 + Content-Length: 0`, `200` with a genuine `""` body, and a non-empty body. A single broker-integration assertion covers one cell of that grid and none of the transport behaviour the production change alters.
   
   The coverage that would be load-bearing is the negative kind: nothing pins the call sites this rule should *not* change. `AdminApi2Test.namespaceAntiAffinity:1228,1232` asserts `isBlank(getNamespaceAntiAffinityGroup(ns))`, and `StringUtils.isBlank(null)` is `true`, so it passes before and after — the suite is silent in both directions, and a later accidental widening of the empty->null rule would go unnoticed.
   
   (The `protected` modifier is fine — TestNG collects and runs it; I saw it execute and fail on master. No change needed there.)



##########
pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/BaseResource.java:
##########
@@ -189,11 +189,23 @@ public <T> CompletableFuture<T> asyncGetRequest(final WebTarget target, FutureCa
     }
 
     protected <T> CompletableFuture<T> asyncGetRequest(final WebTarget target, Class<? extends T> type) {
-        return asyncGetRequest(target, response -> response.readEntity(type));
+        return asyncGetRequest(target, response -> {
+            if (response != null) {

Review Comment:
   **[QUALITY] the two `if (response != null)` guards are unreachable dead code**
   
   This guard and the identical one in the `GenericType` overload below (line 203) can never take the `else` branch. The lambda is only ever invoked as `readResponse.apply(response)` inside `InvocationCallback<Response>.completed(Response response)`, which has already dereferenced `response.getStatus()` (line 217) and `response.hasEntity()` (line 221) before reaching it. If JAX-RS ever did call `completed(null)`, execution would fail at `response.getStatus()` before either guard could run — so they add no protection, only noise.
   
   Both overloads can go back to their one-line form; the `hasEntity()` check in the private method does all the work.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org