Skip to content

Commit

Permalink
[SCB-933]Revert changes to RestObjectMapper that fail on primitive ty…
Browse files Browse the repository at this point in the history
…pes not present
  • Loading branch information
liubao68 committed Sep 27, 2018
1 parent 03e89e5 commit 7588d90
Show file tree
Hide file tree
Showing 11 changed files with 355 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fie
});

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand All @@ -37,7 +38,7 @@
@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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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;
Expand Down Expand Up @@ -130,6 +131,8 @@ private static void testStandalone() throws Throwable {
ITJUnitUtils.runWithRest(TestRestServerConfig.class);
ITJUnitUtils.run(TestRestServerConfigEdge.class);

ITJUnitUtils.run(TestRequestBodySpringMvcSchema.class);

ITJUnitUtils.getParents().pop();
deploys.getBaseProducer().stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void invokeFromEdgeWithQuery() {
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");
Expand All @@ -67,7 +67,7 @@ public void invokeFromEdgeWithRawJson() {
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);
Expand All @@ -76,7 +76,7 @@ public void invokeFromEdgeWithRawJson() {
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);
Expand All @@ -85,7 +85,7 @@ public void invokeFromEdgeWithRawJson() {
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}
}

0 comments on commit 7588d90

Please sign in to comment.