javaspringspring-bootjacksonjackson-databind

Jackson fails due FAIL_ON_TRAILING_TOKENS but feature is disabled


I'm trying to migrate my Spring Boot app from 3.0 to 3.3.

I have some complex JSON sent from the frontend so the ObjectMapper is configured like this

@Bean
public Module dateSerializationModule() {
    return new SimpleModule("dateFormat")
        .addDeserializer(Date.class, new MultiDateDeserializer()).addSerializer(Date.class, new DateSerializer());
}

@Bean
@Primary
public ObjectMapper mapper() {
    return new ObjectMapper()
        .findAndRegisterModules().registerModule(attributesModule()).registerModule(dateSerializationModule())
        .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
        .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
        .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
        .configure(DeserializationFeature.FAIL_ON_TRAILING_TOKENS, false)
        .configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
}

The feature DeserializationFeature.FAIL_ON_TRAILING_TOKENS is disabled by default and i tried to add it here, but i get this exception

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Trailing token (of type FIELD_NAME) found after value (bound as com.fasterxml.jackson.databind.JsonNode): not allowed as per DeserializationFeature.FAIL_ON_TRAILING_TOKENS

Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Trailing token (of type FIELD_NAME) found after value (bound as com.fasterxml.jackson.databind.JsonNode): not allowed as per DeserializationFeature.FAIL_ON_TRAILING_TOKENS at [Source: REDACTED (StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION disabled); line: 1, column: 11847] (through reference chain: my.dto.MyDTO["aDateField"]) at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63) ~[jackson-databind-2.17.1.jar:2.17.1] at com.fasterxml.jackson.databind.DeserializationContext.reportTrailingTokens(DeserializationContext.java:1840) ~[jackson-databind-2.17.1.jar:2.17.1] at com.fasterxml.jackson.databind.ObjectMapper._verifyNoTrailingTokens(ObjectMapper.java:5013) ~[jackson-databind-2.17.1.jar:2.17.1] at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:4886) ~[jackson-databind-2.17.1.jar:2.17.1] at com.fasterxml.jackson.databind.ObjectMapper.readTree(ObjectMapper.java:3145) ~[jackson-databind-2.17.1.jar:2.17.1] at my.app.MultiDateDeserializer.deserialize(MultiDateDeserializer.java:41) ~[main/:na] at my.app.MultiDateDeserializer.deserialize(MultiDateDeserializer.java:1) ~[main/:na]

And this is the MultiDateDeserializer where the exception is thrown

public class MultiDateDeserializer extends StdDeserializer<Date> {
    public MultiDateDeserializer() {
        this(null);
    }

    public MultiDateDeserializer(Class<?> vc) {
        super(vc);
    }

    @Override
    public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
        JsonNode node = jp.getCodec().readTree(jp);  // Exception thrown here
        // ...
    }
}

And the corresponding field on the json

aDateField: "2022-03-09T00:00:00.000"

I debugged to if my ObjectMapper bean and the ObjectMapper used when the exception is thrown is the same and it's the same object instance.

I tried to enable the feature (just for test) and debugged the featureFlags variable inside com.fasterxml.jackson.databind.DeserializationContext but when true or false the value is the same.

I missed something or is it a bug inside jackson 2.17 ? was working with jackson 2.14 with the same input json


Solution

  • Upgrading from jackson 2.17.1 to 2.17.2 solved the issue