Fix attribute mapping in ReactiveRedisOperationsSessionRepository

This commit ensures that attributes with null values are correctly mapped to session on retrieval from Redis.

Closes gh-1035
This commit is contained in:
Vedran Pavic
2018-04-19 15:31:08 +02:00
parent 6f8359ba16
commit 91b4efc5bd
3 changed files with 25 additions and 15 deletions

View File

@@ -22,7 +22,6 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
@@ -149,8 +148,7 @@ public class ReactiveRedisOperationsSessionRepository implements
String sessionKey = getSessionKey(id);
return this.sessionRedisOperations.opsForHash().entries(sessionKey)
.collect(
Collectors.toMap(e -> e.getKey().toString(), Map.Entry::getValue))
.collectMap(e -> e.getKey().toString(), Map.Entry::getValue)
.filter(map -> !map.isEmpty()).map(new SessionMapper(id))
.filter(session -> !session.isExpired()).map(RedisSession::new)
.switchIfEmpty(Mono.defer(() -> deleteById(id).then(Mono.empty())));

View File

@@ -305,13 +305,17 @@ public class ReactiveRedisOperationsSessionRepositoryTests {
@SuppressWarnings("unchecked")
public void getSessionFound() {
given(this.redisOperations.opsForHash()).willReturn(this.hashOperations);
String attrName = "attrName";
String attribute1 = "attribute1";
String attribute2 = "attribute2";
MapSession expected = new MapSession("test");
expected.setLastAccessedTime(Instant.now().minusSeconds(60));
expected.setAttribute(attrName, "attrValue");
expected.setAttribute(attribute1, "test");
expected.setAttribute(attribute2, null);
Map map = map(
ReactiveRedisOperationsSessionRepository.ATTRIBUTE_PREFIX + attrName,
expected.getAttribute(attrName),
ReactiveRedisOperationsSessionRepository.ATTRIBUTE_PREFIX + attribute1,
expected.getAttribute(attribute1),
ReactiveRedisOperationsSessionRepository.ATTRIBUTE_PREFIX + attribute2,
expected.getAttribute(attribute2),
ReactiveRedisOperationsSessionRepository.CREATION_TIME_KEY,
expected.getCreationTime().toEpochMilli(),
ReactiveRedisOperationsSessionRepository.MAX_INACTIVE_INTERVAL_KEY,
@@ -330,8 +334,10 @@ public class ReactiveRedisOperationsSessionRepositoryTests {
assertThat(session.getId()).isEqualTo(expected.getId());
assertThat(session.getAttributeNames())
.isEqualTo(expected.getAttributeNames());
assertThat(session.<String>getAttribute(attrName))
.isEqualTo(expected.getAttribute(attrName));
assertThat(session.<String>getAttribute(attribute1))
.isEqualTo(expected.getAttribute(attribute1));
assertThat(session.<String>getAttribute(attribute2))
.isEqualTo(expected.getAttribute(attribute2));
assertThat(session.getCreationTime()).isEqualTo(expected.getCreationTime());
assertThat(session.getMaxInactiveInterval())
.isEqualTo(expected.getMaxInactiveInterval());

View File

@@ -370,14 +370,18 @@ public class RedisOperationsSessionRepositoryTests {
@Test
public void getSessionFound() {
String attrName = "attrName";
String attribute1 = "attribute1";
String attribute2 = "attribute2";
MapSession expected = new MapSession();
expected.setLastAccessedTime(Instant.now().minusSeconds(60));
expected.setAttribute(attrName, "attrValue");
expected.setAttribute(attribute1, "test");
expected.setAttribute(attribute2, null);
given(this.redisOperations.boundHashOps(getKey(expected.getId())))
.willReturn(this.boundHashOperations);
Map map = map(RedisOperationsSessionRepository.getSessionAttrNameKey(attrName),
expected.getAttribute(attrName),
Map map = map(RedisOperationsSessionRepository.getSessionAttrNameKey(attribute1),
expected.getAttribute(attribute1),
RedisOperationsSessionRepository.getSessionAttrNameKey(attribute2),
expected.getAttribute(attribute2),
RedisOperationsSessionRepository.CREATION_TIME_ATTR,
expected.getCreationTime().toEpochMilli(),
RedisOperationsSessionRepository.MAX_INACTIVE_ATTR,
@@ -389,8 +393,10 @@ public class RedisOperationsSessionRepositoryTests {
RedisSession session = this.redisRepository.findById(expected.getId());
assertThat(session.getId()).isEqualTo(expected.getId());
assertThat(session.getAttributeNames()).isEqualTo(expected.getAttributeNames());
assertThat(session.<String>getAttribute(attrName))
.isEqualTo(expected.getAttribute(attrName));
assertThat(session.<String>getAttribute(attribute1))
.isEqualTo(expected.getAttribute(attribute1));
assertThat(session.<String>getAttribute(attribute2))
.isEqualTo(expected.getAttribute(attribute2));
assertThat(session.getCreationTime()).isEqualTo(expected.getCreationTime());
assertThat(session.getMaxInactiveInterval())
.isEqualTo(expected.getMaxInactiveInterval());