[
https://issues.apache.org/jira/browse/SCB-880?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16602552#comment-16602552
]
ASF GitHub Bot commented on SCB-880:
------------------------------------
liubao68 closed pull request #886: [SCB-880]Give an option to query parameter
convert empty to null
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/886
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/codec/param/QueryProcessorCreator.java
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/QueryProcessorCreator.java
index 231b6c9ce..124e178ab 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 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;
@@ -33,6 +35,9 @@
public static final String PARAMTYPE = "query";
public static class QueryProcessor extends AbstractParamProcessor {
+ private boolean emptyAsNull = DynamicPropertyFactory.getInstance()
+ .getBooleanProperty("servicecomb.rest.parameter.query.emptyAsNull",
false).get();
+
public QueryProcessor(String paramPath, JavaType targetType, Object
defaultValue) {
super(paramPath, targetType, defaultValue);
}
@@ -44,6 +49,12 @@ public Object getValue(HttpServletRequest request) throws
Exception {
value = request.getParameterValues(paramPath);
} else {
value = request.getParameter(paramPath);
+ // make some old systems happy
+ if (emptyAsNull) {
+ 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 63e65e29c..295b6f94d 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 void testCreate() {
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 30e812fca..c7d3a1ba4 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 Response doInvoke(SwaggerInvocation invocation) {
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);
}
----------------------------------------------------------------
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:
[email protected]
> Give an option to query parameter convert empty to null
> -------------------------------------------------------
>
> Key: SCB-880
> URL: https://issues.apache.org/jira/browse/SCB-880
> Project: Apache ServiceComb
> Issue Type: New Feature
> Components: Java-Chassis
> Reporter: liubao
> Assignee: liubao
> Priority: Major
> Fix For: java-chassis-1.1.0
>
>
> Some old system parse query like [http://host/path/a=&b=22] and value of a is
> null. But Servicecomb is empty. We need give an option to make these system
> happy.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)