Skip to content

RedisIndexedSessionRepository nullability issues #3857

Description

@jpmsilva

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions