Skip to content

Commit 339f75d

Browse files
committed
Fix GraphQL WebSocket HandlerMapping bean ordering
Prior to this commit, the GraphQL WebSocket HandlerMapping bean would be ordered at position "2", before the RouterFunction variant defined by Spring Framework at position "3". Since then, the Spring Framework team changed the default order value for this one at "-1", see spring-projects/spring-framework#30278. This prevents the WebSocket upgrade, as the request is handled by the RouterFunction instead of the WebSocket handler. This commit updates the handlermapping order and introduces a test to prevent issues in the future. Fixes gh-37892
1 parent 4c3a0f0 commit 339f75d

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/servlet/GraphQlWebMvcAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public HandlerMapping graphQlWebSocketMapping(GraphQlWebSocketHandler handler, G
195195
mapping.setWebSocketUpgradeMatch(true);
196196
mapping.setUrlMap(Collections.singletonMap(path,
197197
handler.initWebSocketHttpRequestHandler(new DefaultHandshakeHandler())));
198-
mapping.setOrder(2); // Ahead of HTTP endpoint ("routerFunctionMapping" bean)
198+
mapping.setOrder(-2); // Ahead of HTTP endpoint ("routerFunctionMapping" bean)
199199
return mapping;
200200
}
201201

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/servlet/GraphQlWebMvcAutoConfigurationTests.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@
4545
import org.springframework.test.web.servlet.MockMvc;
4646
import org.springframework.test.web.servlet.MvcResult;
4747
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
48+
import org.springframework.web.servlet.HandlerMapping;
4849
import org.springframework.web.servlet.function.RouterFunction;
50+
import org.springframework.web.servlet.function.support.RouterFunctionMapping;
51+
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
52+
import org.springframework.web.socket.server.support.WebSocketHandlerMapping;
4953

5054
import static org.assertj.core.api.Assertions.assertThat;
5155
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
@@ -162,8 +166,12 @@ void shouldSupportCors() {
162166

163167
@Test
164168
void shouldConfigureWebSocketBeans() {
165-
this.contextRunner.withPropertyValues("spring.graphql.websocket.path=/ws")
166-
.run((context) -> assertThat(context).hasSingleBean(GraphQlWebSocketHandler.class));
169+
this.contextRunner.withPropertyValues("spring.graphql.websocket.path=/ws").run((context) -> {
170+
assertThat(context).hasSingleBean(GraphQlWebSocketHandler.class);
171+
assertThat(context.getBeanProvider(HandlerMapping.class).orderedStream().toList()).containsSubsequence(
172+
context.getBean(WebSocketHandlerMapping.class), context.getBean(RouterFunctionMapping.class),
173+
context.getBean(RequestMappingHandlerMapping.class));
174+
});
167175
}
168176

169177
@Test

0 commit comments

Comments
 (0)