From 06c93d2a8df17f2ebe4e0799d354b330d2f6f9de Mon Sep 17 00:00:00 2001 From: Radovan Radic Date: Thu, 5 Dec 2024 08:46:54 +0100 Subject: [PATCH] Fix LocalDate persisting dates as the day before (#3248) * Reproducing issue with LocalDate going one day back * Verify localDate values are saved and read correctly * Sonar warning --- .../data/runtime/convert/DataConversionServiceFactory.java | 3 +++ .../java/io/micronaut/data/runtime/mapper/QueryStatement.java | 2 +- .../io/micronaut/data/tck/tests/AbstractRepositorySpec.groovy | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/data-runtime/src/main/java/io/micronaut/data/runtime/convert/DataConversionServiceFactory.java b/data-runtime/src/main/java/io/micronaut/data/runtime/convert/DataConversionServiceFactory.java index a42c227dbce..e52fb64b439 100644 --- a/data-runtime/src/main/java/io/micronaut/data/runtime/convert/DataConversionServiceFactory.java +++ b/data-runtime/src/main/java/io/micronaut/data/runtime/convert/DataConversionServiceFactory.java @@ -634,6 +634,9 @@ DataConversionServiceImpl build(@NonNull BeanContext beanContext) { // ZonedDateTime addZonedConvertorsConvertors(conversionService, ZonedDateTime.class, Function.identity()); + // LocalDate + conversionService.addConverter(LocalDate.class, java.sql.Date.class, java.sql.Date::valueOf); + // LocalTime conversionService.addConverter(LocalTime.class, Timestamp.class, localTime -> Timestamp.valueOf(localTime.atDate(LocalDate.now()))); conversionService.addConverter(LocalTime.class, Instant.class, localTime -> localTime.atDate(LocalDate.now()).atZone(ZoneId.systemDefault()).toInstant()); diff --git a/data-runtime/src/main/java/io/micronaut/data/runtime/mapper/QueryStatement.java b/data-runtime/src/main/java/io/micronaut/data/runtime/mapper/QueryStatement.java index 09db30b420c..af60dda2895 100644 --- a/data-runtime/src/main/java/io/micronaut/data/runtime/mapper/QueryStatement.java +++ b/data-runtime/src/main/java/io/micronaut/data/runtime/mapper/QueryStatement.java @@ -100,7 +100,7 @@ default QueryStatement setDynamic( if (value instanceof Date date) { return setDate(statement, index, date); } else { - return setDate(statement, index, convertRequired(value, Date.class)); + return setDate(statement, index, convertRequired(value, java.sql.Date.class)); } case TIMESTAMP: Instant instant; diff --git a/data-tck/src/main/groovy/io/micronaut/data/tck/tests/AbstractRepositorySpec.groovy b/data-tck/src/main/groovy/io/micronaut/data/tck/tests/AbstractRepositorySpec.groovy index 8f7d10df9bd..c55a31ea82e 100644 --- a/data-tck/src/main/groovy/io/micronaut/data/tck/tests/AbstractRepositorySpec.groovy +++ b/data-tck/src/main/groovy/io/micronaut/data/tck/tests/AbstractRepositorySpec.groovy @@ -486,6 +486,7 @@ abstract class AbstractRepositorySpec extends Specification { retrievedBook.offsetDateTime == book.offsetDateTime retrievedBook.dateCreated == book.dateCreated retrievedBook.dateUpdated == book.dateUpdated + retrievedBook.localDate == book.localDate // stored as a DATE type without time // retrievedBookProj.date == book.date