From b40838e70e83511fbf49ddeb071cbf2dc29f7722 Mon Sep 17 00:00:00 2001 From: Sarthak Borse Date: Sat, 4 Jul 2026 12:29:22 +0530 Subject: [PATCH] :Allow nullable RedisSessionMapper return type Signed-off-by: Sarthak Borse --- .../data/redis/RedisIndexedSessionRepository.java | 4 ++-- .../session/data/redis/RedisSessionRepository.java | 4 ++-- .../redis/RedisIndexedSessionRepositoryTests.java | 14 ++++++++++++++ .../data/redis/RedisSessionRepositoryTests.java | 11 +++++++++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java index 531e61317..89a4d369f 100644 --- a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java +++ b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java @@ -334,7 +334,7 @@ public class RedisIndexedSessionRepository private SessionIdGenerator sessionIdGenerator = UuidSessionIdGenerator.getInstance(); - private BiFunction, MapSession> redisSessionMapper = new RedisSessionMapper(); + private BiFunction, @Nullable MapSession> redisSessionMapper = new RedisSessionMapper(); /** * Creates a new instance. For an example, refer to the class level javadoc. @@ -761,7 +761,7 @@ public void setSessionIdGenerator(SessionIdGenerator sessionIdGenerator) { * @param redisSessionMapper the mapper to use, cannot be null * @since 3.2 */ - public void setRedisSessionMapper(BiFunction, MapSession> redisSessionMapper) { + public void setRedisSessionMapper(BiFunction, @Nullable MapSession> redisSessionMapper) { Assert.notNull(redisSessionMapper, "redisSessionMapper cannot be null"); this.redisSessionMapper = redisSessionMapper; } diff --git a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisSessionRepository.java b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisSessionRepository.java index 1a298ce40..7ac1517b6 100644 --- a/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisSessionRepository.java +++ b/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisSessionRepository.java @@ -63,7 +63,7 @@ public class RedisSessionRepository implements SessionRepository, MapSession> redisSessionMapper = new RedisSessionMapper(); + private BiFunction, @Nullable MapSession> redisSessionMapper = new RedisSessionMapper(); /** * Create a new {@link RedisSessionRepository} instance. @@ -187,7 +187,7 @@ public void setSessionIdGenerator(SessionIdGenerator sessionIdGenerator) { * @param redisSessionMapper the mapper to use, cannot be null * @since 3.2 */ - public void setRedisSessionMapper(BiFunction, MapSession> redisSessionMapper) { + public void setRedisSessionMapper(BiFunction, @Nullable MapSession> redisSessionMapper) { Assert.notNull(redisSessionMapper, "redisSessionMapper cannot be null"); this.redisSessionMapper = redisSessionMapper; } diff --git a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisIndexedSessionRepositoryTests.java b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisIndexedSessionRepositoryTests.java index 1ac304e65..41c54857e 100644 --- a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisIndexedSessionRepositoryTests.java +++ b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisIndexedSessionRepositoryTests.java @@ -960,6 +960,20 @@ void findByIdWhenChangeSessionIdThenUsesSessionIdGenerator() { assertThat(newSessionId).isEqualTo("test"); } + @Test + void findByIdWhenRedisSessionMapperReturnsNullThenReturnsNull() { + String sessionId = "session-id"; + given(this.redisOperations.boundHashOps(getKey(sessionId))) + .willReturn(this.boundHashOperations); + Map map = map(RedisSessionMapper.CREATION_TIME_KEY, Instant.EPOCH.toEpochMilli(), + RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY, 1, RedisSessionMapper.LAST_ACCESSED_TIME_KEY, + Instant.now().toEpochMilli()); + given(this.boundHashOperations.entries()).willReturn(map); + this.redisRepository.setRedisSessionMapper((id, entries) -> null); + + assertThat(this.redisRepository.findById(sessionId)).isNull(); + } + private String getKey(String id) { return "spring:session:sessions:" + id; } diff --git a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisSessionRepositoryTests.java b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisSessionRepositoryTests.java index e851da723..4db9b02db 100644 --- a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisSessionRepositoryTests.java +++ b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisSessionRepositoryTests.java @@ -399,6 +399,17 @@ void findByIdWhenChangeSessionIdThenUsesSessionIdGenerator() { assertThat(session.changeSessionId()).isEqualTo("test"); } + @Test + void findByIdWhenRedisSessionMapperReturnsNullThenReturnsNull() { + given(this.sessionHashOperations.entries(eq(TEST_SESSION_KEY))) + .willReturn(mapOf(RedisSessionMapper.CREATION_TIME_KEY, Instant.EPOCH.toEpochMilli(), + RedisSessionMapper.LAST_ACCESSED_TIME_KEY, Instant.now().toEpochMilli(), + RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY, MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS)); + this.sessionRepository.setRedisSessionMapper((id, entries) -> null); + + assertThat(this.sessionRepository.findById(TEST_SESSION_ID)).isNull(); + } + private static String getSessionKey(String sessionId) { return "spring:session:sessions:" + sessionId; }