rethinkdb-java icon indicating copy to clipboard operation
rethinkdb-java copied to clipboard

Missing findAndRegisterModules() when initialising the ObjectMapper

Open davide1995 opened this issue 2 years ago • 4 comments

With RethinkDB Java Driver 2.4.4, if an OffsetDateTime is being saved, following error occurs: java.lang.IllegalArgumentException: Java 8 date/time type java.time.OffsetDateTime not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: ch.yourpackage.YourClass["yourField"])

I state that com.fasterxml.jackson.datatype:jackson-datatype-jsr310 is already in my build.gradle file.

To avoid the above issue, as a workaround I need to include this line during RethinkDB initializer: RethinkDB.setResultMapper(RethinkDB.getResultMapper().findAndRegisterModules());

My advice would be to modify your getResultMapper() method in the RethinkDB.java class as follows:

public synchronized static @NotNull ObjectMapper getResultMapper() {
        ObjectMapper mapper = resultMapper;
        if (mapper == null) {
            mapper = new ObjectMapper()
                .findAndRegisterModules() // This line should be the new one
                .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
            resultMapper = mapper;
        }
        return mapper;
    }

davide1995 avatar Apr 23 '23 20:04 davide1995

Huh. At least going by the documentation, OffsetDateTime should be supported: https://rethinkdb.com/api/java/expr/

I wonder if this could be fixed by a change in https://github.com/rethinkdb/rethinkdb-java/blob/master/build.gradle.kts

I'm very unfamiliar with gradle, Jackson, or this driver, so maybe somebody else can chime in.

srh avatar Apr 24 '23 05:04 srh

Is there any update on that?

davide1995 avatar May 14 '23 08:05 davide1995

Apparently not.

srh avatar May 23 '23 01:05 srh

Auto-Registering in the current version of Jackson registers the legacy version JSR310Module. Which was depreciated in 2.5. and replaced by JavaTimeModule

    public synchronized static @NotNull ObjectMapper getResultMapper() {
        ObjectMapper mapper = resultMapper;
        if (mapper == null) {
            mapper = new ObjectMapper()
                .registerModule(new JavaTimeModule())
                .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
            resultMapper = mapper;
        }
        return mapper;
    }
    ```

ROMVoid95 avatar Nov 08 '23 07:11 ROMVoid95