Skip to content

Commit 4fbdad5

Browse files
authored
Remove Template Expression naming restrictions (#1139)
Fixes #1036 Relaxed the regular expression used to determine if an expression is valid to support additional expression variable names. We will no longer restrict what an expression name can be.
1 parent 9b01c81 commit 4fbdad5

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

core/src/main/java/feign/template/Expressions.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public final class Expressions {
3838
*
3939
* see https://tools.ietf.org/html/rfc6570#section-2.3 for more information.
4040
*/
41-
expressions.put(Pattern.compile("(\\w[-\\w.\\[\\]]*[ ]*)(:(.+))?"),
41+
42+
expressions.put(Pattern.compile("^([+#./;?&]?)(.*)$"),
4243
SimpleExpression.class);
4344
}
4445

@@ -70,10 +71,18 @@ public static Expression create(final String value, final FragmentType type) {
7071
Matcher matcher = expressionPattern.matcher(expression);
7172
if (matcher.matches()) {
7273
/* we have a valid variable expression, extract the name from the first group */
73-
variableName = matcher.group(1).trim();
74-
if (matcher.group(2) != null && matcher.group(3) != null) {
75-
/* this variable contains an optional pattern */
76-
variablePattern = matcher.group(3);
74+
variableName = matcher.group(2).trim();
75+
if (variableName.contains(":")) {
76+
/* split on the colon */
77+
String[] parts = variableName.split(":");
78+
variableName = parts[0];
79+
variablePattern = parts[1];
80+
}
81+
82+
/* look for nested expressions */
83+
if (variableName.contains("{")) {
84+
/* nested, literal */
85+
return null;
7786
}
7887
}
7988

core/src/test/java/feign/template/QueryTemplateTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,15 @@ public void expandCollectionValueWithBrackets() {
176176
/* brackets will be pct-encoded */
177177
assertThat(expanded).isEqualToIgnoringCase("collection%5B%5D=1,2");
178178
}
179+
180+
@Test
181+
public void expandCollectionValueWithDollar() {
182+
QueryTemplate template =
183+
QueryTemplate.create("$collection", Collections.singletonList("{$collection}"),
184+
Util.UTF_8, CollectionFormat.CSV);
185+
String expanded = template.expand(Collections.singletonMap("$collection",
186+
Arrays.asList("1", "2")));
187+
/* brackets will be pct-encoded */
188+
assertThat(expanded).isEqualToIgnoringCase("$collection=1,2");
189+
}
179190
}

0 commit comments

Comments
 (0)