Skip to content

Commit

Permalink
Fix GraphQL WebSocket HandlerMapping bean ordering
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bclozel committed Oct 16, 2023
1 parent 4c3a0f0 commit 339f75d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public HandlerMapping graphQlWebSocketMapping(GraphQlWebSocketHandler handler, G
mapping.setWebSocketUpgradeMatch(true);
mapping.setUrlMap(Collections.singletonMap(path,
handler.initWebSocketHttpRequestHandler(new DefaultHandshakeHandler())));
mapping.setOrder(2); // Ahead of HTTP endpoint ("routerFunctionMapping" bean)
mapping.setOrder(-2); // Ahead of HTTP endpoint ("routerFunctionMapping" bean)
return mapping;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.support.RouterFunctionMapping;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.socket.server.support.WebSocketHandlerMapping;

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

@Test
void shouldConfigureWebSocketBeans() {
this.contextRunner.withPropertyValues("spring.graphql.websocket.path=/ws")
.run((context) -> assertThat(context).hasSingleBean(GraphQlWebSocketHandler.class));
this.contextRunner.withPropertyValues("spring.graphql.websocket.path=/ws").run((context) -> {
assertThat(context).hasSingleBean(GraphQlWebSocketHandler.class);
assertThat(context.getBeanProvider(HandlerMapping.class).orderedStream().toList()).containsSubsequence(
context.getBean(WebSocketHandlerMapping.class), context.getBean(RouterFunctionMapping.class),
context.getBean(RequestMappingHandlerMapping.class));
});
}

@Test
Expand Down

0 comments on commit 339f75d

Please sign in to comment.