Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions core/src/main/java/feign/template/Expressions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions core/src/test/java/feign/template/ExpressionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) + "}";
Expand Down