spring-projects / spring-projects/spring-session

RedisIndexedSessionRepository nullability issues

Open Beginner friendly
#3,857 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage type: bug
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.