diff --git a/core/src/main/java/feign/template/Expressions.java b/core/src/main/java/feign/template/Expressions.java index 65fa5123e2..e37872fff6 100644 --- a/core/src/main/java/feign/template/Expressions.java +++ b/core/src/main/java/feign/template/Expressions.java @@ -22,6 +22,7 @@ import java.util.Optional; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; public final class Expressions { @@ -104,15 +105,23 @@ public static Expression create(final String value) { } } - /* check for an operator */ - if (PATH_STYLE_OPERATOR.equalsIgnoreCase(operator)) { - return new PathStyleExpression(variableName, variablePattern); - } + try { + /* check for an operator */ + if (PATH_STYLE_OPERATOR.equalsIgnoreCase(operator)) { + return new PathStyleExpression(variableName, variablePattern); + } - /* default to simple */ - return SimpleExpression.isSimpleExpression(value) - ? new SimpleExpression(variableName, variablePattern) - : null; // Return null if it can't be validated as a Simple Expression -- Probably a Literal + /* default to simple; null if it can't be validated as a Simple Expression -- probably a literal */ + return SimpleExpression.isSimpleExpression(value) + ? new SimpleExpression(variableName, variablePattern) + : null; + } catch (PatternSyntaxException e) { + /* + * the ':' value modifier did not contain a valid regular expression, so this is not a usable + * expression -- treat it as a literal instead of failing template construction. + */ + return null; + } } private static String stripBraces(String expression) { diff --git a/core/src/test/java/feign/template/ExpressionsTest.java b/core/src/test/java/feign/template/ExpressionsTest.java index 0586bd231e..49af8aec27 100644 --- a/core/src/test/java/feign/template/ExpressionsTest.java +++ b/core/src/test/java/feign/template/ExpressionsTest.java @@ -44,6 +44,17 @@ void malformedExpression() { } } + @Test + void invalidPatternModifierIsTreatedAsLiteral() { + /* + * gh-2739: a header-map value such as "{test:[abc}" parses as a "{name:pattern}" expression, + * but the ':' modifier ("[abc") is not a valid regular expression. Compiling it must not blow + * up template construction with a PatternSyntaxException -- it should fall back to a literal. + */ + Expression expression = Expressions.create("{test:[abc}"); + assertThatObject(expression).isNull(); + } + @Test void malformedBodyTemplate() { String bodyTemplate = "{" + "a".repeat(65536) + "}";