You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/09/27 01:13:14 UTC
[incubator-servicecomb-java-chassis] branch master updated:
[SCB-933]Revert changes to RestObjectMapper that fail on primitive types
not present
This is an automated email from the ASF dual-hosted git repository.
liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git
The following commit(s) were added to refs/heads/master by this push:
new 7588d90 [SCB-933]Revert changes to RestObjectMapper that fail on primitive types not present
7588d90 is described below
commit 7588d90894c10a9f94d4f589e543ee14a7d0c272
Author: liubao <ba...@huawei.com>
AuthorDate: Fri Sep 21 15:36:30 2018 +0800
[SCB-933]Revert changes to RestObjectMapper that fail on primitive types not present
---
.../common/rest/codec/RestObjectMapper.java | 4 +-
.../demo/springmvc/server/ControllerImpl.java | 3 +-
.../servicecomb/it/schema/RequestBaseModel.java | 49 ++++++++++
.../apache/servicecomb/it/schema/RequestModel.java | 49 ++++++++++
.../servicecomb/it/schema/ResponseBaseModel.java | 49 ++++++++++
.../servicecomb/it/schema/ResponseModel.java | 49 ++++++++++
.../org/apache/servicecomb/it/ConsumerMain.java | 3 +
.../testcase/TestDefaultJsonValueJaxrsSchema.java | 8 +-
.../testcase/TestRequestBodySpringMvcSchema.java | 103 +++++++++++++++++++++
.../it-edge/src/main/resources/microservice.yaml | 13 +--
.../it/schema/RequestBodySpringMvcSchema.java | 38 ++++++++
11 files changed, 355 insertions(+), 13 deletions(-)
diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/RestObjectMapper.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/RestObjectMapper.java
index 327175b..8f20acb 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/RestObjectMapper.java
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/RestObjectMapper.java
@@ -61,12 +61,12 @@ public class RestObjectMapper extends AbstractRestObjectMapper {
});
getFactory().disable(Feature.AUTO_CLOSE_SOURCE);
+ // Enable features that can tolerance errors and not enable those make more constraints for compatible reasons.
+ // Developers can use validation api to do more checks.
disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
- // If required=true, need to fail, while required=false, default values is given.
- enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
SimpleModule module = new SimpleModule();
// custom types
diff --git a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ControllerImpl.java b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ControllerImpl.java
index 7b22f9f..3c68b97 100644
--- a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ControllerImpl.java
+++ b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ControllerImpl.java
@@ -20,6 +20,7 @@ package org.apache.servicecomb.demo.springmvc.server;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
+import javax.validation.constraints.Min;
import javax.ws.rs.core.MediaType;
import org.apache.servicecomb.demo.controller.Person;
@@ -37,7 +38,7 @@ import org.springframework.web.bind.annotation.RequestParam;
@RequestMapping(path = "/springmvc/controller", produces = MediaType.APPLICATION_JSON)
public class ControllerImpl {
@GetMapping(path = "/add")
- public int add(@RequestParam("a") int a, @RequestParam("b") int b) {
+ public int add(@Min(1) @RequestParam("a") int a, @Min(1) @RequestParam("b") int b) {
return a + b;
}
diff --git a/integration-tests/it-common/src/main/java/org/apache/servicecomb/it/schema/RequestBaseModel.java b/integration-tests/it-common/src/main/java/org/apache/servicecomb/it/schema/RequestBaseModel.java
new file mode 100644
index 0000000..30e1631
--- /dev/null
+++ b/integration-tests/it-common/src/main/java/org/apache/servicecomb/it/schema/RequestBaseModel.java
@@ -0,0 +1,49 @@
+/*
+ * 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;
+
+public class RequestBaseModel {
+ private int type;
+
+ private Integer integerType;
+
+ private String message;
+
+ public int getType() {
+ return type;
+ }
+
+ public void setType(int type) {
+ this.type = type;
+ }
+
+ public Integer getIntegerType() {
+ return integerType;
+ }
+
+ public void setIntegerType(Integer integerType) {
+ this.integerType = integerType;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+ public void setMessage(String message) {
+ this.message = message;
+ }
+}
diff --git a/integration-tests/it-common/src/main/java/org/apache/servicecomb/it/schema/RequestModel.java b/integration-tests/it-common/src/main/java/org/apache/servicecomb/it/schema/RequestModel.java
new file mode 100644
index 0000000..945a87f
--- /dev/null
+++ b/integration-tests/it-common/src/main/java/org/apache/servicecomb/it/schema/RequestModel.java
@@ -0,0 +1,49 @@
+/*
+ * 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;
+
+public class RequestModel extends RequestBaseModel {
+ private int catalog;
+
+ private Integer integerCatalog;
+
+ private String extendedMessage;
+
+ public int getCatalog() {
+ return catalog;
+ }
+
+ public void setCatalog(int catalog) {
+ this.catalog = catalog;
+ }
+
+ public Integer getIntegerCatalog() {
+ return integerCatalog;
+ }
+
+ public void setIntegerCatalog(Integer integerCatalog) {
+ this.integerCatalog = integerCatalog;
+ }
+
+ public String getExtendedMessage() {
+ return extendedMessage;
+ }
+
+ public void setExtendedMessage(String extendedMessage) {
+ this.extendedMessage = extendedMessage;
+ }
+}
diff --git a/integration-tests/it-common/src/main/java/org/apache/servicecomb/it/schema/ResponseBaseModel.java b/integration-tests/it-common/src/main/java/org/apache/servicecomb/it/schema/ResponseBaseModel.java
new file mode 100644
index 0000000..e045ef7
--- /dev/null
+++ b/integration-tests/it-common/src/main/java/org/apache/servicecomb/it/schema/ResponseBaseModel.java
@@ -0,0 +1,49 @@
+/*
+ * 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;
+
+public class ResponseBaseModel {
+ private int type;
+
+ private Integer integerType;
+
+ private String message;
+
+ public int getType() {
+ return type;
+ }
+
+ public void setType(int type) {
+ this.type = type;
+ }
+
+ public Integer getIntegerType() {
+ return integerType;
+ }
+
+ public void setIntegerType(Integer integerType) {
+ this.integerType = integerType;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+ public void setMessage(String message) {
+ this.message = message;
+ }
+}
diff --git a/integration-tests/it-common/src/main/java/org/apache/servicecomb/it/schema/ResponseModel.java b/integration-tests/it-common/src/main/java/org/apache/servicecomb/it/schema/ResponseModel.java
new file mode 100644
index 0000000..e816aa6
--- /dev/null
+++ b/integration-tests/it-common/src/main/java/org/apache/servicecomb/it/schema/ResponseModel.java
@@ -0,0 +1,49 @@
+/*
+ * 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;
+
+public class ResponseModel extends ResponseBaseModel {
+ private int catalog;
+
+ private Integer integerCatalog;
+
+ private String extendedMessage;
+
+ public int getCatalog() {
+ return catalog;
+ }
+
+ public void setCatalog(int catalog) {
+ this.catalog = catalog;
+ }
+
+ public Integer getIntegerCatalog() {
+ return integerCatalog;
+ }
+
+ public void setIntegerCatalog(Integer integerCatalog) {
+ this.integerCatalog = integerCatalog;
+ }
+
+ public String getExtendedMessage() {
+ return extendedMessage;
+ }
+
+ public void setExtendedMessage(String extendedMessage) {
+ this.extendedMessage = extendedMessage;
+ }
+}
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 37e5df8..12a59f0 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
@@ -29,6 +29,7 @@ import org.apache.servicecomb.it.testcase.TestDefaultValue;
import org.apache.servicecomb.it.testcase.TestIgnoreMethod;
import org.apache.servicecomb.it.testcase.TestParamCodec;
import org.apache.servicecomb.it.testcase.TestParamCodecEdge;
+import org.apache.servicecomb.it.testcase.TestRequestBodySpringMvcSchema;
import org.apache.servicecomb.it.testcase.TestRestServerConfig;
import org.apache.servicecomb.it.testcase.TestRestServerConfigEdge;
import org.apache.servicecomb.it.testcase.TestTrace;
@@ -130,6 +131,8 @@ public class ConsumerMain {
ITJUnitUtils.runWithRest(TestRestServerConfig.class);
ITJUnitUtils.run(TestRestServerConfigEdge.class);
+ ITJUnitUtils.run(TestRequestBodySpringMvcSchema.class);
+
ITJUnitUtils.getParents().pop();
deploys.getBaseProducer().stop();
}
diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestDefaultJsonValueJaxrsSchema.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestDefaultJsonValueJaxrsSchema.java
index 629d9a3..6594c9d 100644
--- a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestDefaultJsonValueJaxrsSchema.java
+++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestDefaultJsonValueJaxrsSchema.java
@@ -51,7 +51,7 @@ public class TestDefaultJsonValueJaxrsSchema {
Assert.assertEquals(result, "expected:3:3");
result = client.getForObject("/queryInput", String.class);
- Assert.assertEquals(result, "expected:0:null");
+ Assert.assertEquals(result, "expected:0:0");
result = client.getForObject("/queryInput?size=", String.class);
Assert.assertEquals(result, "expected:0:null");
@@ -67,7 +67,7 @@ public class TestDefaultJsonValueJaxrsSchema {
Map result =
client.postForObject("/jsonInput", entity, Map.class);
Assert.assertEquals(result.get("type"), 100);
- Assert.assertEquals(result.get("message"), "expected:30:0");
+ Assert.assertEquals(result.get("message"), "expected:null:null");
body = new HashMap<>();
body.put("type", 100);
@@ -76,7 +76,7 @@ public class TestDefaultJsonValueJaxrsSchema {
result =
client.postForObject("/jsonInput", entity, Map.class);
Assert.assertEquals(result.get("type"), 100);
- Assert.assertEquals(result.get("message"), "expected:null:0");
+ Assert.assertEquals(result.get("message"), "expected:null:null");
body = new HashMap<>();
body.put("type", 200);
@@ -85,7 +85,7 @@ public class TestDefaultJsonValueJaxrsSchema {
result =
client.postForObject("/jsonInput", entity, Map.class);
Assert.assertEquals(result.get("type"), 200);
- Assert.assertEquals(result.get("message"), "expected:-1:0");
+ Assert.assertEquals(result.get("message"), "expected:-1:null");
body = new HashMap<>();
body.put("type", 200);
diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestRequestBodySpringMvcSchema.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestRequestBodySpringMvcSchema.java
new file mode 100644
index 0000000..f51322d
--- /dev/null
+++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestRequestBodySpringMvcSchema.java
@@ -0,0 +1,103 @@
+/*
+ * 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 java.util.HashMap;
+import java.util.Map;
+
+import org.apache.servicecomb.it.extend.engine.GateRestTemplate;
+import org.apache.servicecomb.it.junit.ITJUnitUtils;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.web.client.RestTemplate;
+
+public class TestRequestBodySpringMvcSchema {
+ private static RestTemplate edgeClient;
+
+ private static String producerName;
+
+ @Before
+ public void prepare() {
+ if (!ITJUnitUtils.getProducerName().equals(producerName)) {
+ producerName = ITJUnitUtils.getProducerName();
+ edgeClient = new GateRestTemplate("it-edge", producerName, "requestBodySpringMvcSchema");
+ }
+ }
+
+ @Test
+ public void basicRequestResponse() {
+ basicRequestResponseImpl();
+ basicRequestResponseImpl();
+ basicRequestResponseImpl();
+ }
+
+ @Test
+ public void testDefaultForPremitive() {
+ testDefaultForPremitiveImpl();
+ testDefaultForPremitiveImpl();
+ testDefaultForPremitiveImpl();
+ }
+
+ private void testDefaultForPremitiveImpl() {
+ HttpHeaders headers = new HttpHeaders();
+ headers.setContentType(MediaType.APPLICATION_JSON);
+ Map<String, Object> request = new HashMap<>();
+ request.put("integerType", 100);
+ request.put("message", "hi");
+ request.put("catalog", 100);
+ request.put("extendedMessage", "hi");
+
+ HttpEntity<Map<String, Object>> entity = new HttpEntity<>(request, headers);
+ Map result =
+ edgeClient.postForObject("/base", entity, Map.class);
+ Assert.assertEquals(result.size(), 6);
+ Assert.assertEquals(result.get("type"), 0);
+ Assert.assertEquals(result.get("integerType"), request.get("integerType"));
+ Assert.assertEquals(result.get("message"), request.get("message"));
+ Assert.assertEquals(result.get("catalog"), request.get("catalog"));
+ Assert.assertEquals(result.get("integerCatalog"), null);
+ Assert.assertEquals(result.get("extendedMessage"), request.get("extendedMessage"));
+ }
+
+ private void basicRequestResponseImpl() {
+ HttpHeaders headers = new HttpHeaders();
+ headers.setContentType(MediaType.APPLICATION_JSON);
+ Map<String, Object> request = new HashMap<>();
+ request.put("type", 100);
+ request.put("integerType", 100);
+ request.put("message", "hi");
+ request.put("catalog", 100);
+ request.put("integerCatalog", 100);
+ request.put("extendedMessage", "hi");
+
+ HttpEntity<Map<String, Object>> entity = new HttpEntity<>(request, headers);
+ Map result =
+ edgeClient.postForObject("/base", entity, Map.class);
+ Assert.assertEquals(result.size(), request.size());
+ Assert.assertEquals(result.get("type"), request.get("type"));
+ Assert.assertEquals(result.get("integerType"), request.get("integerType"));
+ Assert.assertEquals(result.get("message"), request.get("message"));
+ Assert.assertEquals(result.get("catalog"), request.get("catalog"));
+ Assert.assertEquals(result.get("integerCatalog"), request.get("integerCatalog"));
+ Assert.assertEquals(result.get("extendedMessage"), request.get("extendedMessage"));
+ }
+}
diff --git a/integration-tests/it-edge/src/main/resources/microservice.yaml b/integration-tests/it-edge/src/main/resources/microservice.yaml
index 88703c0..df5296a 100644
--- a/integration-tests/it-edge/src/main/resources/microservice.yaml
+++ b/integration-tests/it-edge/src/main/resources/microservice.yaml
@@ -25,12 +25,13 @@ servicecomb:
default: loadbalance
service:
it-auth: loadbalance
- rest:
- parameter:
- decodeAsObject: true
- query:
- ignoreDefaultValue: true
- emptyAsNull: true
+# this is not the majority, not testing this feature by default
+# rest:
+# parameter:
+# decodeAsObject: true
+# query:
+# ignoreDefaultValue: true
+# emptyAsNull: true
operation:
it-producer:
defaultJsonValueJaxrs:
diff --git a/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/RequestBodySpringMvcSchema.java b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/RequestBodySpringMvcSchema.java
new file mode 100644
index 0000000..6efdcbb
--- /dev/null
+++ b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/RequestBodySpringMvcSchema.java
@@ -0,0 +1,38 @@
+/*
+ * 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.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+@RestSchema(schemaId = "requestBodySpringMvcSchema")
+@RequestMapping(path = "/v1/requestBodySpringMvcSchema")
+public class RequestBodySpringMvcSchema {
+ @PostMapping(path = "base")
+ public ResponseModel base(@RequestBody RequestModel request) {
+ ResponseModel response = new ResponseModel();
+ response.setType(request.getType());
+ response.setIntegerType(request.getIntegerType());
+ response.setMessage(request.getMessage());
+ response.setCatalog(request.getCatalog());
+ response.setIntegerCatalog(request.getIntegerCatalog());
+ response.setExtendedMessage(request.getExtendedMessage());
+ return response;
+ }
+}