Skip to content

Commit a7fac3c

Browse files
committed
Merge pull request #11202 from ptahchiev:11201
* pr/11202: Polish "Fix NullPointer when requesting a session that does not exist" Fix NullPointer when requesting a session that does not exist
2 parents 6cae925 + b6609ff commit a7fac3c

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/session/SessionsEndpoint.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public SessionsReport sessionsForUsername(String username) {
6060
@ReadOperation
6161
public SessionDescriptor getSession(@Selector String sessionId) {
6262
Session session = this.sessionRepository.findById(sessionId);
63+
if (session == null) {
64+
return null;
65+
}
6366
return new SessionDescriptor(session);
6467
}
6568

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/session/SessionsEndpointTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ public void getSession() {
8181
assertThat(result.isExpired()).isEqualTo(session.isExpired());
8282
}
8383

84+
@Test
85+
public void getSessionWithIdNotFound() {
86+
given(this.repository.findById("not-found")).willReturn(null);
87+
assertThat(this.endpoint.getSession("not-found")).isNull();
88+
}
89+
8490
@Test
8591
public void deleteSession() {
8692
this.endpoint.deleteSession(session.getId());

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/session/SessionsEndpointWebIntegrationTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ public void sessionsForUsernameFound() throws Exception {
8080
.isEqualTo(new JSONArray().appendElement(session.getId()));
8181
}
8282

83+
@Test
84+
public void sessionForIdNotFound() {
85+
client.get()
86+
.uri((builder) -> builder.path(
87+
"/actuator/sessions/session-id-not-found").build())
88+
.exchange().expectStatus().isNotFound();
89+
}
90+
8391
@Configuration
8492
protected static class TestConfiguration {
8593

0 commit comments

Comments
 (0)