From 178ca877e746460f3f420b20a553fbba21d69c90 Mon Sep 17 00:00:00 2001 From: hengyuss <1183660933@qq.com> Date: Sun, 20 Sep 2026 17:43:40 +0800 Subject: [PATCH] fix(websocket): propagate registerMetaData from annotation to registration DTO --- .../SpringWebSocketClientEventListener.java | 1 + ...pringWebSocketClientEventListenerTest.java | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/shenyu-client/shenyu-client-websocket/shenyu-client-spring-websocket/src/main/java/org/apache/shenyu/client/spring/websocket/init/SpringWebSocketClientEventListener.java b/shenyu-client/shenyu-client-websocket/shenyu-client-spring-websocket/src/main/java/org/apache/shenyu/client/spring/websocket/init/SpringWebSocketClientEventListener.java index ee621e56abf7..5a3e7c87f739 100644 --- a/shenyu-client/shenyu-client-websocket/shenyu-client-spring-websocket/src/main/java/org/apache/shenyu/client/spring/websocket/init/SpringWebSocketClientEventListener.java +++ b/shenyu-client/shenyu-client-websocket/shenyu-client-spring-websocket/src/main/java/org/apache/shenyu/client/spring/websocket/init/SpringWebSocketClientEventListener.java @@ -228,6 +228,7 @@ protected MetaDataRegisterDTO buildMetaDataDTO(final Object bean, @NonNull final .rpcType(RpcTypeEnum.WEB_SOCKET.getName()) .enabled(true) .ruleName(StringUtils.defaultIfBlank(webSocketClient.ruleName(), getContextPath())) + .registerMetaData(webSocketClient.registerMetaData()) .namespaceId(namespaceId) .build(); } diff --git a/shenyu-client/shenyu-client-websocket/shenyu-client-spring-websocket/src/test/java/org/apache/shenyu/client/spring/websocket/init/SpringWebSocketClientEventListenerTest.java b/shenyu-client/shenyu-client-websocket/shenyu-client-spring-websocket/src/test/java/org/apache/shenyu/client/spring/websocket/init/SpringWebSocketClientEventListenerTest.java index 1c5de5420e8b..ed0e73154822 100644 --- a/shenyu-client/shenyu-client-websocket/shenyu-client-spring-websocket/src/test/java/org/apache/shenyu/client/spring/websocket/init/SpringWebSocketClientEventListenerTest.java +++ b/shenyu-client/shenyu-client-websocket/shenyu-client-spring-websocket/src/test/java/org/apache/shenyu/client/spring/websocket/init/SpringWebSocketClientEventListenerTest.java @@ -19,6 +19,7 @@ import org.apache.shenyu.client.core.constant.ShenyuClientConstants; import org.apache.shenyu.client.core.disruptor.ShenyuClientRegisterEventPublisher; +import org.apache.shenyu.client.spring.websocket.annotation.ShenyuServerEndpoint; import org.apache.shenyu.client.spring.websocket.annotation.ShenyuSpringWebSocketClient; import org.apache.shenyu.common.constant.Constants; import org.apache.shenyu.common.enums.RpcTypeEnum; @@ -31,6 +32,10 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockedStatic; import org.mockito.junit.jupiter.MockitoExtension; @@ -44,11 +49,13 @@ import java.util.HashMap; import java.util.Map; import java.util.Properties; +import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.never; @@ -119,6 +126,35 @@ public void testHandle() { eventListener.handle("mock", mockClass); } + @ParameterizedTest + @MethodSource("metadataRegistrationCases") + void testHandlePreservesRegisterMetaData(final Object bean, final boolean registerMetaData) { + try (MockedStatic publisherMockedStatic = mockStatic(ShenyuClientRegisterEventPublisher.class)) { + publisherMockedStatic.when(ShenyuClientRegisterEventPublisher::getInstance).thenReturn(publisher); + SpringWebSocketClientEventListener listener = buildEventListener(false); + + listener.handle("endpoint", bean); + + ArgumentCaptor captor = ArgumentCaptor.forClass(MetaDataRegisterDTO.class); + verify(publisher, atLeastOnce()).publishEvent(captor.capture()); + for (MetaDataRegisterDTO metadata : captor.getAllValues()) { + assertEquals(registerMetaData, metadata.isRegisterMetaData()); + assertEquals(RpcTypeEnum.WEB_SOCKET.getName(), metadata.getRpcType()); + assertEquals("/contextPath", metadata.getContextPath()); + } + } + } + + private static Stream metadataRegistrationCases() { + return Stream.of( + Arguments.of(new MockClass(), false), + Arguments.of(new MetadataEnabledClient(), true), + Arguments.of(new MetadataDisabledClient(), false), + Arguments.of(new DefaultEndpoint(), false), + Arguments.of(new MetadataEnabledEndpoint(), true), + Arguments.of(new MetadataDisabledEndpoint(), false)); + } + @Test public void testBuildApiSuperPath() { String annotationPath = "/path"; @@ -209,4 +245,34 @@ public void mockMethod() { } } + @ShenyuSpringWebSocketClient(registerMetaData = true) + private static class MetadataEnabledClient { + public void onMessage() { + } + } + + @ShenyuSpringWebSocketClient(registerMetaData = false) + private static class MetadataDisabledClient { + public void onMessage() { + } + } + + @ShenyuServerEndpoint("/default") + private static class DefaultEndpoint { + public void onMessage() { + } + } + + @ShenyuServerEndpoint(value = "/enabled", registerMetaData = true) + private static class MetadataEnabledEndpoint { + public void onMessage() { + } + } + + @ShenyuServerEndpoint(value = "/disabled", registerMetaData = false) + private static class MetadataDisabledEndpoint { + public void onMessage() { + } + } + }