Skip to content

Commit 12e4bd6

Browse files
committed
Added a check to GSON serializer to behave consistently with all other serializers
1 parent 5558e3b commit 12e4bd6

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

core-api/src/main/java/com/optimizely/ab/config/parser/GsonConfigParser.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ final class GsonConfigParser implements ConfigParser {
3333

3434
@Override
3535
public ProjectConfig parseProjectConfig(@Nonnull String json) throws ConfigParseException {
36+
if (json == null) {
37+
throw new ConfigParseException("Unable to parse null datafile.");
38+
}
39+
if (json.length() == 0) {
40+
throw new ConfigParseException("Unable to parse empty datafile.");
41+
}
3642
Gson gson = new GsonBuilder()
3743
.registerTypeAdapter(ProjectConfig.class, new ProjectConfigGsonDeserializer())
3844
.registerTypeAdapter(Audience.class, new AudienceGsonDeserializer())

core-api/src/test/java/com/optimizely/ab/config/parser/GsonConfigParserTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import org.junit.Rule;
2323
import org.junit.Test;
2424
import org.junit.rules.ExpectedException;
25-
import static junit.framework.Assert.assertNull;
26-
2725

2826
import static com.optimizely.ab.config.ProjectConfigTestUtils.validConfigJsonV1;
2927
import static com.optimizely.ab.config.ProjectConfigTestUtils.validProjectConfigV1;
@@ -92,22 +90,24 @@ public void validJsonRequiredFieldMissingExceptionWrapping() throws Exception {
9290

9391
/**
9492
* Verify that empty string JSON results in a {@link ConfigParseException} being thrown.
95-
* This serializer doesn't throw exceptions on null or empty string
9693
*/
9794
@Test
9895
public void emptyJsonExceptionWrapping() throws Exception {
96+
thrown.expect(ConfigParseException.class);
97+
9998
GsonConfigParser parser = new GsonConfigParser();
100-
assertNull(parser.parseProjectConfig(""));
99+
parser.parseProjectConfig("");
101100
}
102101

103102
/**
104103
* Verify that null JSON results in a {@link ConfigParseException} being thrown.
105-
* This serializer doesn't throw exceptions on null or empty string
106104
*/
107105
@Test
108106
@SuppressFBWarnings(value="NP_NONNULL_PARAM_VIOLATION", justification="Testing nullness contract violation")
109107
public void nullJsonExceptionWrapping() throws Exception {
108+
thrown.expect(ConfigParseException.class);
109+
110110
GsonConfigParser parser = new GsonConfigParser();
111-
assertNull(parser.parseProjectConfig(null));
111+
parser.parseProjectConfig(null);
112112
}
113113
}

0 commit comments

Comments
 (0)