Skip to content

Commit 7ba16df

Browse files
authored
Replaced comma with Constant Delimiter in Template (OpenFeign#930)
Replaced comma with Constant Delimiter in Template Fixes OpenFeign#924 Commas were used to identify iterable content, which conflicted when a comma delimited literal was provided during expansion. This change switches commas for semi-colons, which are considered reserved secondary delimiters in RFC 6750 and should not be used without being pct-encoded. Should be a safer choice.
1 parent 6c4dfbd commit 7ba16df

4 files changed

Lines changed: 58 additions & 7 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ String expand(Object variable, boolean encode) {
105105
for (Object item : ((Iterable) variable)) {
106106
items.add((encode) ? encode(item) : item.toString());
107107
}
108-
expanded.append(String.join(",", items));
108+
expanded.append(String.join(Template.COLLECTION_DELIMITER, items));
109109
} else {
110110
expanded.append((encode) ? encode(variable) : variable);
111111
}

‎core/src/main/java/feign/template/QueryTemplate.java‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.List;
2424
import java.util.Map;
2525
import java.util.Objects;
26-
import java.util.function.Predicate;
2726
import java.util.stream.Collectors;
2827
import java.util.stream.StreamSupport;
2928

@@ -32,8 +31,7 @@
3231
*/
3332
public final class QueryTemplate extends Template {
3433

35-
public static final String UNDEF = "undef";
36-
/* cache a copy of the variables for lookup later */
34+
private static final String UNDEF = "undef";
3735
private List<String> values;
3836
private final Template name;
3937
private final CollectionFormat collectionFormat;
@@ -64,7 +62,7 @@ public static QueryTemplate create(String name,
6462
Iterable<String> values,
6563
Charset charset,
6664
CollectionFormat collectionFormat) {
67-
if (name == null || name.isEmpty()) {
65+
if (Util.isBlank(name)) {
6866
throw new IllegalArgumentException("name is required.");
6967
}
7068

@@ -82,7 +80,7 @@ public static QueryTemplate create(String name,
8280
while (iterator.hasNext()) {
8381
template.append(iterator.next());
8482
if (iterator.hasNext()) {
85-
template.append(",");
83+
template.append(COLLECTION_DELIMITER);
8684
}
8785
}
8886

@@ -181,7 +179,7 @@ private String queryString(String name, String values) {
181179
}
182180

183181
/* covert the comma separated values into a value query string */
184-
List<String> resolved = Arrays.stream(values.split(","))
182+
List<String> resolved = Arrays.stream(values.split(COLLECTION_DELIMITER))
185183
.filter(Objects::nonNull)
186184
.filter(s -> !UNDEF.equalsIgnoreCase(s))
187185
.collect(Collectors.toList());

‎core/src/main/java/feign/template/Template.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
3232
*/
3333
public class Template {
3434

35+
/*
36+
* special delimiter for collection based expansion, in an attempt to avoid accidental splitting
37+
* for resolved values. semi-colon was chosen because it is a reserved character that must be
38+
* pct-encoded and should not appear unencoded.
39+
*/
40+
static final String COLLECTION_DELIMITER = ";";
41+
3542
private static final Logger logger = Logger.getLogger(Template.class.getName());
3643
private static final Pattern QUERY_STRING_PATTERN = Pattern.compile("(?<!\\{)(\\?)");
3744
private final String template;

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,50 @@ public void expandNameUnresolved() {
118118
String expanded = template.expand(Collections.singletonMap("name", "firsts"));
119119
assertThat(expanded).isEqualToIgnoringCase("%7Bparameter%7D=James&%7Bparameter%7D=Jason");
120120
}
121+
122+
@Test
123+
public void expandSingleValueWithComma() {
124+
QueryTemplate template =
125+
QueryTemplate.create("collection",
126+
Collections.singletonList("{collection}"), Util.UTF_8);
127+
String expanded = template.expand(Collections.singletonMap("collection", "one,two,three"));
128+
assertThat(expanded).isEqualToIgnoringCase("collection=one,two,three");
129+
}
130+
131+
@Test
132+
public void expandSingleValueWithPipe() {
133+
QueryTemplate template =
134+
QueryTemplate.create("collection",
135+
Collections.singletonList("{collection}"), Util.UTF_8);
136+
String expanded = template.expand(Collections.singletonMap("collection", "one|two|three"));
137+
assertThat(expanded).isEqualToIgnoringCase("collection=one%7Ctwo%7Cthree");
138+
}
139+
140+
@Test
141+
public void expandSingleValueWithSpace() {
142+
QueryTemplate template =
143+
QueryTemplate.create("collection",
144+
Collections.singletonList("{collection}"), Util.UTF_8);
145+
String expanded = template.expand(Collections.singletonMap("collection", "one two three"));
146+
assertThat(expanded).isEqualToIgnoringCase("collection=one%20two%20three");
147+
}
148+
149+
@Test
150+
public void expandSingleValueWithTab() {
151+
QueryTemplate template =
152+
QueryTemplate.create("collection",
153+
Collections.singletonList("{collection}"), Util.UTF_8);
154+
String expanded = template.expand(Collections.singletonMap("collection", "one\ttwo\tthree"));
155+
assertThat(expanded).isEqualToIgnoringCase("collection=one%09two%09three");
156+
}
157+
158+
@Test
159+
public void expandSingleValueWithJson() {
160+
QueryTemplate template =
161+
QueryTemplate.create("json", Collections.singletonList("{json}"), Util.UTF_8);
162+
String expanded = template.expand(
163+
Collections.singletonMap("json", "{\"name\":\"feign\",\"version\": \"10\"}"));
164+
assertThat(expanded).isEqualToIgnoringCase(
165+
"json=%7B%22name%22:%22feign%22,%22version%22:%20%2210%22%7D");
166+
}
121167
}

0 commit comments

Comments
 (0)