0

I use spring-cloud-gcp-data-spanner for fetching data from GCP Spanner. One of my tables contains the TIMESTAMP column, which contains date-time in UTC.

Spring Data Cloud Data already has some default Spanner converters for some custom types. My entity class maps TIMESTAMP (com.google.cloud.Timestamp) column to LocalDateTime.

It uses default TIMESTAMP_LOCAL_DATE_TIME_CONVERTER and converts date-time according to my local zone (UTC+3). I added my custom converter and specified it using ConverterAwareMappingSpannerEntityProcessor, but my custom converter isn't used and appears at the end of the converters list (GenericConversionService#converters).

public class LocalDateTimeReadConverter implements Converter<com.google.cloud.Timestamp, LocalDateTime> {

  @Nullable
  @Override
  public LocalDateTime convert(com.google.cloud.Timestamp timestamp) {
    return Instant
        .ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos())
        .atZone(ZoneId.of("UTC"))
        .toLocalDateTime();
  }
}
  @Bean
  public SpannerEntityProcessor spannerConverter(SpannerMappingContext mappingContext) {
    return new ConverterAwareMappingSpannerEntityProcessor(mappingContext,
        List.of(new LocalDateTimeWriteConverter()),
        List.of(new LocalDateTimeReadConverter()));
  }

DB value: 2020-06-18T15:55:09.000Z
Actual value: 2020-06-18T18:55:09.000Z
Expected value: 2020-06-18T15:55:09.000Z

How can I override a default Spanner converter?

Oleg Koskin
  • 13
  • 1
  • 4
  • Have you looked already at these answers? The last link is a fixed thread on github. https://stackoverflow.com/questions/59071133/timestamp-converter-not-working-in-spring-data-rest-with-spanner https://stackoverflow.com/questions/59136904/unable-to-convert-java-sql-timestamp-to-com-google-cloud-timestamp-in-spring-dat https://github.com/spring-cloud/spring-cloud-gcp/pull/2065 – Antonio Ramirez Jun 23 '20 at 22:51
  • Hi, @AntonioRamirez! Yep, I've looked at those topics. They are a little bit about another. My issue is converting between `com.google.cloud.Timestamp` and `java.time.LocalDateTime`. Spring Data already has a default converter and I need to override it. – Oleg Koskin Jun 24 '20 at 06:13
  • Are you setting the params in the proper order? I mean, when consuming the method? – Joss Baron Jun 30 '20 at 23:00
  • 2
    [Dmitry S](https://stackoverflow.com/users/2461728/dmitry-s) posted in an [Answer](https://stackoverflow.com/a/64761713/12695027) that "I created a fix for this issue: https://github.com/spring-cloud/spring-cloud-gcp/pull/2580" – Scratte Nov 10 '20 at 01:59

1 Answers1

0

This was resolved by https://github.com/spring-cloud/spring-cloud-gcp/pull/2580 which is a fix made by Dmitry S as they mentioned in their deleted answer.

cigien
  • 57,834
  • 11
  • 73
  • 112
Oleg Koskin
  • 13
  • 1
  • 4
  • If I was able to persuade someone to un-delete the original Answer, would you be happy accepting that one and removing this Answer of your? – Scratte Dec 11 '20 at 14:27