Skip to content

Commit

Permalink
Update jackson to 2.9.9 and allow trailing comma in GELF Input (#6325)
Browse files Browse the repository at this point in the history
* Allow trailing comma in Gelf JSON parser

* Update jackson to 2.9.9

* Keep exsiting behavior when using objectMapper.readTree() with empty values

FasterXML/jackson-databind#2211
- In Jackson <2.9.0 an IOException was raised
- In Jackson >=2.9.0, null is returned
  • Loading branch information
mpfz0r authored and bernd committed Aug 26, 2019
1 parent ccbcee5 commit 786c341
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public Message decode(@Nonnull RawMessage rawMessage) {
final JsonNode event;
try {
event = objectMapper.readTree(payload);
if (event == null) {
throw new IOException("null result");
}
} catch (IOException e) {
LOG.error("Couldn't decode raw message {}", rawMessage);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ public JsonParse(ObjectMapper objectMapper) {
public JsonNode evaluate(FunctionArgs args, EvaluationContext context) {
final String value = valueParam.required(args, context);
try {
return objectMapper.readTree(value);
final JsonNode node = objectMapper.readTree(value);
if (node == null) {
throw new IOException("null result");
}
return node;
} catch (IOException e) {
log.warn("Unable to parse JSON", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

Expand All @@ -62,7 +63,9 @@ public class GelfCodec extends AbstractCodec {
public GelfCodec(@Assisted Configuration configuration, GelfChunkAggregator aggregator) {
super(configuration);
this.aggregator = aggregator;
this.objectMapper = new ObjectMapper().enable(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS);
this.objectMapper = new ObjectMapper().enable(
JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS,
JsonParser.Feature.ALLOW_TRAILING_COMMA);
this.decompressSizeLimit = configuration.getInt(CK_DECOMPRESS_SIZE_LIMIT, DEFAULT_DECOMPRESS_SIZE_LIMIT);
}

Expand Down Expand Up @@ -125,6 +128,9 @@ public Message decode(@Nonnull final RawMessage rawMessage) {

try {
node = objectMapper.readTree(json);
if (node == null) {
throw new IOException("null result");
}
} catch (final Exception e) {
log.error("Could not parse JSON, first 400 characters: " +
StringUtils.abbreviate(json, 403), e);
Expand Down
3 changes: 3 additions & 0 deletions graylog2-server/src/main/java/org/graylog2/plugin/Tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,9 @@ public static Optional<AbsoluteRange> extractHistogramBoundaries(final String qu
try {
final JsonParser jp = OBJECT_MAPPER.getFactory().createParser(query);
final JsonNode rootNode = OBJECT_MAPPER.readTree(jp);
if (rootNode == null) {
throw new IOException("null result");
}
final JsonNode timestampNode = rootNode.findValue("range").findValue("timestamp");
final String from = elasticSearchTimeFormatToISO8601(timestampNode.findValue("from").asText());
final String to = elasticSearchTimeFormatToISO8601(timestampNode.findValue("to").asText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public void deserialize() throws IOException {
assertThat(objectMapper.readValue("\"parameter\"", ValueType.class)).isEqualTo(ValueType.PARAMETER);
assertThatThrownBy(() -> objectMapper.readValue("\"\"", ValueType.class))
.isInstanceOf(JsonMappingException.class)
.hasMessageStartingWith("Can not deserialize value of type org.graylog2.contentpacks.model.entities.references.ValueType from String \"\": No enum constant org.graylog2.contentpacks.model.entities.references.ValueType");
.hasMessageStartingWith("Cannot deserialize value of type `org.graylog2.contentpacks.model.entities.references.ValueType` from String \"\": No enum constant org.graylog2.contentpacks.model.entities.references.ValueType");
assertThatThrownBy(() -> objectMapper.readValue("\"UNKNOWN\"", ValueType.class))
.isInstanceOf(JsonMappingException.class)
.hasMessageStartingWith("Can not deserialize value of type org.graylog2.contentpacks.model.entities.references.ValueType from String \"UNKNOWN\": No enum constant org.graylog2.contentpacks.model.entities.references.ValueType");
.hasMessageStartingWith("Cannot deserialize value of type `org.graylog2.contentpacks.model.entities.references.ValueType` from String \"UNKNOWN\": No enum constant org.graylog2.contentpacks.model.entities.references.ValueType");
assertThatThrownBy(() -> objectMapper.readValue("0", ValueType.class))
.isInstanceOf(JsonMappingException.class)
.hasMessageStartingWith("Unexpected token (VALUE_NUMBER_INT), expected VALUE_STRING: expected String");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,12 @@ public void decodeSucceedsWithMinimalMessages() throws Exception {
assertThat(codec.decode(new RawMessage("{\"message\":\"0\"}".getBytes(StandardCharsets.UTF_8)))).isNotNull();
}

@Test
public void decodeSucceedsWithTrailingComma() throws Exception {
assertThat(codec.decode(new RawMessage("{\"short_message\":\"0\",}".getBytes(StandardCharsets.UTF_8)))).isNotNull();
assertThat(codec.decode(new RawMessage("{\"message\":\"0\",}".getBytes(StandardCharsets.UTF_8)))).isNotNull();
}

@Test
public void decodeSucceedsWithValidTimestampIssue4027() throws Exception {
// https://github.com/Graylog2/graylog2-server/issues/4027
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
<HdrHistogram.version>2.1.10</HdrHistogram.version>
<hibernate-validator.version>6.0.13.Final</hibernate-validator.version>
<hk2.version>2.5.0-b32</hk2.version> <!-- The HK2 version should match the version being used by Jersey -->
<jackson.version>2.8.11.20190726</jackson.version>
<jackson.version>2.9.9</jackson.version>
<jadconfig.version>0.13.0</jadconfig.version>
<java-semver.version>0.9.0</java-semver.version>
<javapoet.version>1.11.1</javapoet.version>
Expand Down

0 comments on commit 786c341

Please sign in to comment.