[camel] branch camel-2.x updated: CAMEL-14163: multiple request query parameters ...

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[camel] branch camel-2.x updated: CAMEL-14163: multiple request query parameters ...

zregvart
This is an automated email from the ASF dual-hosted git repository.

zregvart pushed a commit to branch camel-2.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-2.x by this push:
     new e5397a4  CAMEL-14163: multiple request query parameters ...
e5397a4 is described below

commit e5397a4b87f977a3d8c879718d530debde8881cf
Author: Zoran Regvart <[hidden email]>
AuthorDate: Fri Nov 8 22:22:28 2019 +0100

    CAMEL-14163: multiple request query parameters ...
   
    ...in Rest component
   
    Instead of forcing the parameter value to be converted to a String, the
    value is passed to URISupport that generates multiple request parameters
    for parameters whose value is instance of java.util.List.
   
    (cherry picked from commit bbc0b86e8ddfe84541df4ba0201b416323def5a6)
---
 .../apache/camel/component/rest/RestProducer.java  |  7 ++++-
 .../camel/component/rest/RestProducerTest.java     | 34 ++++++++++++++++++++--
 2 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/component/rest/RestProducer.java b/camel-core/src/main/java/org/apache/camel/component/rest/RestProducer.java
index 83b36cc..e6fddf4 100644
--- a/camel-core/src/main/java/org/apache/camel/component/rest/RestProducer.java
+++ b/camel-core/src/main/java/org/apache/camel/component/rest/RestProducer.java
@@ -404,7 +404,7 @@ public class RestProducer extends DefaultAsyncProducer {
                             key = key.substring(0, key.length() - 1);
                             optional = true;
                         }
-                        String value = inMessage.getHeader(key, String.class);
+                        Object value = inMessage.getHeader(key);
                         if (value != null) {
                             params.put(entry.getKey(), value);
                         } else if (!optional) {
@@ -417,7 +417,12 @@ public class RestProducer extends DefaultAsyncProducer {
                 }
             }
             query = URISupport.createQueryString(params);
+            // remove any dangling & caused by the absence of optional parameters
+            while (query.endsWith("&")) {
+              query = query.substring(0, query.length() - 1);
+            }
         }
+
         return query;
     }
 }
diff --git a/camel-core/src/test/java/org/apache/camel/component/rest/RestProducerTest.java b/camel-core/src/test/java/org/apache/camel/component/rest/RestProducerTest.java
index 50ce942..b2bdb08 100644
--- a/camel-core/src/test/java/org/apache/camel/component/rest/RestProducerTest.java
+++ b/camel-core/src/test/java/org/apache/camel/component/rest/RestProducerTest.java
@@ -18,6 +18,8 @@ package org.apache.camel.component.rest;
 
 import java.io.UnsupportedEncodingException;
 import java.net.URISyntaxException;
+import java.util.Arrays;
+import java.util.Collections;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.impl.DefaultCamelContext;
@@ -32,6 +34,22 @@ public class RestProducerTest {
     private CamelContext camelContext = new DefaultCamelContext();
 
     @Test
+    public void shouldCreateMultipleQueryParameters() throws UnsupportedEncodingException, URISyntaxException {
+        final DefaultMessage message = new DefaultMessage(camelContext);
+        message.setHeader("multiple", Arrays.asList("value1", "value2", "value3"));
+
+        assertEquals("param=value1&param=value2&param=value3", RestProducer.createQueryParameters("param={multiple}", message));
+    }
+
+    @Test
+    public void shouldCreateMultipleOptionalQueryParameters() throws UnsupportedEncodingException, URISyntaxException {
+        final DefaultMessage message = new DefaultMessage(camelContext);
+        message.setHeader("multiple", Collections.EMPTY_LIST);
+
+        assertEquals("", RestProducer.createQueryParameters("multiple={multiple?}", message));
+    }
+
+    @Test
     public void shouldCreateDefinedQueryParameters() throws UnsupportedEncodingException, URISyntaxException {
         assertEquals("param=value", RestProducer.createQueryParameters("param=value", null));
     }
@@ -71,9 +89,19 @@ public class RestProducerTest {
         final DefaultMessage message = new DefaultMessage(camelContext);
         message.setHeader("requiredParamPlaceholder", "header_required_value");
         message.setHeader("optionalPresentParamPlaceholder", "header_optional_present_value");
+        message.setHeader("multiple", Arrays.asList("value1", "value2", "value3"));
+        message.setHeader("multipleOptional", Collections.EMPTY_LIST);
 
-        assertEquals("given=value&required=header_required_value&optional_present=header_optional_present_value",
-            RestProducer.createQueryParameters(
-                "given=value&required={requiredParamPlaceholder}&optional={optionalParamPlaceholder?}&optional_present={optionalPresentParamPlaceholder?}", message));
+        assertEquals("given=value"
+            + "&required=header_required_value"
+            + "&optional_present=header_optional_present_value"
+            + "&multiple=value1&multiple=value2&multiple=value3",
+            RestProducer.createQueryParameters("given=value"
+                + "&required={requiredParamPlaceholder}"
+                + "&optional={optionalParamPlaceholder?}"
+                + "&optional_present={optionalPresentParamPlaceholder?}"
+                + "&multiple={multiple}"
+                + "&multipleOptional={multipleOptional?}",
+                message));
     }
 }