Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.time.LocalDateTime data binding seems to be incorrect in Grails 6.1.0 #13280

Closed
monetschemist opened this issue Dec 12, 2023 · 4 comments
Closed
Assignees

Comments

@monetschemist
Copy link

monetschemist commented Dec 12, 2023

Expected Behavior

A domain class having a field declared to be java.time.LocalDateTime should have default data binding that expects the standard ISO-8601 string formats defined in that class.

For example, a String value such as 2023-12-11T17:12 should be successfully converted to the corresponding LocalDateTime value.

Actual Behaviour

The default data binding fails to convert such String values. For example, in my class Scenario which has a LocalDateTime field dateTime, the ScenarioController save() method fails on data binding with a message like:

scenario.errors org.grails.datastore.mapping.validation.ValidationErrors: 1 errors
Field error in object 'Scenario' on field 'dateTime': rejected value [2023-12-11T17:12]; codes [Scenario.dateTime.typeMismatch.error,Scenario.dateTime.typeMismatch,scenario.dateTime.typeMismatch.error,scenario.dateTime.typeMismatch,typeMismatch.Scenario.dateTime,typeMismatch.dateTime,typeMismatch.java.time.LocalDateTime,typeMismatch]; arguments [dateTime]; default message [Text '2023-12-11T17:12' could not be parsed at index 10]

This problem can be worked around by definining a specific data binding following the example of the BindUsing annotation provided in the Grails 6.1.0 docs. In my case, the following works:

import grails.databinding.BindUsing

...

    @BindUsing({obj, source ->
        LocalDateTime.parse(source['dateTime'])
    })
    LocalDateTime dateTime

I suppose this hints that the default data binding for LocalDateTime should use its parse() method.

Steps To Reproduce

Create a domain class with a java.time.LocalDateTime field; create the CRUD for it; run the application and create a new instance of that domain class; enter the desired local date and time and save.

Environment Information

  • Ubuntu 23.10
  • OpenJDK Runtime Environment Temurin-11.0.21+9 (build 11.0.21+9)

Example Application

This application has a single domain class with a LocalDateTime field

Version

6.1.0

@monetschemist
Copy link
Author

I realized I probably should have mentioned that the format including the T between the date and time components of the string is generated in my case by HTML input type="datetime-local" and not g:datePicker.

puneetbehl pushed a commit that referenced this issue Feb 28, 2024
Adds a new converter named `LocalDateTimeConverter` which will automatically convert `String` or `LocalDateTime` values when using data binder.

Fixes #13280
@monetschemist
Copy link
Author

@puneetbehl thank you so much! a question if I may - is this in 6.1.2 or somewhere down the road?

Again thank you so much!

@puneetbehl
Copy link
Contributor

It will be a part of Grails 6.2.0 which we'll release in a few days.

@monetschemist
Copy link
Author

@puneetbehl thanks very much for that clarification, and thanks again for fixing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants