Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public Object getValue(RestServerRequest request) throws Exception {

String contentType = request.getContentType();
if (contentType != null && contentType.startsWith(MediaType.TEXT_PLAIN)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can get the encoding information from the content-type , just like this "Content-Type: text/html; charset=utf-8" .
I don't think using the "UTF-8" without checking the charset is good idea. We can use the default setting as UTF-8, but when user specify the charset, we need to take it in the consideration.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I add a TODO to fix this later. But we use a hard code now. Now UTF-8 is the correct one, because most URI encoding use UTF-8 and we use json as body, they are all UTF-8

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return IOUtils.toString(inputStream);
// TODO: we should consider body encoding
return IOUtils.toString(inputStream, "UTF-8");
}
return RestObjectMapper.INSTANCE.readValue(inputStream, targetType);
}
Expand Down Expand Up @@ -101,7 +102,8 @@ public Object getValue(RestServerRequest request) throws Exception {

if (InputStream.class.isInstance(body)) {
InputStream inputStream = (InputStream) body;
return IOUtils.toString(inputStream);
// TODO: we should consider body encoding
return IOUtils.toString(inputStream, "UTF-8");
}

return RestObjectMapper.INSTANCE.convertValue(body, targetType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.servicecomb.common.rest.codec.param;

import java.lang.reflect.Type;
import java.net.URLDecoder;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.type.TypeFactory;
Expand All @@ -39,7 +40,7 @@ public Object getValue(RestServerRequest request) throws Exception {
if (value == null) {
return null;
}
return convertValue(value, targetType);
return convertValue(URLDecoder.decode(value, "UTF-8"), targetType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ private static void testController(RestTemplate template, String microserviceNam
template.getForObject(prefix + "/controller/sayhi?name={name}",
String.class,
"world1"));
TestMgr.check("hi hi 中国 [hi 中国]",
template.getForObject(prefix + "/controller/sayhi?name={name}",
String.class,
"hi 中国"));

Map<String, String> params = new HashMap<>();
params.put("name", "world2");
Expand All @@ -101,6 +105,11 @@ private static void testController(RestTemplate template, String microserviceNam
null,
String.class,
"world"));
TestMgr.check("hello hello 中国",
template.postForObject(prefix + "/controller/sayhello/{name}",
null,
String.class,
"hello 中国"));

HttpHeaders headers = new HttpHeaders();
headers.add("name", "world");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@
import io.servicecomb.demo.server.User;
import java.io.IOException;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.*;

import org.junit.Test;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
Expand All @@ -65,6 +66,18 @@ public void ableToQueryAtRootBasePath() {

assertThat(responseEntity.getStatusCode(), is(OK));
assertThat(responseEntity.getBody(), is("Hi Mike"));

List<HttpMessageConverter<?>> convertersOld = restTemplate.getMessageConverters();
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2HttpMessageConverter());
restTemplate.setMessageConverters(converters);
responseEntity = restTemplate
.getForEntity(baseUrl + "sayHi?name={name}", String.class, "小 强");

assertThat(responseEntity.getStatusCode(), is(OK));
assertThat(responseEntity.getBody(), is("Hi 小 强"));

restTemplate.setMessageConverters(convertersOld);
}

@Test
Expand Down Expand Up @@ -276,6 +289,19 @@ public void postsEndWithPathParam() {
"world");

assertThat(jsonOf(result, String.class), is("hello world"));

List<HttpMessageConverter<?>> convertersOld = restTemplate.getMessageConverters();
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2HttpMessageConverter());
restTemplate.setMessageConverters(converters);
result = restTemplate.postForObject(
controllerUrl + "sayhello/{name}",
null,
String.class,
"中 国");

assertThat(result, is("hello 中 国"));
restTemplate.setMessageConverters(convertersOld);
}

@Test
Expand All @@ -290,6 +316,22 @@ public void ableToPostObjectAsJsonWithRequestVariable() {
"hello");

assertThat(jsonOf(result, String.class), is("hello world"));

List<HttpMessageConverter<?>> convertersOld = restTemplate.getMessageConverters();
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2HttpMessageConverter());
restTemplate.setMessageConverters(converters);
input = new Person();
input.setName("中国");

result = restTemplate.postForObject(
controllerUrl + "saysomething?prefix={prefix}",
jsonRequest(input),
String.class,
"hello");

assertThat(result, is("hello 中国"));
restTemplate.setMessageConverters(convertersOld);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private CseClientHttpResponse invoke(RequestMeta requestMeta, Object[] args) {
requestMeta.getOperationMeta(),
args);
invocation.getHandlerContext().put(RestConst.REST_CLIENT_REQUEST_PATH,
this.uri.getPath() + "?" + this.uri.getQuery());
this.uri.getRawPath() + "?" + this.uri.getRawQuery());

if (context != null) {
invocation.addContext(context);
Expand Down