apache / apache/grails-core

[Grails 5] Unclear how to enable codecs for Java 8 date/time class marshalling

Open
#14,363 0 comments 0 reactions 1 assignee Claimed by @puneetbehl View on GitHub
relates-to: grails-data-mongodb
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

I've reviewed the documentation at https://gorm.grails.org/latest/mongodb/manual/, I don't see how to take advantage of the codecs in `groovy.org.grails.datastore.bson.codecs`. My assumption was that just adding the MongoDb plugin to my application would automatically enable marshalling/unmarshalling of Java 8 types such as LocalDate and ZonedDateTime, but instead they're serialized as `String`, and when retrieved I receive an exception. Details follow.

Steps I took:
- Create a new Grails (v5.1.7) app: `grails create-app test-mongo --profile rest-api`
- Add the Gorm MongoDb plugin to `build.config`: `implementation 'org.grails.plugins:mongodb:7.3.0'`
- Create a trivial Domain and associated Controller with `save` & `list` endpoints), with `LocalDate` and `ZonedDateTime` props
- Add MongoDb connection settings to local dev config in `application.yml`

`POST` to /foo successful, but serializes LocalDate and ZonedDateTime as `String`. On `GET`, receive the following error:

>Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDate' for property 'dob'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.time.LocalDate' for property 'dob': no matching editors or conversion strategy found

I also tried adding this: `implementation 'org.grails:grails-datastore-gorm-mongodb:7.3.0'`, but same result.

Example repo: https://github.com/MichaelJRussell/mongo-test
- build.gradle: https://github.com/MichaelJRussell/mongo-test/blob/main/build.gradle
- Domain: https://github.com/MichaelJRussell/mongo-test/blob/main/grails-app/domain/Foo.groovy

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.