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

commit 6ac7b978337f821609b05dbe5a6957df79b6aeab
Author: liubao <[email protected]>
AuthorDate: Thu Aug 30 16:00:00 2018 +0800

    [SCB-880]Give an option to query parameter convert empty to null
---
 .../rest/codec/param/QueryProcessorCreator.java    |  9 +++++
 .../codec/param/TestQueryProcessorCreator.java     | 42 ++++++++++++++++++++++
 .../swagger/engine/SwaggerProducerOperation.java   |  3 +-
 3 files changed, 53 insertions(+), 1 deletion(-)

diff --git 
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/QueryProcessorCreator.java
 
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/QueryProcessorCreator.java
index 231b6c9..7cd31d3 100644
--- 
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/QueryProcessorCreator.java
+++ 
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/QueryProcessorCreator.java
@@ -21,10 +21,12 @@ import java.lang.reflect.Type;
 
 import javax.servlet.http.HttpServletRequest;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.servicecomb.common.rest.codec.RestClientRequest;
 
 import com.fasterxml.jackson.databind.JavaType;
 import com.fasterxml.jackson.databind.type.TypeFactory;
+import com.netflix.config.DynamicPropertyFactory;
 
 import io.swagger.models.parameters.Parameter;
 import io.swagger.models.parameters.QueryParameter;
@@ -44,6 +46,13 @@ public class QueryProcessorCreator implements 
ParamValueProcessorCreator {
         value = request.getParameterValues(paramPath);
       } else {
         value = request.getParameter(paramPath);
+        // make some old systems happy
+        if (DynamicPropertyFactory.getInstance()
+            
.getBooleanProperty("servicecomb.rest.parameter.query.emptyAsNull", 
false).get()) {
+          if (StringUtils.isEmpty((String) value)) {
+            value = null;
+          }
+        }
         if (value == null) {
           Object defaultValue = getDefaultValue();
           if (defaultValue != null) {
diff --git 
a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestQueryProcessorCreator.java
 
b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestQueryProcessorCreator.java
index 63e65e2..295b6f9 100644
--- 
a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestQueryProcessorCreator.java
+++ 
b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestQueryProcessorCreator.java
@@ -17,9 +17,15 @@
 
 package org.apache.servicecomb.common.rest.codec.param;
 
+import javax.servlet.http.HttpServletRequest;
+
 import 
org.apache.servicecomb.common.rest.codec.param.QueryProcessorCreator.QueryProcessor;
+import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils;
 import org.junit.Assert;
 import org.junit.Test;
+import org.mockito.Mockito;
+
+import com.fasterxml.jackson.databind.type.TypeFactory;
 
 import io.swagger.models.parameters.Parameter;
 import io.swagger.models.parameters.QueryParameter;
@@ -35,5 +41,41 @@ public class TestQueryProcessorCreator {
     ParamValueProcessor processor = creator.create(parameter, String.class);
 
     Assert.assertEquals(QueryProcessor.class, processor.getClass());
+
+    String result = (String) processor.convertValue("Hello", 
TypeFactory.defaultInstance().constructType(String.class));
+    Assert.assertEquals("Hello", result);
+
+    result = (String) processor.convertValue("", 
TypeFactory.defaultInstance().constructType(String.class));
+    Assert.assertEquals("", result);
+
+    result = (String) processor.convertValue(null, 
TypeFactory.defaultInstance().constructType(String.class));
+    Assert.assertEquals(null, result);
+  }
+
+  @Test
+  public void testCreateNullAsEmpty() throws Exception {
+    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
+    ArchaiusUtils.setProperty("servicecomb.rest.parameter.query.emptyAsNull", 
"true");
+    ParamValueProcessorCreator creator =
+        
ParamValueProcessorCreatorManager.INSTANCE.findValue(QueryProcessorCreator.PARAMTYPE);
+    Parameter parameter = new QueryParameter();
+    parameter.setName("query");
+
+    ParamValueProcessor processor = creator.create(parameter, String.class);
+
+    Assert.assertEquals(QueryProcessor.class, processor.getClass());
+
+    Mockito.when(request.getParameter("query")).thenReturn("Hello");
+    String result = (String) processor.getValue(request);
+    Assert.assertEquals("Hello", result);
+
+    Mockito.when(request.getParameter("query")).thenReturn("");
+    result = (String) (String) processor.getValue(request);
+    Assert.assertEquals(null, result);
+
+    Mockito.when(request.getParameter("query")).thenReturn(null);
+    result = (String) processor.convertValue(null, 
TypeFactory.defaultInstance().constructType(String.class));
+    result = (String) (String) processor.getValue(request);
+    Assert.assertEquals(null, result);
   }
 }
diff --git 
a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/engine/SwaggerProducerOperation.java
 
b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/engine/SwaggerProducerOperation.java
index 30e812f..c7d3a1b 100644
--- 
a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/engine/SwaggerProducerOperation.java
+++ 
b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/engine/SwaggerProducerOperation.java
@@ -164,9 +164,10 @@ public class SwaggerProducerOperation {
       Object result = producerMethod.invoke(producerInstance, args);
       response = responseMapper.mapResponse(invocation.getStatus(), result);
     } catch (IllegalArgumentException ae) {
+      // ae.getMessage() is always null. Give a custom error message.
       response = processException(invocation,
           new InvocationException(Status.BAD_REQUEST.getStatusCode(), "",
-              new CommonExceptionData(ae.getMessage()), ae));
+              new CommonExceptionData("Parameters not valid or types not 
match."), ae));
     } catch (Throwable e) {
       response = processException(invocation, e);
     }

Reply via email to