Uploaded image for project: 'Camel'
  1. Camel
  2. CAMEL-20763

Rest template with underscore fails after Camel 4.2.0

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • 4.2.0
    • 4.4.3, 4.7.0
    • camel-core
    • None
    • Camel 4.2.0 or later

    • Unknown

    Description

      We have a camel route, which in it's simplified form looks like this:

      @Component
      public class CamelRouteBuilder extends RouteBuilder{
      
          @Override
          public void configure() throws Exception {
      
              restConfiguration().component("servlet");
      
              rest("/hello")
                  .get("/{first_name}")
                  .to("direct:helloworld");
      
              from("direct:helloworld")
                  .transform()
                  .simple("Hi ${header.first_name}");
          }
      }
      

      This works fine on Spring Boot 3.1.11 / Camel 4.0.5 . When we upgrade to Camel 4.2.0 or later, it fails on startup with the following exception:
       

      Caused by: java.util.regex.PatternSyntaxException: named capturing group is missing trailing '>' near index 17
      \/hello\/(?<first_name>[^\/]+)
                       ^
              at java.base/java.util.regex.Pattern.error(Pattern.java:2028) ~[na:na]
              at java.base/java.util.regex.Pattern.groupname(Pattern.java:2945) ~[na:na]
              at java.base/java.util.regex.Pattern.group0(Pattern.java:2990) ~[na:na]
              at java.base/java.util.regex.Pattern.sequence(Pattern.java:2124) ~[na:na]
              at java.base/java.util.regex.Pattern.expr(Pattern.java:2069) ~[na:na]
              at java.base/java.util.regex.Pattern.compile(Pattern.java:1783) ~[na:na]
              at java.base/java.util.regex.Pattern.<init>(Pattern.java:1430) ~[na:na]
              at java.base/java.util.regex.Pattern.compile(Pattern.java:1069) ~[na:na]
              at org.apache.camel.support.RestConsumerContextPathMatcher.register(RestConsumerContextPathMatcher.java:255) ~[camel-support-4.2.0.jar:4.2.0]
              at org.apache.camel.http.common.CamelServlet.connect(CamelServlet.java:424) ~[camel-http-common-4.2.0.jar:4.2.0]
              at org.apache.camel.component.servlet.CamelHttpTransportServlet.connect(CamelHttpTransportServlet.java:100) ~[camel-servlet-4.2.0.jar:4.2.0]
      ....
      

      I believe this is due to a change introduced by https://issues.apache.org/jira/browse/CAMEL-8306 in which the following method:

          /**
           * Pre-compiled consumer path for wildcard match
           *
           * @param consumerPath a consumer path
           */
          public static void register(String consumerPath) {
              // Convert URI template to a regex pattern
              String regex = consumerPath
                      .replace("/", "\\/")
                      .replace("{", "(?<")
                      .replace("}", ">[^\\/]+)");
      
              // Add support for wildcard * as path suffix
              regex = regex.replace("*", ".*");
      
              // Match the provided path against the regex pattern
              Pattern pattern = Pattern.compile(regex);
              PATH_PATTERN.put(consumerPath, pattern);
          }
      

       
      does not allow for underscore (and other regex special characters) in the parameter name. I could not find any documentation that describes limits on the characters that can be used in rest templates.

      Our production code has many, many rest routes with many parameters with underscores in them.

      I will prepare a PR with a proposed fix

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              daveroa Dave Riseley
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: