[ 
https://issues.apache.org/jira/browse/SCB-726?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16544123#comment-16544123
 ] 

ASF GitHub Bot commented on SCB-726:
------------------------------------

wujimin closed pull request #800: [SCB-726] edge support convert from form-data 
or x-www-form-urlencoded to json automatically
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/800
 
 
   

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/RestObjectMapper.java
 
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/RestObjectMapper.java
index 6dc1bbb01..f75b7e98f 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
@@ -52,6 +52,7 @@ public StringBuffer format(Date date, StringBuffer 
toAppendTo, FieldPosition fie
     getFactory().disable(Feature.AUTO_CLOSE_SOURCE);
     disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
     disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
+    enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
   }
 
   public String convertToString(Object value) throws Exception {
diff --git 
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/BodyProcessorCreator.java
 
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/BodyProcessorCreator.java
index 61aaabb08..e550eea6d 100644
--- 
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/BodyProcessorCreator.java
+++ 
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/BodyProcessorCreator.java
@@ -59,6 +59,14 @@ public Object getValue(HttpServletRequest request) throws 
Exception {
         return convertValue(body, targetType);
       }
 
+      // edge support convert from form-data or x-www-form-urlencoded to json 
automatically
+      String contentType = request.getContentType();
+      contentType = contentType == null ? "" : 
contentType.toLowerCase(Locale.US);
+      if (contentType.startsWith(MediaType.MULTIPART_FORM_DATA)
+          || contentType.startsWith(MediaType.APPLICATION_FORM_URLENCODED)) {
+        return 
RestObjectMapper.INSTANCE.convertValue(request.getParameterMap(), targetType);
+      }
+
       // for standard HttpServletRequest, getInputStream will never return null
       // but for mocked HttpServletRequest, maybe get a null
       //  like 
org.apache.servicecomb.provider.springmvc.reference.ClientToHttpServletRequest
@@ -70,8 +78,8 @@ public Object getValue(HttpServletRequest request) throws 
Exception {
       if (isRequired == false && inputStream.available() == 0) {
         return null;
       }
-      String contentType = request.getContentType();
-      if (contentType != null && 
!contentType.toLowerCase(Locale.US).startsWith(MediaType.APPLICATION_JSON)) {
+
+      if (!contentType.isEmpty() && 
!contentType.startsWith(MediaType.APPLICATION_JSON)) {
         // TODO: we should consider body encoding
         return IOUtils.toString(inputStream, "UTF-8");
       }
@@ -86,7 +94,6 @@ public void setValue(RestClientRequest clientRequest, Object 
arg) throws Excepti
         if (arg != null) {
           clientRequest.write(output.getBuffer());
         }
-
       }
     }
 
diff --git 
a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestBodyProcessor.java
 
b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestBodyProcessor.java
index 4950b41d8..0b9fb1f46 100644
--- 
a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestBodyProcessor.java
+++ 
b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestBodyProcessor.java
@@ -232,4 +232,50 @@ public void testSetValueRawJson() throws Exception {
     Assert.assertEquals(MediaType.APPLICATION_JSON, 
headers.get(HttpHeaders.CONTENT_TYPE));
     Assert.assertEquals("value", outputBodyBuffer.toString());
   }
+
+  static class BodyModel {
+    public String name;
+
+    public int age;
+  }
+
+  @Test
+  public void convertFromFormData() throws Exception {
+    createProcessor(BodyModel.class);
+    Map<String, String[]> parameterMap = new HashMap<>();
+    parameterMap.put("name", new String[] {"n"});
+    parameterMap.put("age", new String[] {"10"});
+    new Expectations() {
+      {
+        request.getParameterMap();
+        result = parameterMap;
+        request.getContentType();
+        result = MediaType.MULTIPART_FORM_DATA + ";utf-8";
+      }
+    };
+
+    BodyModel bm = (BodyModel) processor.getValue(request);
+    Assert.assertEquals("n", bm.name);
+    Assert.assertEquals(10, bm.age);
+  }
+
+  @Test
+  public void convertFromUrlencoded() throws Exception {
+    createProcessor(BodyModel.class);
+    Map<String, String[]> parameterMap = new HashMap<>();
+    parameterMap.put("name", new String[] {"n"});
+    parameterMap.put("age", new String[] {"10"});
+    new Expectations() {
+      {
+        request.getParameterMap();
+        result = parameterMap;
+        request.getContentType();
+        result = MediaType.APPLICATION_FORM_URLENCODED + ";utf-8";
+      }
+    };
+
+    BodyModel bm = (BodyModel) processor.getValue(request);
+    Assert.assertEquals("n", bm.name);
+    Assert.assertEquals(10, bm.age);
+  }
 }


 

----------------------------------------------------------------
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]


> edge support convert from form-data or x-www-form-urlencoded to json 
> automatically
> ----------------------------------------------------------------------------------
>
>                 Key: SCB-726
>                 URL: https://issues.apache.org/jira/browse/SCB-726
>             Project: Apache ServiceComb
>          Issue Type: Task
>          Components: Java-Chassis
>            Reporter: wujimin
>            Assignee: wujimin
>            Priority: Major
>




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to