spring-projects / spring-projects/spring-session
RedisIndexedSessionRepository nullability issues
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 1.9k
- Forks
- 1.2k
- Avg merge
- 4h 27m
- Merged PRs (30d)
- 55
Description
Describe the bug
When upgrading to spring-session-data-redis 4.1.0 we can no longer call org.springframework.session.data.redis.RedisIndexedSessionRepository#setRedisSessionMapper to set a custom session mapper in Kotlin.
This is because the signature of redisSessionMapper expects the BiFunction to return a non-null MapSession, whereas before this was not the case (the result could be a null session).
Observing org.springframework.session.data.redis.RedisIndexedSessionRepository#getSession we see that the result of the BiFunction is expected to be a nullable value:
private @Nullable RedisSession getSession(String id, boolean allowExpired) {
// omitted for brevity
MapSession loaded = this.redisSessionMapper.apply(id, entries);
if (loaded == null || (!allowExpired && loaded.isExpired())) {
return null;
}
// omitted for brevity
}
To Reproduce
The custom mapper below fails to compile in Kotlin because it returns null when loading an invalid session causes an IllegalStateException to be thrown:
@Bean
fun redisSessionRepositoryCustomizer() =
SessionRepositoryCustomizer<RedisIndexedSessionRepository> { redisSessionRepository ->
val delegate = RedisSessionMapper()
redisSessionRepository.setRedisSessionMapper { sessionId, map ->
try {
delegate.apply(sessionId, map)
} catch (_: IllegalStateException) {
redisSessionRepository.sessionRedisOperations.delete("${sessionDataRedisProperties.namespace}:sessions:$sessionId")
null
}
}
}
Expected behavior
The custom mapper above should compile in Kotlin.
Sample
N/A
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java at setRedisSessionMapper and getSession. Check the mapper's nullability against the nullable result handled by getSession, then verify that the Kotlin custom mapper in the issue compiles while invalid sessions can still return null.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, kotlin, redis, spring
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 68/100