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

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 84031a8cd44b CAMEL-27361: camel-core - an optional property 
placeholder {{?x}} in an endpoint path no longer makes the query start with & 
(#30580)
84031a8cd44b is described below

commit 84031a8cd44b47bec715da2ff0d523d2d34288de
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Nov 22 11:30:41 2026 +0100

    CAMEL-27361: camel-core - an optional property placeholder {{?x}} in an 
endpoint path no longer makes the query start with & (#30580)
    
    EndpointUriFactorySupport.buildQueryParameters and the catalog's
    asEndpointUri chose between ? and & with indexOf('?'), so the ? of an
    optional placeholder such as {{?directoryName}} in a path parameter was
    taken for the start of the query. The options were then appended with &,
    became part of the path and were lost.
    
    The new URISupport.indexOfQuestionMark skips a ? inside {{ }}. It is used
    by EndpointUriFactorySupport, AbstractCamelCatalog (both branches of
    doAsEndpointUri) and the endpoint DSL naming resolver of the Java parser,
    which had the same check. A real ? in the context path still makes the
    query start with &.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../camel/catalog/impl/AbstractCamelCatalog.java   |  6 ++--
 .../camel/catalog/RuntimeCamelCatalogTest.java     | 18 ++++++++++
 .../component/EndpointOptionEdgeCasesTest.java     | 38 ++++++++++++++++++++++
 .../camel/java/in/NamingEndpointDslResolver.java   |  4 ++-
 .../org/apache/camel/java/in/EndpointDslTest.java  | 12 +++++++
 .../component/EndpointUriFactorySupport.java       |  3 +-
 .../java/org/apache/camel/util/URISupport.java     | 27 +++++++++++++++
 .../java/org/apache/camel/util/URISupportTest.java | 13 ++++++++
 8 files changed, 117 insertions(+), 4 deletions(-)

diff --git 
a/core/camel-core-catalog/src/main/java/org/apache/camel/catalog/impl/AbstractCamelCatalog.java
 
b/core/camel-core-catalog/src/main/java/org/apache/camel/catalog/impl/AbstractCamelCatalog.java
index 9c6fe38e5c4c..9d38a0d174d6 100644
--- 
a/core/camel-core-catalog/src/main/java/org/apache/camel/catalog/impl/AbstractCamelCatalog.java
+++ 
b/core/camel-core-catalog/src/main/java/org/apache/camel/catalog/impl/AbstractCamelCatalog.java
@@ -1078,8 +1078,9 @@ public abstract class AbstractCamelCatalog {
                 // wrap secret values with RAW to avoid breaking URI encoding 
in case of encoded values
                 copy.replaceAll((key, val) -> wrapRAW(key, val, rows));
 
-                boolean hasQuestionMark = sb.toString().contains("?");
                 // the last option may already contain a ? char, if so we 
should use & instead of ?
+                // (but not the ? of an optional property placeholder 
{{?name}}) (CAMEL-27361)
+                boolean hasQuestionMark = 
URISupport.indexOfQuestionMark(sb.toString()) != -1;
                 sb.append(hasQuestionMark ? ampersand : '?');
                 String query = URISupport.createQueryString(copy, ampersand, 
encode);
                 sb.append(query);
@@ -1113,7 +1114,8 @@ public abstract class AbstractCamelCatalog {
                     first = false;
                 }
             }
-            boolean hasQuestionmark = sb.indexOf("?") != -1;
+            // a ? char in the path, but not the ? of an optional property 
placeholder {{?name}} (CAMEL-27361)
+            boolean hasQuestionmark = 
URISupport.indexOfQuestionMark(sb.toString()) != -1;
 
             if (!copy.isEmpty()) {
                 // wrap secret values with RAW to avoid breaking URI encoding 
in case of encoded values
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/catalog/RuntimeCamelCatalogTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/catalog/RuntimeCamelCatalogTest.java
index bffba9d194ca..822f738cab31 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/catalog/RuntimeCamelCatalogTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/catalog/RuntimeCamelCatalogTest.java
@@ -81,6 +81,24 @@ public class RuntimeCamelCatalogTest {
         assertEquals("timer:foo?period={{howoften}}&repeatCount=5", uri);
     }
 
+    @Test
+    public void testAsEndpointUriOptionalPlaceholderInPath() throws Exception {
+        Map<String, String> map = new HashMap<>();
+        map.put("directoryName", "src/data/{{?subDir}}");
+        map.put("noop", "true");
+
+        // the ? of an optional placeholder in the path is not the start of 
the query (CAMEL-27361)
+        String uri = catalog.asEndpointUri("file", map, false);
+        assertEquals("file:src/data/{{?subDir}}?noop=true", uri);
+
+        // some options of the syntax are missing
+        map = new HashMap<>();
+        map.put("languageName", "{{?lang}}");
+        map.put("contentCache", "true");
+        uri = catalog.asEndpointUri("language", map, false);
+        assertEquals("language:{{?lang}}?contentCache=true", uri);
+    }
+
     @Test
     public void testAsEndpointUriBeanLookup() throws Exception {
         Map<String, String> map = new HashMap<>();
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/support/component/EndpointOptionEdgeCasesTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/support/component/EndpointOptionEdgeCasesTest.java
index 66969a8eb207..d828ecab95c4 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/support/component/EndpointOptionEdgeCasesTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/support/component/EndpointOptionEdgeCasesTest.java
@@ -18,16 +18,20 @@ package org.apache.camel.support.component;
 
 import java.net.URISyntaxException;
 import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Set;
 
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.ResolveEndpointFailedException;
+import org.apache.camel.component.file.FileEndpoint;
 import org.apache.camel.component.timer.TimerEndpoint;
+import org.apache.camel.spi.EndpointUriFactory;
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 public class EndpointOptionEdgeCasesTest extends ContextTestSupport {
 
@@ -80,6 +84,40 @@ public class EndpointOptionEdgeCasesTest extends 
ContextTestSupport {
         assertEquals("my:C:\\dir/p$1", factory.buildUri("my", Map.of("type", 
"C:\\dir", "typeId", "p$1")));
     }
 
+    @Test
+    public void testOptionalPlaceholderInPathParameter() throws Exception {
+        EndpointUriFactory factory = 
context.getCamelContextExtension().getEndpointUriFactory("file");
+
+        // the ? of an optional placeholder in the path is not the start of 
the query (CAMEL-27361)
+        Map<String, Object> map = new LinkedHashMap<>();
+        map.put("directoryName", "target/data/{{?subDir}}");
+        map.put("noop", "true");
+        map.put("antInclude", "*.txt");
+        String uri = factory.buildUri("file", map, false);
+        
assertEquals("file:target/data/{{?subDir}}?noop=true&antInclude=*.txt", uri);
+
+        // and the options are not lost when the placeholder is resolved
+        FileEndpoint endpoint = context.getEndpoint(uri, FileEndpoint.class);
+        assertEquals("target/data", 
endpoint.getConfiguration().getDirectory());
+        assertTrue(endpoint.isNoop());
+        assertEquals("*.txt", endpoint.getAntInclude());
+    }
+
+    @Test
+    public void testQuestionMarkInPathParameter() throws Exception {
+        MyEndpointUriFactory factory = new MyEndpointUriFactory();
+        factory.setCamelContext(context);
+
+        // a ? in the path (not in a placeholder) still makes the query start 
with &
+        Map<String, Object> map = new LinkedHashMap<>();
+        map.put("type", "channel?x=1");
+        map.put("foo", "bar");
+        assertEquals("my:channel?x=1&foo=bar", factory.buildUri("my", map, 
false));
+
+        map.put("type", "{{?channel}}");
+        assertEquals("my:{{?channel}}?foo=bar", factory.buildUri("my", map, 
false));
+    }
+
     @Test
     public void testTimePatternOption() {
         TimerEndpoint endpoint = context.getEndpoint("timer:a?period=5S", 
TimerEndpoint.class);
diff --git 
a/core/camel-java-io/src/main/java/org/apache/camel/java/in/NamingEndpointDslResolver.java
 
b/core/camel-java-io/src/main/java/org/apache/camel/java/in/NamingEndpointDslResolver.java
index 22e685fe00c1..239ee56195bf 100644
--- 
a/core/camel-java-io/src/main/java/org/apache/camel/java/in/NamingEndpointDslResolver.java
+++ 
b/core/camel-java-io/src/main/java/org/apache/camel/java/in/NamingEndpointDslResolver.java
@@ -21,6 +21,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.regex.Pattern;
 
+import org.apache.camel.util.URISupport;
+
 /**
  * The endpoint DSL by the rules it is generated with: the factory is the 
component scheme in camel case
  * ({@code platformHttp} is {@code platform-http}), its argument is the path, 
and each builder method is the option of
@@ -74,7 +76,7 @@ final class NamingEndpointDslResolver implements 
EndpointDslResolver {
         }
         String uri = scheme + "://" + path;
         if (!query.isEmpty()) {
-            uri += (path.contains("?") ? "&" : "?") + query;
+            uri += (URISupport.indexOfQuestionMark(path) != -1 ? "&" : "?") + 
query;
         }
         return new Endpoint(uri, problems);
     }
diff --git 
a/core/camel-java-io/src/test/java/org/apache/camel/java/in/EndpointDslTest.java
 
b/core/camel-java-io/src/test/java/org/apache/camel/java/in/EndpointDslTest.java
index 4fd45f88a4cf..f128a4f2882b 100644
--- 
a/core/camel-java-io/src/test/java/org/apache/camel/java/in/EndpointDslTest.java
+++ 
b/core/camel-java-io/src/test/java/org/apache/camel/java/in/EndpointDslTest.java
@@ -66,6 +66,18 @@ class EndpointDslTest {
                 "myKafka://audit");
     }
 
+    @Test
+    void optionalPlaceholderInPath() {
+        // the ? of an optional placeholder in the path is not the start of 
the query (CAMEL-27361)
+        JavaParseResult result = new LwJavaParser().parse("""
+                import static 
org.apache.camel.builder.endpoint.StaticEndpointBuilders.*;
+                
from(file("inbox/{{?subDir}}").noop(true)).to(http("host/path?q=1").connectTimeout(500));
+                """);
+        assertThat(uris(result)).containsExactly(
+                "file://inbox/{{?subDir}}?noop=true",
+                "http://host/path?q=1&connectTimeout=500";);
+    }
+
     @Test
     void multiValueOptionsNeedTheCatalog() {
         JavaParseResult result = new LwJavaParser().parse("""
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/component/EndpointUriFactorySupport.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/component/EndpointUriFactorySupport.java
index a4e224c09376..d7ffad748046 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/component/EndpointUriFactorySupport.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/component/EndpointUriFactorySupport.java
@@ -154,7 +154,8 @@ public abstract class EndpointUriFactorySupport implements 
EndpointUriFactory {
         if (ObjectHelper.isNotEmpty(query)) {
             // there may be a ? sign in the context path then use & instead
             // (this is not correct but lets deal with this as the 
camel-catalog handled this)
-            boolean questionMark = uri.indexOf('?') != -1;
+            // but not the ? of an optional property placeholder {{?name}} in 
a path parameter (CAMEL-27361)
+            boolean questionMark = URISupport.indexOfQuestionMark(uri) != -1;
             if (questionMark) {
                 uri = uri + "&" + query;
             } else {
diff --git 
a/core/camel-util/src/main/java/org/apache/camel/util/URISupport.java 
b/core/camel-util/src/main/java/org/apache/camel/util/URISupport.java
index 42fdf3c0739d..c96c888e3353 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/URISupport.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/URISupport.java
@@ -285,6 +285,33 @@ public final class URISupport {
         return StringHelper.after(uri, "?");
     }
 
+    /**
+     * The index of the first ? sign in the uri that is not inside a property 
placeholder, such as the ? of an optional
+     * placeholder {{?name}}.
+     *
+     * @param  uri the uri, which may still have property placeholders
+     * @return     the index, or -1 if there is no such ? sign
+     */
+    public static int indexOfQuestionMark(String uri) {
+        if (uri == null) {
+            return -1;
+        }
+        int depth = 0;
+        for (int i = 0; i < uri.length(); i++) {
+            char ch = uri.charAt(i);
+            if (ch == '{' && uri.startsWith("{{", i)) {
+                depth++;
+                i++;
+            } else if (ch == '}' && depth > 0 && uri.startsWith("}}", i)) {
+                depth--;
+                i++;
+            } else if (ch == '?' && depth == 0) {
+                return i;
+            }
+        }
+        return -1;
+    }
+
     /**
      * Strips the query parameters from the uri
      *
diff --git 
a/core/camel-util/src/test/java/org/apache/camel/util/URISupportTest.java 
b/core/camel-util/src/test/java/org/apache/camel/util/URISupportTest.java
index 3efd2c663858..6f587c5f6369 100644
--- a/core/camel-util/src/test/java/org/apache/camel/util/URISupportTest.java
+++ b/core/camel-util/src/test/java/org/apache/camel/util/URISupportTest.java
@@ -1016,4 +1016,17 @@ public class URISupportTest {
         assertThat(URISupport.textBlockToSingleLine(uri)).isSameAs(uri);
     }
 
+    @Test
+    public void testIndexOfQuestionMark() {
+        assertThat(URISupport.indexOfQuestionMark(null)).isEqualTo(-1);
+        
assertThat(URISupport.indexOfQuestionMark("smb:host:445/share")).isEqualTo(-1);
+        
assertThat(URISupport.indexOfQuestionMark("timer:foo?period=5000")).isEqualTo(9);
+        // the ? of an optional property placeholder is not the start of the 
query (CAMEL-27361)
+        
assertThat(URISupport.indexOfQuestionMark("smb:host:445/share/{{?directoryName}}")).isEqualTo(-1);
+        
assertThat(URISupport.indexOfQuestionMark("smb:{{host}}/{{?dir}}?password=RAW(a?b)")).isEqualTo(21);
+        
assertThat(URISupport.indexOfQuestionMark("file:{{?a:{{?b}}}}/x?noop=true")).isEqualTo(20);
+        // a ? in the context path outside of a placeholder still counts
+        
assertThat(URISupport.indexOfQuestionMark("http:host/path?q={{?x}}")).isEqualTo(14);
+    }
+
 }

Reply via email to