Register Converters for Offset java.time types in JSR310Converters.

We now appropriately handle OffsetDateTime and OffsetTime the same as all other java.time types, supported as simple types on Spring application (persistent) entity classes.

Closes #2677
This commit is contained in:
John Blum
2023-08-16 17:35:00 -07:00
committed by Mark Paluch
parent 372d26ab21
commit dc09635cc4
6 changed files with 178 additions and 19 deletions

View File

@@ -32,7 +32,6 @@ import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.test.condition.EnabledOnRedisClusterAvailable;
import org.springframework.lang.NonNullApi;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@@ -52,13 +51,14 @@ class RedisRepositoryClusterIntegrationTests extends RedisRepositoryIntegrationT
@EnableRedisRepositories(considerNestedRepositories = true, indexConfiguration = MyIndexConfiguration.class,
keyspaceConfiguration = MyKeyspaceConfiguration.class,
includeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,
classes = { PersonRepository.class, CityRepository.class, ImmutableObjectRepository.class }) })
classes = { PersonRepository.class, CityRepository.class, ImmutableObjectRepository.class, UserRepository.class }) })
static class Config {
@Bean
RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<byte[], byte[]> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
return template;

View File

@@ -17,6 +17,8 @@ package org.springframework.data.redis.repository;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -49,6 +51,9 @@ import org.springframework.data.redis.core.index.SimpleIndexDefinition;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.QueryByExampleExecutor;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Base for testing Redis repository support in different configurations.
@@ -62,6 +67,7 @@ public abstract class RedisRepositoryIntegrationTestBase {
@Autowired PersonRepository repo;
@Autowired CityRepository cityRepo;
@Autowired ImmutableObjectRepository immutableObjectRepo;
@Autowired UserRepository userRepository;
@Autowired KeyValueTemplate kvTemplate;
@BeforeEach
@@ -495,6 +501,24 @@ public abstract class RedisRepositoryIntegrationTestBase {
assertThat(loaded.nested).isEqualTo(nested);
}
@Test // GH-2677
void shouldProperlyHandleEntityWithOffsetJavaTimeTypes() {
User jonDoe = User.as("Jon Doe")
.expires(OffsetTime.now().plusMinutes(5))
.lastAccess(OffsetDateTime.now());
this.userRepository.save(jonDoe);
User loadedJonDoe = this.userRepository.findById(jonDoe.getName()).orElse(null);
assertThat(loadedJonDoe).isNotNull();
assertThat(loadedJonDoe).isNotSameAs(jonDoe);
assertThat(loadedJonDoe.getName()).isEqualTo(jonDoe.getName());
assertThat(loadedJonDoe.getLastAccessed()).isEqualTo(jonDoe.getLastAccessed());
assertThat(loadedJonDoe.getExpiration()).isEqualTo(jonDoe.getExpiration());
}
public interface PersonRepository
extends PagingAndSortingRepository<Person, String>, CrudRepository<Person, String>,
QueryByExampleExecutor<Person> {
@@ -542,6 +566,8 @@ public abstract class RedisRepositoryIntegrationTestBase {
public interface ImmutableObjectRepository extends CrudRepository<Immutable, String> {}
public interface UserRepository extends CrudRepository<User, String> { }
/**
* Custom Redis {@link IndexConfiguration} forcing index of {@link Person#lastname}.
*
@@ -784,4 +810,72 @@ public abstract class RedisRepositoryIntegrationTestBase {
return Objects.equals(getNested(), nested) ? this : new Immutable(this.id, this.name, nested);
}
}
@RedisHash("Users")
static class User {
static User as(@NonNull String name) {
Assert.hasText(name, () -> String.format("Name [%s] of User is required", name));
return new User(name);
}
private OffsetDateTime lastAccessed;
private OffsetTime expiration;
@Id
private final String name;
private User(@NonNull String name) {
this.name = name;
}
@Nullable
public OffsetTime getExpiration() {
return this.expiration;
}
@Nullable
public OffsetDateTime getLastAccessed() {
return this.lastAccessed;
}
public String getName() {
return this.name;
}
public User lastAccess(@Nullable OffsetDateTime dateTime) {
this.lastAccessed = dateTime;
return this;
}
public User expires(@Nullable OffsetTime time) {
this.expiration = time;
return this;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof User that)) {
return false;
}
return this.getName().equals(that.getName());
}
@Override
public int hashCode() {
return Objects.hash(getName());
}
@Override
public String toString() {
return getName();
}
}
}

View File

@@ -57,8 +57,7 @@ public class RedisRepositoryIntegrationTests extends RedisRepositoryIntegrationT
@EnableRedisRepositories(considerNestedRepositories = true, indexConfiguration = MyIndexConfiguration.class,
keyspaceConfiguration = MyKeyspaceConfiguration.class,
includeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,
classes = { PersonRepository.class, CityRepository.class, ImmutableObjectRepository.class }) })
classes = { PersonRepository.class, CityRepository.class, ImmutableObjectRepository.class, UserRepository.class }) })
static class Config {
@Bean
@@ -70,6 +69,7 @@ public class RedisRepositoryIntegrationTests extends RedisRepositoryIntegrationT
RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, String> template = new RedisTemplate<>();
template.setDefaultSerializer(StringRedisSerializer.UTF_8);
template.setConnectionFactory(connectionFactory);
@@ -107,6 +107,7 @@ public class RedisRepositoryIntegrationTests extends RedisRepositoryIntegrationT
public void shouldConsiderCustomTypeMapper() {
Person rand = new Person();
rand.id = "rand";
rand.firstname = "rand";
rand.lastname = "al'thor";