[ https://issues.apache.org/jira/browse/SCB-1051?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16709659#comment-16709659 ]
ASF GitHub Bot commented on SCB-1051: ------------------------------------- wujimin closed pull request #1015: [SCB-1051]when interface set produces=text/plain;charset=utf-8. and c… URL: https://github.com/apache/servicecomb-java-chassis/pull/1015 This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/RestOperationMeta.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/RestOperationMeta.java index ee7c40137..36979ea92 100644 --- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/RestOperationMeta.java +++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/RestOperationMeta.java @@ -173,6 +173,9 @@ protected void createProduceProcessors() { } } else { for (String produce : produces) { + if (produce.contains(";")) { + produce = produce.substring(0, produce.indexOf(";")); + } ProduceProcessor processor = ProduceProcessorManager.INSTANCE.findValue(produce); if (processor == null) { LOGGER.error("produce {} is not supported", produce); diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/definition/TestRestOperationMeta.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/definition/TestRestOperationMeta.java index 34621d6e2..6103df9ed 100644 --- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/definition/TestRestOperationMeta.java +++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/definition/TestRestOperationMeta.java @@ -147,6 +147,18 @@ public void testCreateProduceProcessorsTextAndWildcard() { MediaType.APPLICATION_JSON + "," + MediaType.APPLICATION_XML + "," + MediaType.WILDCARD)); } + @Test + public void testCreateProduceProcessorsWithSemicolon() { + RestOperationMeta operationMeta = new RestOperationMeta(); + operationMeta.produces = Arrays + .asList(MediaType.TEXT_PLAIN + ";charset=UTF-8", MediaType.APPLICATION_JSON + ";charset=UTF-8"); + operationMeta.createProduceProcessors(); + Assert.assertSame(ProduceProcessorManager.PLAIN_PROCESSOR, + operationMeta.ensureFindProduceProcessor(MediaType.TEXT_PLAIN)); + Assert.assertSame(ProduceProcessorManager.JSON_PROCESSOR, + operationMeta.ensureFindProduceProcessor(MediaType.APPLICATION_JSON)); + } + @Test public void testEnsureFindProduceProcessorRequest(@Mocked HttpServletRequestEx requestEx) { RestOperationMeta operationMeta = new RestOperationMeta(); diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java index 0ef503900..53edb8b9b 100644 --- a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java +++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java @@ -22,6 +22,7 @@ import org.apache.servicecomb.it.deploy.MicroserviceDeploy; import org.apache.servicecomb.it.junit.ITJUnitUtils; import org.apache.servicecomb.it.schema.TestApiOperation; +import org.apache.servicecomb.it.testcase.TestAcceptType; import org.apache.servicecomb.it.testcase.TestAnnotatedAttribute; import org.apache.servicecomb.it.testcase.TestApiParam; import org.apache.servicecomb.it.testcase.TestChangeTransport; @@ -102,6 +103,7 @@ private static void runShareTestCases() throws Throwable { // only rest support default value feature ITJUnitUtils.runWithRest(TestDefaultValue.class); + ITJUnitUtils.runWithRest(TestAcceptType.class); ITJUnitUtils.runWithRest(TestDownload.class); diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestAcceptType.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestAcceptType.java new file mode 100644 index 000000000..9b2f2b18e --- /dev/null +++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestAcceptType.java @@ -0,0 +1,102 @@ +/* + * 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.servicecomb.it.testcase; + +import org.apache.servicecomb.it.Consumers; +import org.apache.servicecomb.swagger.invocation.exception.InvocationException; +import org.junit.Assert; +import org.junit.Test; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; + +public class TestAcceptType { + interface AcceptTypeIntf { + } + + private static Consumers<AcceptTypeIntf> consumersAcceptTypeSpringmvc = new Consumers<>("acceptTypeSpringmvcSchema", + AcceptTypeIntf.class); + + private static Consumers<AcceptTypeIntf> consumersAcceptTypeJaxrs = new Consumers<>("acceptTypeJaxrsSchema", + AcceptTypeIntf.class); + + @Test + public void testTextPlain_rt() { + checkTextPlain(consumersAcceptTypeSpringmvc); + checkTextPlain(consumersAcceptTypeJaxrs); + } + + private void checkTextPlain(Consumers<AcceptTypeIntf> consumers) { + String result = textHeader_rt(consumers, MediaType.TEXT_PLAIN_VALUE); + Assert.assertEquals("cse", result); + try { + textHeader_rt(consumers, MediaType.APPLICATION_JSON_VALUE); + Assert.fail("should throw exception"); + } catch (InvocationException e) { + Assert.assertEquals(406, e.getStatusCode()); + Assert.assertTrue(e.getMessage().contains("Accept application/json is not supported")); + } catch (Throwable e) { + Assert.fail(" should throw InvocationException"); + } + } + + private String textHeader_rt(Consumers<AcceptTypeIntf> consumers, String type) { + HttpHeaders headers = new HttpHeaders(); + headers.add("accept", type); + HttpEntity<?> entity = new HttpEntity<>(headers); + ResponseEntity<String> response = consumers.getSCBRestTemplate() + .exchange("/sayHi?name=cse", + HttpMethod.GET, + entity, + String.class); + return response.getBody(); + } + + @Test + public void testProducerApplicationJson_rt() { + checkApplicationJson(consumersAcceptTypeSpringmvc); + checkApplicationJson(consumersAcceptTypeJaxrs); + } + + private void checkApplicationJson(Consumers<AcceptTypeIntf> consumers) { + String result = jsonHeader_rt(consumers, MediaType.APPLICATION_JSON_VALUE); + Assert.assertEquals("cse", result); + try { + jsonHeader_rt(consumersAcceptTypeSpringmvc, MediaType.TEXT_PLAIN_VALUE); + Assert.fail("should throw exception"); + } catch (InvocationException e) { + Assert.assertEquals(406, e.getStatusCode()); + Assert.assertTrue(e.getMessage().contains("Accept text/plain is not supported")); + } catch (Throwable e) { + Assert.fail("should throw InvocationException"); + } + } + + private String jsonHeader_rt(Consumers<AcceptTypeIntf> consumers, String type) { + HttpHeaders headers = new HttpHeaders(); + headers.add("accept", type); + HttpEntity<?> entity = new HttpEntity<>(headers); + ResponseEntity<String> response = consumers.getSCBRestTemplate() + .exchange("/sayHello?name=cse", + HttpMethod.GET, + entity, + String.class); + return response.getBody(); + } +} diff --git a/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/AcceptTypeJaxrsSchema.java b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/AcceptTypeJaxrsSchema.java new file mode 100644 index 000000000..37804d033 --- /dev/null +++ b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/AcceptTypeJaxrsSchema.java @@ -0,0 +1,42 @@ +/* + * 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.servicecomb.it.schema; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; + +import org.apache.servicecomb.provider.rest.common.RestSchema; + +@RestSchema(schemaId = "acceptTypeJaxrsSchema") +@Path("/v1/acceptTypeJaxrsSchema") +public class AcceptTypeJaxrsSchema { + @Path("/sayHi") + @GET + @Produces("text/plain;charset=UTF-8") + public String sayHi(@QueryParam("name") String name) { + return name; + } + + @Path("/sayHello") + @GET + @Produces("application/json;charset=UTF-8") + public String sayHello(@QueryParam("name") String name) { + return name; + } +} diff --git a/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/AcceptTypeSpringmvcSchema.java b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/AcceptTypeSpringmvcSchema.java new file mode 100644 index 000000000..f9d72dd3e --- /dev/null +++ b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/AcceptTypeSpringmvcSchema.java @@ -0,0 +1,36 @@ +/* + * 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.servicecomb.it.schema; + +import org.apache.servicecomb.provider.rest.common.RestSchema; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; + +@RestSchema(schemaId = "acceptTypeSpringmvcSchema") +@RequestMapping(path = "/v1/acceptTypeSpringmvcSchema") +public class AcceptTypeSpringmvcSchema { + @RequestMapping(path = "/sayHi", method = RequestMethod.GET, produces = "text/plain;charset=UTF-8") + public String sayHi(@RequestParam(name = "name", defaultValue = "test") String name) { + return name; + } + + @RequestMapping(path = "/sayHello", method = RequestMethod.GET, produces = "application/json;charset=UTF-8") + public String sayHello(@RequestParam(name = "name", defaultValue = "test") String name) { + return name; + } +} ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org > when interface set produces=text/plain;charset=utf-8. and consumers set > accept = text/plain,will cause error > ------------------------------------------------------------------------------------------------------------ > > Key: SCB-1051 > URL: https://issues.apache.org/jira/browse/SCB-1051 > Project: Apache ServiceComb > Issue Type: Improvement > Components: Java-Chassis > Reporter: 何一乐 > Assignee: 何一乐 > Priority: Major > Attachments: screenshot-1.png > > -- This message was sent by Atlassian JIRA (v7.6.3#76005)