Skip to content

Fixes #16 : Handle null values gracefully #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.protobuf.Value;
import com.google.protobuf.util.JsonFormat;
import lombok.extern.slf4j.Slf4j;
import org.hypertrace.config.service.ConfigServiceUtils;
import org.hypertrace.core.documentstore.Document;

import java.io.IOException;
Expand Down Expand Up @@ -124,5 +125,10 @@ public Value deserialize(JsonParser p, DeserializationContext ctxt)
JsonFormat.parser().merge(jsonString, valueBuilder);
return valueBuilder.build();
}

@Override
public Value getNullValue(DeserializationContext ctxt) {
return ConfigServiceUtils.emptyValue();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.google.common.io.Resources;
import com.google.protobuf.NullValue;
import com.google.protobuf.Value;
import com.google.protobuf.util.JsonFormat;
import java.io.IOException;
import java.nio.charset.Charset;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -23,6 +26,15 @@ void convertToAndFromJson() throws IOException {
assertEquals(configDocument, ConfigDocument.fromJson(configDocument.toJson()));
}

@Test
void convertDocumentContainingNullValue() throws IOException {
long timestamp = System.currentTimeMillis();
Value nullValue = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
ConfigDocument configDocument = new ConfigDocument(RESOURCE_NAME, RESOURCE_NAMESPACE,
TENANT_ID, DEFAULT_CONTEXT, 15, "user1", nullValue, timestamp, timestamp);
assertEquals(configDocument, ConfigDocument.fromJson(configDocument.toJson()));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion fails without the above changes in ConfigDocument as in that case, the config document - ConfigDocument.fromJson(configDocument.toJson()) would have config value as null.

}

@Test
@DisplayName("Test backward compatibility using a json string without creation and update timestamps")
void convertJsonStringWithoutTimestamp() throws IOException {
Expand Down