Skip to content

Commit 27552d2

Browse files
[#4638]fixbug content type is not case insensitive (#4641)
1 parent 2bf2764 commit 27552d2

File tree

8 files changed

+229
-5
lines changed

8 files changed

+229
-5
lines changed

common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/BodyProcessorCreator.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,10 @@ private Object getValueImpl(HttpServletRequest request) throws IOException {
169169

170170
// edge support convert from form-data or x-www-form-urlencoded to json automatically
171171
String contentType = validContentType(request.getContentType());
172-
if (contentType.equals(MediaType.MULTIPART_FORM_DATA)
173-
|| contentType.equals(MediaType.APPLICATION_FORM_URLENCODED)) {
172+
173+
// support RFC 7231, ignore case for content-type
174+
if (StringUtils.equalsIgnoreCase(contentType, MediaType.MULTIPART_FORM_DATA)
175+
|| StringUtils.equalsIgnoreCase(contentType, MediaType.APPLICATION_FORM_URLENCODED)) {
174176
return convertValue(request.getParameterMap(), targetType);
175177
}
176178

@@ -182,7 +184,7 @@ private Object getValueImpl(HttpServletRequest request) throws IOException {
182184
return null;
183185
}
184186

185-
if (MediaType.APPLICATION_JSON.equals(contentType)) {
187+
if (StringUtils.equalsIgnoreCase(contentType, MediaType.APPLICATION_JSON)) {
186188
try {
187189
ObjectReader reader = serialViewClass != null
188190
? RestObjectMapperFactory.getRestObjectMapper().readerWithView(serialViewClass)
@@ -202,7 +204,7 @@ private Object getValueImpl(HttpServletRequest request) throws IOException {
202204
}
203205
}
204206

205-
if (SwaggerConst.PROTOBUF_TYPE.equals(contentType)) {
207+
if (StringUtils.equalsIgnoreCase(contentType, SwaggerConst.PROTOBUF_TYPE)) {
206208
ProtoMapper protoMapper = scopedProtobufSchemaManager
207209
.getOrCreateProtoMapper(openAPI, operationMeta.getSchemaId(),
208210
REQUEST_BODY_NAME,
@@ -214,7 +216,7 @@ private Object getValueImpl(HttpServletRequest request) throws IOException {
214216
return result.getValue();
215217
}
216218

217-
if (MediaType.TEXT_PLAIN.equals(contentType)) {
219+
if (StringUtils.equalsIgnoreCase(contentType, MediaType.TEXT_PLAIN)) {
218220
try {
219221
if (targetType != null && String.class.equals(targetType.getRawClass())) {
220222
return IOUtils.toString(inputStream, StandardCharsets.UTF_8);

demo/demo-etcd/consumer/src/main/java/org/apache/servicecomb/samples/ConsumerController.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@
1919

2020
import org.apache.servicecomb.provider.pojo.RpcReference;
2121
import org.apache.servicecomb.provider.rest.common.RestSchema;
22+
import org.springframework.http.MediaType;
2223
import org.springframework.web.bind.annotation.GetMapping;
24+
import org.springframework.web.bind.annotation.PostMapping;
25+
import org.springframework.web.bind.annotation.RequestBody;
2326
import org.springframework.web.bind.annotation.RequestMapping;
2427
import org.springframework.web.bind.annotation.RequestParam;
28+
import org.springframework.web.bind.annotation.ResponseBody;
2529

2630
@RestSchema(schemaId = "ConsumerController")
2731
@RequestMapping(path = "/")
@@ -39,4 +43,10 @@ public String sayHello(@RequestParam("name") String name) {
3943
public String getConfig(@RequestParam("key") String key) {
4044
return providerService.getConfig(key);
4145
}
46+
47+
@PostMapping(path = "/testContentType", consumes = MediaType.APPLICATION_JSON_VALUE)
48+
@ResponseBody
49+
public User testContentType(@RequestBody User user) {
50+
return providerService.testContentType(user);
51+
}
4252
}

demo/demo-etcd/consumer/src/main/java/org/apache/servicecomb/samples/ProviderService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ public interface ProviderService {
2121
String sayHello(String name);
2222

2323
String getConfig(String key);
24+
25+
User testContentType(User user);
2426
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.servicecomb.samples;
18+
19+
20+
public class User {
21+
22+
private Long id;
23+
24+
private String name;
25+
26+
public Long getId() {
27+
return id;
28+
}
29+
30+
public void setId(Long id) {
31+
this.id = id;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
public void setName(String name) {
39+
this.name = name;
40+
}
41+
}

demo/demo-etcd/provider/src/main/java/org/apache/servicecomb/samples/ProviderController.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@
2121
import org.springframework.beans.factory.InitializingBean;
2222
import org.springframework.beans.factory.annotation.Autowired;
2323
import org.springframework.core.env.Environment;
24+
import org.springframework.http.MediaType;
2425
import org.springframework.web.bind.annotation.GetMapping;
26+
import org.springframework.web.bind.annotation.PostMapping;
27+
import org.springframework.web.bind.annotation.RequestBody;
2528
import org.springframework.web.bind.annotation.RequestMapping;
2629
import org.springframework.web.bind.annotation.RequestParam;
30+
import org.springframework.web.bind.annotation.ResponseBody;
2731

2832
@RestSchema(schemaId = "ProviderController")
2933
@RequestMapping(path = "/")
@@ -47,6 +51,12 @@ public String getConfig(@RequestParam("key") String key) {
4751
return environment.getProperty(key);
4852
}
4953

54+
@PostMapping(path = "/testContentType", consumes = MediaType.APPLICATION_JSON_VALUE)
55+
@ResponseBody
56+
public User testContentType(@RequestBody User user) {
57+
return user;
58+
}
59+
5060
@Override
5161
public void afterPropertiesSet() throws Exception {
5262
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.servicecomb.samples;
18+
19+
public class User {
20+
21+
private Long id;
22+
23+
private String name;
24+
25+
public Long getId() {
26+
return id;
27+
}
28+
29+
public void setId(Long id) {
30+
this.id = id;
31+
}
32+
33+
public String getName() {
34+
return name;
35+
}
36+
37+
public void setName(String name) {
38+
this.name = name;
39+
}
40+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.servicecomb.samples;
18+
19+
import java.io.IOException;
20+
import java.nio.charset.StandardCharsets;
21+
22+
import org.apache.servicecomb.demo.CategorizedTestCase;
23+
import org.apache.servicecomb.demo.TestMgr;
24+
import org.apache.servicecomb.foundation.common.utils.JsonUtils;
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
import org.springframework.http.HttpEntity;
28+
import org.springframework.http.HttpHeaders;
29+
import org.springframework.http.HttpMethod;
30+
import org.springframework.http.ResponseEntity;
31+
import org.springframework.stereotype.Component;
32+
import org.springframework.web.client.RestOperations;
33+
import org.springframework.web.client.RestTemplate;
34+
35+
@Component
36+
public class ProviderIT implements CategorizedTestCase {
37+
38+
RestOperations template = new RestTemplate();
39+
40+
private static final Logger LOGGER = LoggerFactory.getLogger(ProviderIT.class);
41+
42+
@Override
43+
public void testRestTransport() throws Exception {
44+
45+
User user = getUser("Application/json");
46+
TestMgr.check(1L, user.getId());
47+
TestMgr.check("czd", user.getName());
48+
49+
User user2 = getUser("application/json");
50+
TestMgr.check(1L, user2.getId());
51+
TestMgr.check("czd", user2.getName());
52+
53+
User user3 = getUser("APPLICATION/JSON");
54+
TestMgr.check(1L, user3.getId());
55+
TestMgr.check("czd", user3.getName());
56+
}
57+
58+
private User getUser(String contentType) throws IOException {
59+
HttpHeaders headers = new HttpHeaders();
60+
headers.set("Content-Type", contentType);
61+
62+
String requestBody = """
63+
{
64+
"id": 1,
65+
"name": "czd"
66+
}
67+
""";
68+
69+
HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);
70+
71+
String url = Config.GATEWAY_URL + "/testContentType";
72+
ResponseEntity<String> response = template.exchange(
73+
url, HttpMethod.POST, entity, String.class);
74+
75+
User user = JsonUtils.readValue(response.getBody().getBytes(StandardCharsets.UTF_8), User.class);
76+
return user;
77+
}
78+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.servicecomb.samples;
18+
19+
20+
public class User {
21+
22+
private Long id;
23+
24+
private String name;
25+
26+
public Long getId() {
27+
return id;
28+
}
29+
30+
public void setId(Long id) {
31+
this.id = id;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
public void setName(String name) {
39+
this.name = name;
40+
}
41+
}

0 commit comments

Comments
 (0)