From 141d376dbd5d8f39094c48e089f5bb915d44a695 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Kalici=C5=84ski?= Date: Tue, 5 Nov 2024 10:38:53 +0100 Subject: [PATCH] Fix for NullPointerException in `pubnub-gson` when grant() is called without authKeys. --- .../access_manager/PNAccessManagerGrantResult.java | 6 ++++++ .../access_manager/PNAccessManagerKeyData.java | 4 +++- .../java/com/pubnub/api/integration/pam/GrantIT.java | 12 ++++++++---- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/pubnub-gson/pubnub-gson-api/src/main/java/com/pubnub/api/java/models/consumer/access_manager/PNAccessManagerGrantResult.java b/pubnub-gson/pubnub-gson-api/src/main/java/com/pubnub/api/java/models/consumer/access_manager/PNAccessManagerGrantResult.java index a9ab93397..afb73add3 100644 --- a/pubnub-gson/pubnub-gson-api/src/main/java/com/pubnub/api/java/models/consumer/access_manager/PNAccessManagerGrantResult.java +++ b/pubnub-gson/pubnub-gson-api/src/main/java/com/pubnub/api/java/models/consumer/access_manager/PNAccessManagerGrantResult.java @@ -39,8 +39,14 @@ public static PNAccessManagerGrantResult from(com.pubnub.api.models.consumer.acc private static Map> from(Map> data) { Map> newMap = new HashMap<>(data.size()); for (Map.Entry> stringMapEntry : data.entrySet()) { + if (stringMapEntry.getValue() == null) { + continue; + } Map innerMap = new HashMap<>(stringMapEntry.getValue().size()); for (Map.Entry innerEntry : stringMapEntry.getValue().entrySet()) { + if (innerEntry.getValue() == null) { + continue; + } innerMap.put(innerEntry.getKey(), PNAccessManagerKeyData.from(innerEntry.getValue())); } newMap.put(stringMapEntry.getKey(), innerMap); diff --git a/pubnub-gson/pubnub-gson-api/src/main/java/com/pubnub/api/java/models/consumer/access_manager/PNAccessManagerKeyData.java b/pubnub-gson/pubnub-gson-api/src/main/java/com/pubnub/api/java/models/consumer/access_manager/PNAccessManagerKeyData.java index 8d889cbd1..cbc5ea7fd 100644 --- a/pubnub-gson/pubnub-gson-api/src/main/java/com/pubnub/api/java/models/consumer/access_manager/PNAccessManagerKeyData.java +++ b/pubnub-gson/pubnub-gson-api/src/main/java/com/pubnub/api/java/models/consumer/access_manager/PNAccessManagerKeyData.java @@ -3,6 +3,7 @@ import com.google.gson.annotations.SerializedName; import lombok.Builder; import lombok.Data; +import org.jetbrains.annotations.NotNull; @Data @Builder(toBuilder = true) @@ -29,7 +30,8 @@ public class PNAccessManagerKeyData { @SerializedName("j") private boolean joinEnabled; - static PNAccessManagerKeyData from(com.pubnub.api.models.consumer.access_manager.PNAccessManagerKeyData data) { + @NotNull + static PNAccessManagerKeyData from(@NotNull com.pubnub.api.models.consumer.access_manager.PNAccessManagerKeyData data) { return PNAccessManagerKeyData.builder() .readEnabled(data.getReadEnabled()) .writeEnabled(data.getWriteEnabled()) diff --git a/pubnub-gson/pubnub-gson-impl/src/integrationTest/java/com/pubnub/api/integration/pam/GrantIT.java b/pubnub-gson/pubnub-gson-impl/src/integrationTest/java/com/pubnub/api/integration/pam/GrantIT.java index 35f1d6554..131e52ab9 100644 --- a/pubnub-gson/pubnub-gson-impl/src/integrationTest/java/com/pubnub/api/integration/pam/GrantIT.java +++ b/pubnub-gson/pubnub-gson-impl/src/integrationTest/java/com/pubnub/api/integration/pam/GrantIT.java @@ -6,18 +6,23 @@ import com.pubnub.api.java.models.consumer.access_manager.PNAccessManagerKeyData; import org.junit.Test; +import java.util.Arrays; import java.util.Collections; import static org.junit.Assert.assertEquals; public class GrantIT extends BaseIntegrationTest { + @Test + public void grantWithoutAuthKeyDoesntCrash() throws PubNubException { + getServer().grant().channels(Arrays.asList("abc")).write(true).read(false).ttl(0).sync(); + } + @Test public void grantAllForUUID() throws PubNubException { String uuid = "uuid123"; String authKey = "authKey123"; int ttl = 120; - PNAccessManagerGrantResult expectedResult = PNAccessManagerGrantResult.builder() .channelGroups(Collections.emptyMap()) .channels(Collections.emptyMap()) @@ -35,7 +40,7 @@ public void grantAllForUUID() throws PubNubException { .build()))) .build(); - PNAccessManagerGrantResult result = getServer() + PNAccessManagerGrantResult result2 = getServer() .grant() .uuids(Collections.singletonList(uuid)) .authKeys(Collections.singletonList(authKey)) @@ -44,7 +49,6 @@ public void grantAllForUUID() throws PubNubException { .update(true) .delete(true) .sync(); - System.out.println(result); - assertEquals(expectedResult, result); + assertEquals(expectedResult, result2); } }