Skip to content

Commit c146f09

Browse files
committed
Fix feature flagging parser coverage
1 parent 0984370 commit c146f09

3 files changed

Lines changed: 9 additions & 7 deletions

File tree

products/feature-flagging/feature-flagging-lib/src/main/java/com/datadog/featureflag/JsonApiUfcResponseParser.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.datadog.featureflag;
22

3-
import com.squareup.moshi.JsonDataException;
43
import com.squareup.moshi.JsonReader;
54
import datadog.trace.api.featureflag.ufc.v1.ServerConfiguration;
65
import java.io.ByteArrayInputStream;
@@ -87,8 +86,7 @@ private static ServerConfiguration validConfiguration(
8786
}
8887

8988
private static void requireEndOfDocument(final JsonReader reader) throws IOException {
90-
if (reader.peek() != JsonReader.Token.END_DOCUMENT) {
91-
throw new JsonDataException("JSON document was not fully consumed");
92-
}
89+
// A strict JsonReader throws if another top-level value follows the parsed document.
90+
reader.peek();
9391
}
9492
}

products/feature-flagging/feature-flagging-lib/src/main/java/com/datadog/featureflag/UniversalFlagConfigParser.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ ServerConfiguration parse(final JsonReader reader) throws IOException {
5151
}
5252

5353
private static void requireEndOfDocument(final JsonReader reader) throws IOException {
54-
if (reader.peek() != JsonReader.Token.END_DOCUMENT) {
55-
throw new JsonDataException("JSON document was not fully consumed");
56-
}
54+
// A strict JsonReader throws if another top-level value follows the parsed document.
55+
reader.peek();
5756
}
5857

5958
static final class FlagMapAdapter extends JsonAdapter<Map<String, Flag>> {

products/feature-flagging/feature-flagging-lib/src/test/java/com/datadog/featureflag/JsonApiUfcResponseParserTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ void rejectsUnexpectedJsonApiType() throws Exception {
4242
assertNull(parse("{\"data\":{\"type\":\"other-type\",\"attributes\":" + emptyConfig() + "}}"));
4343
}
4444

45+
@Test
46+
void rejectsNonStringJsonApiType() throws Exception {
47+
assertNull(parse("{\"data\":{\"type\":null,\"attributes\":" + emptyConfig() + "}}"));
48+
}
49+
4550
@Test
4651
void rejectsConfigurationWithoutFlags() throws Exception {
4752
assertNull(

0 commit comments

Comments
 (0)