Skip to content

Commit ef733e0

Browse files
Merge branch '2.7.x' into 3.0.x
Closes gh-2308
2 parents 41eb118 + ffe7fd3 commit ef733e0

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

spring-session-data-redis/src/integration-test/java/org/springframework/session/data/redis/ReactiveRedisSessionRepositoryITests.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,15 +24,21 @@
2424

2525
import org.springframework.beans.factory.annotation.Autowired;
2626
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.data.redis.core.ReactiveRedisOperations;
2728
import org.springframework.session.Session;
2829
import org.springframework.session.data.redis.ReactiveRedisSessionRepository.RedisSession;
2930
import org.springframework.session.data.redis.config.annotation.web.server.EnableRedisWebSession;
3031
import org.springframework.test.context.ContextConfiguration;
3132
import org.springframework.test.context.junit.jupiter.SpringExtension;
3233
import org.springframework.test.context.web.WebAppConfiguration;
34+
import org.springframework.test.util.ReflectionTestUtils;
3335

3436
import static org.assertj.core.api.Assertions.assertThat;
3537
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
38+
import static org.mockito.ArgumentMatchers.endsWith;
39+
import static org.mockito.BDDMockito.given;
40+
import static org.mockito.Mockito.reset;
41+
import static org.mockito.Mockito.spy;
3642

3743
/**
3844
* Integration tests for {@link ReactiveRedisSessionRepository}.
@@ -215,6 +221,30 @@ void changeSessionSaveOldSessionInstance() {
215221
assertThat(this.repository.findById(session.getId()).block()).isNotNull();
216222
}
217223

224+
// gh-2281
225+
@Test
226+
@SuppressWarnings("unchecked")
227+
void saveChangeSessionIdAfterCheckWhenOriginalKeyDoesNotExistsThenIgnoreError() {
228+
ReactiveRedisOperations<String, Object> sessionRedisOperations = (ReactiveRedisOperations<String, Object>) ReflectionTestUtils
229+
.getField(this.repository, "sessionRedisOperations");
230+
ReactiveRedisOperations<String, Object> spyOperations = spy(sessionRedisOperations);
231+
ReflectionTestUtils.setField(this.repository, "sessionRedisOperations", spyOperations);
232+
233+
RedisSession toSave = this.repository.createSession().block();
234+
String sessionId = toSave.getId();
235+
236+
given(spyOperations.hasKey(endsWith(sessionId))).willReturn(Mono.just(true));
237+
238+
this.repository.save(toSave).block();
239+
RedisSession session = this.repository.findById(sessionId).block();
240+
this.repository.deleteById(sessionId).block();
241+
String newSessionId = session.changeSessionId();
242+
this.repository.save(session).block();
243+
assertThat(this.repository.findById(sessionId).block()).isNull();
244+
assertThat(this.repository.findById(newSessionId).block()).isNull();
245+
reset(spyOperations);
246+
}
247+
218248
@Configuration
219249
@EnableRedisWebSession
220250
static class Config extends BaseConfig {

spring-session-data-redis/src/main/java/org/springframework/session/data/redis/ReactiveRedisSessionRepository.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2022 the original author or authors.
2+
* Copyright 2014-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,12 +25,14 @@
2525
import org.reactivestreams.Publisher;
2626
import reactor.core.publisher.Mono;
2727

28+
import org.springframework.core.NestedExceptionUtils;
2829
import org.springframework.data.redis.core.ReactiveRedisOperations;
2930
import org.springframework.session.MapSession;
3031
import org.springframework.session.ReactiveSessionRepository;
3132
import org.springframework.session.SaveMode;
3233
import org.springframework.session.Session;
3334
import org.springframework.util.Assert;
35+
import org.springframework.util.StringUtils;
3436

3537
/**
3638
* A {@link ReactiveSessionRepository} that is implemented using Spring Data's
@@ -317,7 +319,10 @@ private Mono<Void> saveChangeSessionId() {
317319
String sessionKey = getSessionKey(sessionId);
318320

319321
return ReactiveRedisSessionRepository.this.sessionRedisOperations.rename(originalSessionKey, sessionKey)
320-
.and(replaceSessionId);
322+
.and(replaceSessionId).onErrorResume((ex) -> {
323+
String message = NestedExceptionUtils.getMostSpecificCause(ex).getMessage();
324+
return StringUtils.startsWithIgnoreCase(message, "ERR no such key");
325+
}, (ex) -> Mono.empty());
321326
}
322327
}
323328

0 commit comments

Comments
 (0)