Skip to content

Commit

Permalink
Deserialize null capabilities in Device entities as empty sets
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-signal authored Nov 1, 2024
1 parent fc0a7b7 commit c9a396b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -178,6 +179,7 @@ public Set<DeviceCapability> getCapabilities() {
return capabilities;
}

@JsonSetter
public void setCapabilities(@Nullable final Set<DeviceCapability> capabilities) {
this.capabilities = (capabilities == null || capabilities.isEmpty())
? Collections.emptySet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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");
}
}

}

0 comments on commit c9a396b

Please sign in to comment.