vaadin / hilla

Build better business applications, faster. No more juggling REST endpoints or deciphering GraphQL queries. Hilla seamlessly connects Spring Boot and React to accelerate application development.
https://hilla.dev
Apache License 2.0
926 stars 57 forks source link

Support for java.time.Year (and possibly other time types as well) #2856

Open peholmst opened 1 month ago

peholmst commented 1 month ago

Describe your motivation

I'd like to be able to use java.time.Year in browser callable services, and possibly other time types as well. Many business applications work with time in various ways and so it would make sense to support this in Hilla as well.

Without any custom configuration, Jackson serializes Year as a string. Hilla is able to read this as well. However, the model type is ObjectModel and the DTO type is unknown. This causes problems with form binding; the default empty value is [object Object] and not an empty string.

Describe the solution you'd like

A quick and dirty solution would be to just hard code support for java.time.Year and other built-in Java types that one could assume are used in browser callable services.

A more flexible solution would be an API for customizing the code generator. Just as you have to create a custom serializer and deserializer to instruct Jackson to treat Year as an integer, you could create a custom converter that instructs the Hilla code generator how to treat Year.

Describe alternatives you've considered

You could just use an integer or string in your DTO and handle the conversion inside the browser callable service. However, this goes agains the principle of being able to use the same backend in both Flow and Hilla.

This turns the browser callable services into adapters - yet another abstraction layer in front of the actual application services.

Additional context

No response

platosha commented 3 weeks ago

Regarding the API for customizing the code generator, one solution would be better exposure for the Jackson mapper that we use in both code generation and runtime.

For now let us go with hard-coded java.time.Year support.

platosha commented 3 weeks ago

What about other java.time.* classes? Any other common missing types?

peholmst commented 3 weeks ago

java.time.YearMonth would be useful, as would java.time.Period and java.time.Duration. I assume LocalDate, LocalDateTime, ZonedDateTime, OffsetDateTime, and Instant are already supported.

platosha commented 3 weeks ago

We need to define the scope for supported types somehow. Any good ideas for the baseline?

peholmst commented 3 weeks ago

Maybe everything that Java provides out of the box should be supported?