Home > OS >  gradle runtime dependency makes test fail because of jackson dependencies issue
gradle runtime dependency makes test fail because of jackson dependencies issue

Time:12-02

On a JEE project running on Wildfly 15, I am trying to configure the logs to be in json format. Replicating what I have done dozens of times for some more recent Spring Boot projects, I am adding net.logstash.logback:logstash-logback-encoder:6.6 as a runtimeOnly dependency, as I plan to have a logback.xml config file.

When building my application and running the test, adding this dependency makes some tests fail : some controllers are not able to serialize LocalDateTime anymore.

Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: de.gefa.tankkarte.common.port.adapter.restapi.RestServiceErrorResponseEntity["timestamp"])
    at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
    at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1276)
    at com.fasterxml.jackson.databind.ser.impl.UnsupportedTypeSerializer.serialize(UnsupportedTypeSerializer.java:35)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:770)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
    at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1514)
    at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:1006)
    at org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider.writeTo(ResteasyJackson2Provider.java:328)
    at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.writeTo(AbstractWriterInterceptorContext.java:137) 

I don't understand why a dependency supposed to be used for logging interferes with the way the REST response gets built... and jackson-datatype-jsr310 IS part of the classpath. When I run the dependencies task, I get this :

runtimeClasspath - Runtime classpath of source set 'main'.
...
 --- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.10.4 -> 2.12.0
|     --- com.fasterxml.jackson.core:jackson-annotations:2.12.0
|    |    \--- com.fasterxml.jackson:jackson-bom:2.12.0
|    |          --- com.fasterxml.jackson.core:jackson-annotations:2.12.0 (c)
|    |          --- com.fasterxml.jackson.core:jackson-core:2.12.0 (c)
|    |          --- com.fasterxml.jackson.core:jackson-databind:2.12.0 (c)
|    |         \--- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.12.0 (c)
|     --- com.fasterxml.jackson.core:jackson-core:2.12.0
|    |    \--- com.fasterxml.jackson:jackson-bom:2.12.0 (*)
|     --- com.fasterxml.jackson.core:jackson-databind:2.12.0
|    |     --- com.fasterxml.jackson.core:jackson-annotations:2.12.0 (*)
|    |     --- com.fasterxml.jackson.core:jackson-core:2.12.0 (*)
|    |    \--- com.fasterxml.jackson:jackson-bom:2.12.0 (*)
|    \--- com.fasterxml.jackson:jackson-bom:2.12.0 (*)
...
 --- net.logstash.logback:logstash-logback-encoder:6.6
|    \--- com.fasterxml.jackson.core:jackson-databind:2.12.0 (*)

I really don't understand how and why this conflicts...

CodePudding user response:

I don't think I understand the reason, but after trying different things, only downgrading the version from 6.6 to 6.4 helped. Now dependencies look like that :

runtimeClasspath - Runtime classpath of source set 'main'.
...
 --- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.10.4
|     --- com.fasterxml.jackson.core:jackson-annotations:2.10.4 -> 2.11.0
|     --- com.fasterxml.jackson.core:jackson-core:2.10.4 -> 2.11.0
|    \--- com.fasterxml.jackson.core:jackson-databind:2.10.4 -> 2.11.0
|          --- com.fasterxml.jackson.core:jackson-annotations:2.11.0
|         \--- com.fasterxml.jackson.core:jackson-core:2.11.0
...
 --- net.logstash.logback:logstash-logback-encoder:6.4
|    \--- com.fasterxml.jackson.core:jackson-databind:2.11.0 (*)
     

I don't understand why in that case jackson-datatype-jsr310:2.10.4 doesn't get upgraded automatically by 2.11.0, while it was upgraded to 2.12.0 previously.

This is OK for now, but a bit scarry for the future : next time somebody tries to upgrade versions on this application, it's likely that there will be some issues..

What seems to also work is if I exclude the transitive dependency - then, I can even use latest logback-encoder version :

runtimeOnly ('net.logstash.logback:logstash-logback-encoder:7.0.1') {
    exclude group: 'com.fasterxml.jackson.core', module: 'jackson-databind'
}

So if anyone understands what is going on and how to make it more reliable for the future, don't hesitate to make suggestions !

  • Related