FasterXML / jackson-modules-java8

Set of support modules for Java 8 datatypes (Optionals, date/time) and features (parameter names)
Apache License 2.0
398 stars 116 forks source link

Custom java time module ignored after upgrade to 2.16 #293

Closed Sjoerd97 closed 9 months ago

Sjoerd97 commented 9 months ago

Specs Java 17 Gradle 8 Jackson 2.16

We recently upgraded to jackson 2.16 and we noticed that our custom deserializer is no longer used whenever we register a JsonMapper. Whenever we downgrade back to 2.15.2 it magically works again. I tried registering the module after the JsonMapper was build but to no avail

To reproduce you can use this piece of code

`class CustomDateTimeDeserializerTest {

private static final JsonMapper JSON_MAPPER = JsonMapper.builder()
    .addModule(new JavaTimeModule().addDeserializer(LocalDateTime.class, new CustomDateTimeDeserializer()))
    .build();

@Test
void deserializeSummerTime() throws JsonProcessingException {
    LocalDateTimeObject ldtAmsterdam = JSON_MAPPER.readValue("{ \"ldt\": \"2023-04-25T12:39:10Z\" }", LocalDateTimeObject.class);
    assertEquals(LocalDateTime.of(2023, 4, 25, 14, 39, 10), ldtAmsterdam.ldt());
}

}

record LocalDateTimeObject(LocalDateTime ldt) {
}`

Additionally you can create a overriden deserialize method with random logic and put a breakpoint on a certain line in the method. You will come to find out it never reaches the breakpoint

cowtowncoder commented 9 months ago

Ok this is due to #288 which is fixed for 2.16.1.

The root cause of regression is the fact that the use of JavaTimeModule to add (de)serializers through was not an intended use case -- so that was not tested. And although I think that is not a good idea to do (adding things to modules you don't control), this appears to be commonly done, so there's a fix.

If you have time and interest, it'd be great to verify that 2.16.1-SNAPSHOT of this module indeed fixes your problem. I don't yet know when 2.16.1 will be released, but it should be done before end of 2023 (that is within next 4 weeks).

robobdeijn commented 9 months ago

@Sjoerd97 is currently unavailable, but in his absence I have tested the 2.16.1-SNAPSHOT version and I can confirm that this fixes our issue. Thanks!

cowtowncoder commented 9 months ago

Thank you @robobdeijn ! Will close as Duplicate; will be resolved by 2.16.1 release.