From c9a396b9e3466aaea5334e2fc6136180020ab0b6 Mon Sep 17 00:00:00 2001 From: Jon Chambers <63609320+jon-signal@users.noreply.github.com> Date: Fri, 1 Nov 2024 16:48:56 -0400 Subject: [PATCH] Deserialize null capabilities in `Device` entities as empty sets --- .../textsecuregcm/storage/Device.java | 2 ++ .../textsecuregcm/storage/DeviceTest.java | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/Device.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/Device.java index 0d4e1533c..5e674a9ae 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/Device.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/Device.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import java.time.Duration; @@ -178,6 +179,7 @@ public Set getCapabilities() { return capabilities; } + @JsonSetter public void setCapabilities(@Nullable final Set capabilities) { this.capabilities = (capabilities == null || capabilities.isEmpty()) ? Collections.emptySet() diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/storage/DeviceTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/storage/DeviceTest.java index ca0aa849b..e72f80fd0 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/storage/DeviceTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/storage/DeviceTest.java @@ -6,11 +6,15 @@ package org.whispersystems.textsecuregcm.storage; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import java.time.Duration; import java.time.Instant; +import com.fasterxml.jackson.core.JsonProcessingException; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; +import org.whispersystems.textsecuregcm.util.SystemMapper; class DeviceTest { @@ -42,4 +46,25 @@ public void testIsExpired(final boolean primary, final Duration timeSinceLastSee assertEquals(expectExpired, device.isExpired()); } + @Test + void deserializeCapabilities() throws JsonProcessingException { + { + final Device device = SystemMapper.jsonMapper().readValue(""" + { + "capabilities": null + } + """, Device.class); + + assertNotNull(device.getCapabilities(), + "Device deserialization should populate null capabilities with an empty set"); + } + + { + final Device device = SystemMapper.jsonMapper().readValue("{}", Device.class); + + assertNotNull(device.getCapabilities(), + "Device deserialization should populate null capabilities with an empty set"); + } + } + }