rethinkdb / rethinkdb-java

Official RethinkDB Java client
https://rethinkdb.com/api/java/
Apache License 2.0
21 stars 10 forks source link

Missing findAndRegisterModules() when initialising the ObjectMapper #70

Open davide1995 opened 1 year ago

davide1995 commented 1 year ago

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;
    }
srh commented 1 year ago

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.

davide1995 commented 1 year ago

Is there any update on that?

srh commented 1 year ago

Apparently not.

ROMVoid95 commented 9 months ago

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;
    }