Skip to content

Commit

Permalink
Fix usage of deprecated Jackson methods (#17861)
Browse files Browse the repository at this point in the history
* Bump com.fasterxml.jackson:jackson-bom from 2.15.3 to 2.16.1.

* Fix deprecation warnings for PropertyNamingStrategy uses

* Fix mapping errors due to missing modules

Jackson is now warning about attempting to serialize
Optionals when the appropriate module is not loaded
(FasterXML/jackson-databind#4087)

* Bump repackaged mongojack version from 2.10.1.4 to 2.10.1.5

* Revert version bump of jackson and mongojack

---------

Co-authored-by: Bernd Ahlers <[email protected]>
  • Loading branch information
thll and bernd committed Jan 25, 2024
1 parent c2f6e88 commit 69db70c
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
import com.fasterxml.jackson.databind.jsontype.TypeIdResolver;
import com.fasterxml.jackson.databind.type.SimpleType;
Expand Down Expand Up @@ -59,7 +59,7 @@ public ObjectMapper get() {
* (one of my many useless talents is finding corner cases).
* </p>
*/
public static class PreserveLeadingUnderscoreStrategy extends PropertyNamingStrategy.SnakeCaseStrategy {
public static class PreserveLeadingUnderscoreStrategy extends PropertyNamingStrategies.SnakeCaseStrategy {
@Override
public String translate(String input) {
String translated = super.translate(input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.graylog2.contentpacks.jackson;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.TreeNode;
Expand Down Expand Up @@ -48,13 +47,12 @@ protected Object _deserialize(JsonParser p, DeserializationContext ctxt) throws
final TreeNode treeNode = p.readValueAsTree();
final TreeNode typeNode = treeNode.path("type");
if (!typeNode.isObject()) {

ctxt.reportWrongTokenException(typeNode.traverse(), JsonToken.START_OBJECT, "expected START_OBJECT before the type information and deserialized value");
ctxt.reportWrongTokenException(_baseType, JsonToken.START_OBJECT, "expected START_OBJECT before the type information and deserialized value");
}

final TreeNode valueNode = typeNode.path("@value");
if (!valueNode.isValueNode()) {
ctxt.reportWrongTokenException(typeNode.traverse(), JsonToken.VALUE_STRING, "expected VALUE_STRING as type information and deserialized value");
ctxt.reportWrongTokenException(_baseType, JsonToken.VALUE_STRING, "expected VALUE_STRING as type information and deserialized value");
}

final JsonParser jsonParser = valueNode.traverse();
Expand All @@ -63,7 +61,7 @@ protected Object _deserialize(JsonParser p, DeserializationContext ctxt) throws

final JsonParser newParser = treeNode.traverse();
if (newParser.nextToken() != JsonToken.START_OBJECT) {
ctxt.reportWrongTokenException(newParser, JsonToken.START_OBJECT, "expected START_OBJECT");
ctxt.reportWrongTokenException(_baseType, JsonToken.START_OBJECT, "expected START_OBJECT");
}
return deser.deserialize(newParser, ctxt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ValueType deserialize(JsonParser p, DeserializationContext ctxt) throws I
throw ctxt.weirdStringException(str, ValueType.class, e.getMessage());
}
} else {
throw ctxt.wrongTokenException(p, JsonToken.VALUE_STRING, "expected String " + Arrays.toString(ValueType.values()));
throw ctxt.wrongTokenException(p, handledType(), JsonToken.VALUE_STRING, "expected String " + Arrays.toString(ValueType.values()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ public SemverDeserializer(Semver.SemverType semverType) {

@Override
public Semver deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException {
switch (p.getCurrentTokenId()) {
switch (p.currentTokenId()) {
case JsonTokenId.ID_STRING:
case JsonTokenId.ID_NUMBER_INT:
final String str = p.getText().trim();
try {
return buildSemver(str);
} catch (SemverException e) {
ctxt.reportMappingException(e.getMessage());
ctxt.reportInputMismatch(this, e.getMessage());
}
default:
throw ctxt.wrongTokenException(p, JsonToken.VALUE_STRING, "expected String or Number");
throw ctxt.wrongTokenException(p, handledType(), JsonToken.VALUE_STRING, "expected String or Number");
}
}

private Semver buildSemver(String s) {
return new Semver(s, semverType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ public SemverRequirementDeserializer(Semver.SemverType semverType) {

@Override
public Requirement deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException {
switch (p.getCurrentTokenId()) {
switch (p.currentTokenId()) {
case JsonTokenId.ID_STRING:
final String str = p.getText().trim();
try {
return buildRequirement(str);
} catch (SemverException e) {
ctxt.reportMappingException(e.getMessage());
ctxt.reportInputMismatch(this, e.getMessage());
}
default:
throw ctxt.wrongTokenException(p, JsonToken.VALUE_STRING, null);
throw ctxt.wrongTokenException(p, handledType(), JsonToken.VALUE_STRING, null);
}
}

Expand All @@ -70,4 +70,4 @@ private Requirement buildRequirement(String s) {
return null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public VersionDeserializer() {

@Override
public Version deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException {
switch (p.getCurrentTokenId()) {
switch (p.currentTokenId()) {
case JsonTokenId.ID_STRING:
final String str = p.getText().trim();
return Version.valueOf(str);
case JsonTokenId.ID_NUMBER_INT:
return Version.forIntegers(p.getIntValue());
}
throw ctxt.wrongTokenException(p, JsonToken.VALUE_STRING, "expected String or Number");
throw ctxt.wrongTokenException(p, handledType(), JsonToken.VALUE_STRING, "expected String or Number");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
package org.graylog2.shared.bindings.providers;

import com.codahale.metrics.json.MetricsModule;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import com.fasterxml.jackson.databind.module.SimpleModule;
Expand Down Expand Up @@ -127,7 +128,10 @@ public ObjectMapperProvider(@GraylogClassLoader final ClassLoader classLoader,
.disable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
.disable(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY)
.setPropertyNamingStrategy(new PropertyNamingStrategy.SnakeCaseStrategy())
// Starting from Jackson 2.16, the default for INCLUDE_SOURCE_IN_LOCATION was changed to `disabled`.
// We are explicitly enabling it again to get verbose output that helps with troubleshooting.
.enable(JsonParser.Feature.INCLUDE_SOURCE_IN_LOCATION)
.setPropertyNamingStrategy(new PropertyNamingStrategies.SnakeCaseStrategy())
.setSubtypeResolver(subtypeResolver)
.setTypeFactory(typeFactory)
.setDateFormat(new StdDateFormat().withColonInTimeZone(false))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,10 @@ public class V20191125144500_MigrateDashboardsToViewsTest {
private final ObjectMapper objectMapper = new ObjectMapperProvider().get();

static class StaticRandomObjectIdProvider extends RandomObjectIdProvider {
private final Date date;
private AtomicInteger counter;

StaticRandomObjectIdProvider(Date date) {
super(date);
this.date = date;
this.counter = new AtomicInteger(0);
}

Expand All @@ -94,7 +93,8 @@ public String get() {

@Before
public void setUp() throws Exception {
final MongoJackObjectMapperProvider mapperProvider = new MongoJackObjectMapperProvider(new ObjectMapper());
final MongoJackObjectMapperProvider mapperProvider =
new MongoJackObjectMapperProvider(new ObjectMapperProvider().get());
final DashboardsService dashboardsService = new DashboardsService(mongodb.mongoConnection(), mapperProvider);

final RandomObjectIdProvider randomObjectIdProvider = new StaticRandomObjectIdProvider(new Date(1575020937839L));
Expand Down Expand Up @@ -157,7 +157,7 @@ public void migrateSampleDashboard() throws Exception {
.build()
);

assertViewsWritten(1,resourceFile("sample_dashboard-expected_views.json"));
assertViewsWritten(1, resourceFile("sample_dashboard-expected_views.json"));
assertSearchesWritten(1, resourceFile("sample_dashboard-expected_searches.json"));
}

Expand Down Expand Up @@ -245,22 +245,22 @@ public void migrateSampleDashboardWithUnknownWidget() {
.findFirst();
assertThat(nonImplementedWidget).isPresent();
assertThat(nonImplementedWidget.get()).isEqualTo(NonImplementedWidget.create(
"5020d62d-24a0-4b0c-8819-78e668cc2428",
"TOTALLY_UNKNOWN_WIDGET",
ImmutableMap.<String, Object>builder()
.put("valuetype", "total")
.put("renderer", "line")
.put("interpolation", "linear")
.put("timerange", ImmutableMap.<String, Object>of(
"type", "relative",
"range", 28800
))
.put("rangeType", "relative")
.put("field", "nf_bytes")
.put("query", "")
.put("interval", "minute")
.put("relative", 28800)
.build()
"5020d62d-24a0-4b0c-8819-78e668cc2428",
"TOTALLY_UNKNOWN_WIDGET",
ImmutableMap.<String, Object>builder()
.put("valuetype", "total")
.put("renderer", "line")
.put("interpolation", "linear")
.put("timerange", ImmutableMap.<String, Object>of(
"type", "relative",
"range", 28800
))
.put("rangeType", "relative")
.put("field", "nf_bytes")
.put("query", "")
.put("interval", "minute")
.put("relative", 28800)
.build()
)
);
}
Expand Down Expand Up @@ -383,28 +383,28 @@ public void migratesADashboardWithMissingQuickValuesAttributes() {

final List<ViewWidget> widgetWithoutAttributes = new ArrayList<>(findNewWidgets.apply("4ce93e89-4771-4ce2-8b59-6dc058cbfd3b"));
assertThat(widgetWithoutAttributes).hasSize(1);
assertThat(((AggregationWidget)widgetWithoutAttributes.get(0)).config().visualization()).isEqualTo("table");
assertThat(((AggregationWidget) widgetWithoutAttributes.get(0)).config().visualization()).isEqualTo("table");

final List<ViewWidget> widgetWithOnlyShowPieChartIsFalse = new ArrayList<>(findNewWidgets.apply("5c12c588-be0c-436b-b999-ee18378efd45"));
assertThat(widgetWithOnlyShowPieChartIsFalse).hasSize(1);
assertThat(((AggregationWidget)widgetWithOnlyShowPieChartIsFalse.get(0)).config().visualization()).isEqualTo("table");
assertThat(((AggregationWidget) widgetWithOnlyShowPieChartIsFalse.get(0)).config().visualization()).isEqualTo("table");

final List<ViewWidget> widgetWithOnlyShowDataTableIsFalse = new ArrayList<>(findNewWidgets.apply("e6a16d9a-23c0-4b7f-93b5-d790b5d64672"));
assertThat(widgetWithOnlyShowDataTableIsFalse).hasSize(1);
assertThat(((AggregationWidget)widgetWithOnlyShowDataTableIsFalse.get(0)).config().visualization()).isEqualTo("table");
assertThat(((AggregationWidget) widgetWithOnlyShowDataTableIsFalse.get(0)).config().visualization()).isEqualTo("table");

final List<ViewWidget> widgetWithBothAttributesPresentButFalse = new ArrayList<>(findNewWidgets.apply("568c005a-11ec-4be9-acd7-b2aa509c07e0"));
assertThat(widgetWithBothAttributesPresentButFalse).hasSize(1);
assertThat(((AggregationWidget)widgetWithBothAttributesPresentButFalse.get(0)).config().visualization()).isEqualTo("table");
assertThat(((AggregationWidget) widgetWithBothAttributesPresentButFalse.get(0)).config().visualization()).isEqualTo("table");

final List<ViewWidget> widgetWithPieChartPresentAndTrue = new ArrayList<>(findNewWidgets.apply("2e3c5e76-bbfd-4ac3-a27b-7491a5cbf59a"));
assertThat(widgetWithPieChartPresentAndTrue).hasSize(1);
assertThat(((AggregationWidget)widgetWithPieChartPresentAndTrue.get(0)).config().visualization()).isEqualTo("pie");
assertThat(((AggregationWidget) widgetWithPieChartPresentAndTrue.get(0)).config().visualization()).isEqualTo("pie");

final List<ViewWidget> widgetWithBothPieChartAndDataTablePresentAndTrue = new ArrayList<>(findNewWidgets.apply("26a0a3e1-718f-4bfe-90a2-cb441390152d"));
assertThat(widgetWithBothPieChartAndDataTablePresentAndTrue).hasSize(2);
assertThat(widgetWithBothPieChartAndDataTablePresentAndTrue)
.extracting(viewWidget -> ((AggregationWidget)viewWidget).config().visualization())
.extracting(viewWidget -> ((AggregationWidget) viewWidget).config().visualization())
.containsExactlyInAnyOrder("table", "pie");
}

Expand Down Expand Up @@ -448,7 +448,7 @@ public void migrateDashboardWithWidgetsMissingQueryAttributes() throws Exception
.build()
);

assertViewsWritten(1,resourceFile("dashboard_with_widgets_missing_query_attributes-expected_views.json"));
assertViewsWritten(1, resourceFile("dashboard_with_widgets_missing_query_attributes-expected_views.json"));
assertSearchesWritten(1, resourceFile("dashboard_with_widgets_missing_query_attributes-expected_searches.json"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import com.fasterxml.jackson.databind.module.SimpleModule;
Expand Down Expand Up @@ -78,7 +78,7 @@ public void setup() {
this.objectMapper = mapper
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
.setPropertyNamingStrategy(new PropertyNamingStrategy.SnakeCaseStrategy())
.setPropertyNamingStrategy(new PropertyNamingStrategies.SnakeCaseStrategy())
.setTypeFactory(typeFactory)
.registerModule(new GuavaModule())
.registerModule(new JodaModule())
Expand Down Expand Up @@ -115,7 +115,7 @@ void mergeWithExecutionState() {
ExecutionStateGlobalOverride.Builder executionState = ExecutionStateGlobalOverride.builder();

executionState.timerange(RelativeRange.create(60));
executionState.searchTypesBuilder().put(messageListId, SearchTypeExecutionState.builder().offset(150).limit(300).build());
executionState.searchTypesBuilder().put(messageListId, SearchTypeExecutionState.builder().offset(150).limit(300).build());

final Query mergedQuery = query.applyExecutionState(executionState.build());
assertThat(mergedQuery)
Expand Down Expand Up @@ -207,6 +207,7 @@ private RelativeRange relativeRange(int range) {
throw new RuntimeException("invalid time range", e);
}
}

private Query.Builder validQueryBuilder() {
return Query.builder().id(UUID.randomUUID().toString()).timerange(mock(TimeRange.class)).query(ElasticsearchQueryString.empty());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.codahale.metrics.json.MetricsModule;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import com.fasterxml.jackson.databind.module.SimpleModule;
Expand Down Expand Up @@ -53,7 +53,7 @@ public void setup() throws Exception {
this.objectMapper = mapper
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
.setPropertyNamingStrategy(new PropertyNamingStrategy.SnakeCaseStrategy())
.setPropertyNamingStrategy(new PropertyNamingStrategies.SnakeCaseStrategy())
.setTypeFactory(typeFactory)
.registerModule(new GuavaModule())
.registerModule(new JodaModule())
Expand All @@ -67,7 +67,7 @@ public void setup() throws Exception {
.addSerializer(new ObjectIdSerializer()));

// kludge because we don't have an injector in tests
ImmutableMap<String, Class> subtypes = ImmutableMap.<String, Class>builder()
ImmutableMap<String, Class> subtypes = ImmutableMap.<String, Class>builder()
.put(StreamFilter.NAME, StreamFilter.class)
.put(ElasticsearchQueryString.NAME, ElasticsearchQueryString.class)
.put(MessageList.NAME, MessageList.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.graylog2.rest.documentation.generator;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.graylog2.rest.resources.HelloWorldResource;
import org.graylog2.shared.ServerVersion;
Expand All @@ -44,7 +44,7 @@ public class GeneratorTest {
public static void init() {
objectMapper = new ObjectMapper();
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
}

@Test
Expand Down

0 comments on commit 69db70c

Please sign in to comment.