Skip to content

Commit 6ec002d

Browse files
authored
bugfix: application/x-www-form-urlencoded There is a problem with parameter parsing (#14700)
* bugfix: application/x-www-form-urlencoded There is a problem with parameter parsing * bugfix: application/x-www-form-urlencoded There is a problem with parameter parsing
1 parent ef253c9 commit 6ec002d

14 files changed

Lines changed: 43 additions & 41 deletions

File tree

dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/ArgInfo.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ public Type actualReflectType() {
160160
return actualType;
161161
}
162162

163+
public void setActualType(Type actualType) {
164+
this.actualType = actualType;
165+
}
166+
163167
@Override
164168
public String toString() {
165169
return "ArgInfo{" + "index="

dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestInvoker.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.apache.dubbo.common.URL;
2020
import org.apache.dubbo.metadata.ParameterTypesComparator;
21+
import org.apache.dubbo.metadata.rest.ArgInfo;
2122
import org.apache.dubbo.metadata.rest.RestMethodMetadata;
2223
import org.apache.dubbo.metadata.rest.ServiceRestMetadata;
2324
import org.apache.dubbo.metadata.rest.media.MediaType;
@@ -109,11 +110,10 @@ protected Result doInvoke(Invocation invocation) {
109110
Method reflectMethod = restMethodMetadata.getReflectMethod();
110111
mediaType =
111112
MediaTypeUtil.convertMediaType(reflectMethod.getReturnType(), r.getContentType());
112-
Object value = HttpMessageCodecManager.httpMessageDecode(
113-
r.getBody(),
114-
reflectMethod.getReturnType(),
115-
reflectMethod.getGenericReturnType(),
116-
mediaType);
113+
ArgInfo argInfo = new ArgInfo();
114+
argInfo.setParamType(reflectMethod.getReturnType());
115+
argInfo.setActualType(reflectMethod.getGenericReturnType());
116+
Object value = HttpMessageCodecManager.httpMessageDecode(r.getBody(), argInfo, mediaType);
117117
appResponse.setValue(value);
118118
// resolve response attribute & attachment
119119
HttpHeaderUtil.parseResponseHeader(appResponse, r);

dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/param/parse/provider/BodyProviderParamParser.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ protected void doParse(ProviderParseContext parseContext, ArgInfo argInfo) {
4141
try {
4242
String contentType = parseContext.getRequestFacade().getHeader(RestHeaderEnum.CONTENT_TYPE.getHeader());
4343
MediaType mediaType = MediaTypeUtil.convertMediaType(argInfo.getParamType(), contentType);
44-
Object param = HttpMessageCodecManager.httpMessageDecode(
45-
request.getInputStream(), argInfo.getParamType(), argInfo.actualReflectType(), mediaType);
44+
Object param = HttpMessageCodecManager.httpMessageDecode(request.getInputStream(), argInfo, mediaType);
4645
parseContext.setValueByIndex(argInfo.getIndex(), param);
4746
} catch (Throwable e) {
4847
throw new ParamParseException("dubbo rest protocol provider body param parser error: " + e.getMessage());

dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/param/parse/provider/ParamProviderParamParser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ protected void doParse(ProviderParseContext parseContext, ArgInfo argInfo) {
5353
String param = request.getParameter(argInfo.getAnnotationNameAttribute());
5454

5555
Object paramValue = paramTypeConvert(argInfo.getParamType(), param);
56-
parseContext.setValueByIndex(argInfo.getIndex(), paramValue);
56+
if (paramValue != null) {
57+
parseContext.setValueByIndex(argInfo.getIndex(), paramValue);
58+
}
5759
}
5860

5961
@Override

dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/HttpMessageCodecManager.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,29 @@
1717
package org.apache.dubbo.rpc.protocol.rest.message;
1818

1919
import org.apache.dubbo.common.URL;
20+
import org.apache.dubbo.metadata.rest.ArgInfo;
2021
import org.apache.dubbo.metadata.rest.media.MediaType;
2122
import org.apache.dubbo.rpc.model.FrameworkModel;
2223
import org.apache.dubbo.rpc.protocol.rest.exception.UnSupportContentTypeException;
2324
import org.apache.dubbo.rpc.protocol.rest.pair.MessageCodecResultPair;
2425

2526
import java.io.OutputStream;
26-
import java.lang.reflect.Type;
2727
import java.util.Set;
2828

2929
public class HttpMessageCodecManager {
3030
private static final Set<HttpMessageCodec> httpMessageCodecs = FrameworkModel.defaultModel()
3131
.getExtensionLoader(HttpMessageCodec.class)
3232
.getSupportedExtensionInstances();
3333

34-
public static Object httpMessageDecode(byte[] body, Class<?> type, Type actualType, MediaType mediaType)
35-
throws Exception {
34+
public static Object httpMessageDecode(byte[] body, ArgInfo argInfo, MediaType mediaType) throws Exception {
35+
Class<?> type = argInfo.getParamType();
3636
if (body == null || body.length == 0) {
3737
return null;
3838
}
3939

4040
for (HttpMessageCodec httpMessageCodec : httpMessageCodecs) {
4141
if (httpMessageCodec.contentTypeSupport(mediaType, type) || typeJudge(mediaType, type, httpMessageCodec)) {
42-
return httpMessageCodec.decode(body, type, actualType);
42+
return httpMessageCodec.decode(body, argInfo);
4343
}
4444
}
4545
throw new UnSupportContentTypeException("UnSupport content-type :" + mediaType.value);

dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/HttpMessageDecode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
*/
1717
package org.apache.dubbo.rpc.protocol.rest.message;
1818

19-
import java.lang.reflect.Type;
19+
import org.apache.dubbo.metadata.rest.ArgInfo;
2020

2121
public interface HttpMessageDecode<InputStream> {
2222

23-
Object decode(InputStream body, Class<?> targetType, Type actualTYpe) throws Exception;
23+
Object decode(InputStream body, ArgInfo argInfo) throws Exception;
2424
}

dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/codec/ByteArrayCodec.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919
import org.apache.dubbo.common.URL;
2020
import org.apache.dubbo.common.extension.Activate;
21+
import org.apache.dubbo.metadata.rest.ArgInfo;
2122
import org.apache.dubbo.metadata.rest.media.MediaType;
2223
import org.apache.dubbo.rpc.protocol.rest.message.HttpMessageCodec;
2324

2425
import java.io.OutputStream;
25-
import java.lang.reflect.Type;
2626

2727
/**
2828
* body type is byte array
@@ -31,7 +31,7 @@
3131
public class ByteArrayCodec implements HttpMessageCodec<byte[], OutputStream> {
3232

3333
@Override
34-
public Object decode(byte[] body, Class<?> targetType, Type type) throws Exception {
34+
public Object decode(byte[] body, ArgInfo argInfo) throws Exception {
3535
return body;
3636
}
3737

dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/codec/JsonCodec.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
import org.apache.dubbo.common.URL;
2020
import org.apache.dubbo.common.extension.Activate;
2121
import org.apache.dubbo.common.utils.JsonUtils;
22+
import org.apache.dubbo.metadata.rest.ArgInfo;
2223
import org.apache.dubbo.metadata.rest.media.MediaType;
2324
import org.apache.dubbo.rpc.protocol.rest.message.HttpMessageCodec;
2425
import org.apache.dubbo.rpc.protocol.rest.message.MediaTypeMatcher;
2526
import org.apache.dubbo.rpc.protocol.rest.util.DataParseUtils;
2627

2728
import java.io.OutputStream;
28-
import java.lang.reflect.Type;
2929
import java.nio.charset.StandardCharsets;
3030
import java.util.HashSet;
3131
import java.util.Set;
@@ -46,8 +46,8 @@ public static void addUnSupportClass(Class<?> unSupportClass) {
4646
}
4747

4848
@Override
49-
public Object decode(byte[] body, Class<?> targetType, Type actualType) throws Exception {
50-
return DataParseUtils.jsonConvert(actualType, body);
49+
public Object decode(byte[] body, ArgInfo argInfo) throws Exception {
50+
return DataParseUtils.jsonConvert(argInfo.actualReflectType(), body);
5151
}
5252

5353
@Override

dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/codec/MultiValueCodec.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,16 @@
1919
import org.apache.dubbo.common.URL;
2020
import org.apache.dubbo.common.extension.Activate;
2121
import org.apache.dubbo.common.utils.ReflectUtils;
22+
import org.apache.dubbo.metadata.rest.ArgInfo;
2223
import org.apache.dubbo.metadata.rest.media.MediaType;
2324
import org.apache.dubbo.rpc.protocol.rest.message.HttpMessageCodec;
2425
import org.apache.dubbo.rpc.protocol.rest.message.MediaTypeMatcher;
2526
import org.apache.dubbo.rpc.protocol.rest.util.DataParseUtils;
2627

2728
import java.io.OutputStream;
2829
import java.lang.reflect.Field;
29-
import java.lang.reflect.Type;
30-
import java.util.ArrayList;
3130
import java.util.List;
3231
import java.util.Map;
33-
import java.util.Set;
3432

3533
/**
3634
* body is form
@@ -39,17 +37,15 @@
3937
public class MultiValueCodec implements HttpMessageCodec<byte[], OutputStream> {
4038

4139
@Override
42-
public Object decode(byte[] body, Class<?> targetType, Type type) throws Exception {
40+
public Object decode(byte[] body, ArgInfo argInfo) throws Exception {
41+
Class<?> targetType = argInfo.getParamType();
4342
Object map = DataParseUtils.multipartFormConvert(body, targetType);
4443
Map valuesMap = (Map) map;
4544
if (Map.class.isAssignableFrom(targetType)) {
4645
return map;
4746
} else if (DataParseUtils.isTextType(targetType)) {
4847

49-
// only fetch first
50-
Set set = valuesMap.keySet();
51-
ArrayList arrayList = new ArrayList<>(set);
52-
Object key = arrayList.get(0);
48+
String key = argInfo.getParamName();
5349
Object value = valuesMap.get(key);
5450
if (value == null) {
5551
return null;

dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/message/codec/ResteasyResponseCodec.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
import org.apache.dubbo.common.extension.Activate;
2121
import org.apache.dubbo.common.utils.ClassUtils;
2222
import org.apache.dubbo.common.utils.JsonUtils;
23+
import org.apache.dubbo.metadata.rest.ArgInfo;
2324
import org.apache.dubbo.metadata.rest.media.MediaType;
2425
import org.apache.dubbo.rpc.protocol.rest.message.HttpMessageCodec;
2526

2627
import java.io.OutputStream;
2728
import java.lang.reflect.Method;
28-
import java.lang.reflect.Type;
2929
import java.nio.charset.StandardCharsets;
3030

3131
@Activate(onClass = "javax.ws.rs.core.Response")
@@ -58,7 +58,7 @@ public MediaType contentType() {
5858
}
5959

6060
@Override
61-
public Object decode(byte[] body, Class<?> targetType, Type type) throws Exception {
61+
public Object decode(byte[] body, ArgInfo argInfo) throws Exception {
6262
if (null == body || body.length == 0) {
6363
return null;
6464
}

0 commit comments

Comments
 (0)