Skip to content

Commit 527ceb4

Browse files
authored
Handle null values gracefully (#34)
1 parent 620171f commit 527ceb4

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

config-service-impl/src/main/java/org/hypertrace/config/service/store/ConfigDocument.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.google.protobuf.Value;
1818
import com.google.protobuf.util.JsonFormat;
1919
import lombok.extern.slf4j.Slf4j;
20+
import org.hypertrace.config.service.ConfigServiceUtils;
2021
import org.hypertrace.core.documentstore.Document;
2122

2223
import java.io.IOException;
@@ -124,5 +125,10 @@ public Value deserialize(JsonParser p, DeserializationContext ctxt)
124125
JsonFormat.parser().merge(jsonString, valueBuilder);
125126
return valueBuilder.build();
126127
}
128+
129+
@Override
130+
public Value getNullValue(DeserializationContext ctxt) {
131+
return ConfigServiceUtils.emptyValue();
132+
}
127133
}
128134
}

config-service-impl/src/test/java/org/hypertrace/config/service/store/ConfigDocumentTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import static org.junit.jupiter.api.Assertions.assertEquals;
99

1010
import com.google.common.io.Resources;
11+
import com.google.protobuf.NullValue;
12+
import com.google.protobuf.Value;
13+
import com.google.protobuf.util.JsonFormat;
1114
import java.io.IOException;
1215
import java.nio.charset.Charset;
1316
import org.junit.jupiter.api.DisplayName;
@@ -23,6 +26,15 @@ void convertToAndFromJson() throws IOException {
2326
assertEquals(configDocument, ConfigDocument.fromJson(configDocument.toJson()));
2427
}
2528

29+
@Test
30+
void convertDocumentContainingNullValue() throws IOException {
31+
long timestamp = System.currentTimeMillis();
32+
Value nullValue = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
33+
ConfigDocument configDocument = new ConfigDocument(RESOURCE_NAME, RESOURCE_NAMESPACE,
34+
TENANT_ID, DEFAULT_CONTEXT, 15, "user1", nullValue, timestamp, timestamp);
35+
assertEquals(configDocument, ConfigDocument.fromJson(configDocument.toJson()));
36+
}
37+
2638
@Test
2739
@DisplayName("Test backward compatibility using a json string without creation and update timestamps")
2840
void convertJsonStringWithoutTimestamp() throws IOException {

0 commit comments

Comments
 (0)