[camel] branch master 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 master 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 master
in repository https://gitbox.apache.org/repos/asf/camel.git


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

commit bbc0b86e8ddfe84541df4ba0201b416323def5a6
Author: Zoran Regvart <[hidden email]>
AuthorDate: Fri Nov 8 21:14:02 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.
---
 .../apache/camel/component/rest/RestProducer.java  |  7 ++++-
 .../camel/component/rest/RestProducerTest.java     | 34 ++++++++++++++++++++--
 2 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestProducer.java b/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestProducer.java
index ad4f989..4a98e71 100644
--- a/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestProducer.java
+++ b/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestProducer.java
@@ -428,7 +428,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) {
@@ -441,7 +441,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/core/camel-core/src/test/java/org/apache/camel/component/rest/RestProducerTest.java b/core/camel-core/src/test/java/org/apache/camel/component/rest/RestProducerTest.java
index eac21b4..3409a7e 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/rest/RestProducerTest.java
+++ b/core/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));
     }
@@ -69,9 +87,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));
     }
 }