Skip to content

Commit c833771

Browse files
committed
Fix 3105 , make invoke command with Json string parameter without "class" key
1 parent 2911b31 commit c833771

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokeTelnetHandler.java

+10-15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.apache.dubbo.rpc.protocol.dubbo.telnet;
1818

19+
import com.alibaba.fastjson.JSON;
20+
import com.alibaba.fastjson.JSONObject;
1921
import org.apache.dubbo.common.extension.Activate;
2022
import org.apache.dubbo.common.utils.ReflectUtils;
2123
import org.apache.dubbo.common.utils.StringUtils;
@@ -27,14 +29,10 @@
2729
import org.apache.dubbo.rpc.model.ProviderMethodModel;
2830
import org.apache.dubbo.rpc.model.ProviderModel;
2931

30-
import com.alibaba.fastjson.JSON;
31-
import com.alibaba.fastjson.JSONObject;
32-
3332
import java.lang.reflect.Method;
3433
import java.util.ArrayList;
3534
import java.util.Collection;
3635
import java.util.List;
37-
import java.util.Map;
3836

3937
import static org.apache.dubbo.common.utils.PojoUtils.realize;
4038
import static org.apache.dubbo.rpc.RpcContext.getContext;
@@ -50,19 +48,19 @@ private static Method findMethod(List<ProviderMethodModel> methods, String metho
5048
Class<?>[] paramTypes) {
5149
for (ProviderMethodModel model : methods) {
5250
Method m = model.getMethod();
53-
if (isMatch(m, args, paramTypes,method)) {
51+
if (isMatch(m, args, paramTypes, method)) {
5452
return m;
5553
}
5654
}
5755
return null;
5856
}
5957

60-
private static boolean isMatch(Method method,List<Object> args, Class<?>[] paramClasses,String lookupMethodName) {
61-
if(!method.getName().equals(lookupMethodName)) {
58+
private static boolean isMatch(Method method, List<Object> args, Class<?>[] paramClasses, String lookupMethodName) {
59+
if (!method.getName().equals(lookupMethodName)) {
6260
return false;
6361
}
6462

65-
Class<?> types[]=method.getParameterTypes();
63+
Class<?> types[] = method.getParameterTypes();
6664
if (types.length != args.size()) {
6765
return false;
6866
}
@@ -99,13 +97,10 @@ private static boolean isMatch(Method method,List<Object> args, Class<?>[] param
9997
if (!ReflectUtils.isCompatible(type, arg)) {
10098
return false;
10199
}
102-
} else if (arg instanceof Map) {
103-
String name = (String) ((Map<?, ?>) arg).get("class");
104-
Class<?> cls = arg.getClass();
105-
if (name != null && name.length() > 0) {
106-
cls = ReflectUtils.forName(name);
107-
}
108-
if (!type.isAssignableFrom(cls)) {
100+
} else if (arg instanceof JSONObject) {
101+
try {
102+
((JSONObject) arg).toJavaObject(type);
103+
} catch (Exception ex) {
109104
return false;
110105
}
111106
} else if (arg instanceof Collection) {

dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoServiceImpl.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public Type enumlength(Type... types) {
7171
return Type.Lower;
7272
return types[0];
7373
}
74-
74+
7575
public Type getType(Type type) {
7676
return type;
7777
}
@@ -109,12 +109,12 @@ public long add(int a, long b) {
109109

110110
@Override
111111
public int getPerson(Person person) {
112-
return 1;
112+
return person.getAge();
113113
}
114114

115115
@Override
116116
public int getPerson(Person person1, Person perso2) {
117-
return 2;
117+
return person1.getAge() + perso2.getAge();
118118
}
119119

120120
}

dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.apache.dubbo.rpc.protocol.dubbo.support.DemoService;
2626
import org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl;
2727
import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils;
28-
2928
import org.junit.After;
3029
import org.junit.Before;
3130
import org.junit.Test;
@@ -198,6 +197,34 @@ public void testInvokeAutoFindMethod() throws RemotingException {
198197
assertTrue(result.contains("ok"));
199198
}
200199

200+
@Test
201+
public void testInvokeJsonParamMethod() throws RemotingException {
202+
mockChannel = mock(Channel.class);
203+
given(mockChannel.getAttribute("telnet.service")).willReturn(null);
204+
given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555"));
205+
given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886"));
206+
207+
ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), DemoService.class);
208+
ApplicationModel.initProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", providerModel);
209+
String param = "{\"name\":\"Dubbo\",\"age\":8}";
210+
String result = invoke.telnet(mockChannel, "getPerson(" + param + ")");
211+
assertTrue(result.contains("8"));
212+
}
213+
214+
@Test
215+
public void testInvokeMultiJsonParamMethod() throws RemotingException {
216+
mockChannel = mock(Channel.class);
217+
given(mockChannel.getAttribute("telnet.service")).willReturn(null);
218+
given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555"));
219+
given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886"));
220+
221+
ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), DemoService.class);
222+
ApplicationModel.initProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", providerModel);
223+
String param = "{\"name\":\"Dubbo\",\"age\":8},{\"name\":\"Apache\",\"age\":20}";
224+
String result = invoke.telnet(mockChannel, "getPerson(" + param + ")");
225+
assertTrue(result.contains("28"));
226+
}
227+
201228
@Test
202229
public void testMessageNull() throws RemotingException {
203230
mockChannel = mock(Channel.class);

0 commit comments

Comments
 (0)