diff --git a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ReflectionServiceDescriptor.java b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ReflectionServiceDescriptor.java index 629d6c0385..489f7e4c6c 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ReflectionServiceDescriptor.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ReflectionServiceDescriptor.java @@ -75,7 +75,18 @@ public class ReflectionServiceDescriptor implements ServiceDescriptor { methods.forEach((methodName, methodList) -> { Map descMap = descToMethods.computeIfAbsent(methodName, k -> new HashMap<>()); - methodList.forEach(methodModel -> descMap.put(methodModel.getParamDesc(), methodModel)); + // not support BI_STREAM and SERVER_STREAM at the same time, for example, + // void foo(Request, StreamObserver) ---> SERVER_STREAM + // StreamObserver foo(StreamObserver) ---> BI_STREAM + long streamMethodCount = methodList.stream() + .peek(methodModel -> descMap.put(methodModel.getParamDesc(), methodModel)) + .map(MethodDescriptor::getRpcType) + .filter(rpcType -> rpcType == MethodDescriptor.RpcType.SERVER_STREAM + || rpcType == MethodDescriptor.RpcType.BI_STREAM) + .count(); + if (streamMethodCount > 1L) + throw new IllegalStateException("Stream method could not be overloaded.There are " + streamMethodCount + +" stream method signatures. method(" + methodName + ")"); }); } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/ReflectionServiceDescriptorTest.java b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/ReflectionServiceDescriptorTest.java index 3361312e1f..b7011526be 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/ReflectionServiceDescriptorTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/ReflectionServiceDescriptorTest.java @@ -21,6 +21,7 @@ import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.metadata.definition.TypeDefinitionBuilder; import org.apache.dubbo.rpc.support.DemoService; +import org.apache.dubbo.rpc.support.DemoService1; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -42,6 +43,16 @@ class ReflectionServiceDescriptorTest { Assertions.assertEquals(1, service2.getMethods("sayHello2").size()); } + @Test + void testStreamRpcTypeException() { + try { + new ReflectionServiceDescriptor(DemoService1.class); + } catch (IllegalStateException e) { + Assertions.assertTrue(e.getMessage() + .contains("Stream method could not be overloaded.")); + } + } + @Test void getFullServiceDefinition() { TypeDefinitionBuilder.initBuilders(new FrameworkModel()); diff --git a/dubbo-common/src/test/java/org/apache/dubbo/rpc/support/DemoService1.java b/dubbo-common/src/test/java/org/apache/dubbo/rpc/support/DemoService1.java new file mode 100644 index 0000000000..1251c7cce9 --- /dev/null +++ b/dubbo-common/src/test/java/org/apache/dubbo/rpc/support/DemoService1.java @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.rpc.support; + +import org.apache.dubbo.common.stream.StreamObserver; + +public interface DemoService1 { + StreamObserver sayHello(StreamObserver request); + + void sayHello(String msg, StreamObserver request); +} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/rpc/support/DemoService1Impl.java b/dubbo-common/src/test/java/org/apache/dubbo/rpc/support/DemoService1Impl.java new file mode 100644 index 0000000000..7b5ec906fb --- /dev/null +++ b/dubbo-common/src/test/java/org/apache/dubbo/rpc/support/DemoService1Impl.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.rpc.support; + +import org.apache.dubbo.common.stream.StreamObserver; + +public class DemoService1Impl implements DemoService1{ + @Override + public StreamObserver sayHello(StreamObserver request) { + request.onNext("BI_STREAM"); + return request; + } + + @Override + public void sayHello(String msg, StreamObserver request) { + request.onNext(msg); + request.onNext("SERVER_STREAM"); + request.onCompleted(); + } +}