Skip to content

Commit 0712563

Browse files
committed
#1696 - Re-instantiate conversion for request parameter template variables.
When adding a request parameter template variable and the URI part of the template already contains a request parameter we now automatically turn the variable type into request parameter continuation. That apparently got lost in the move to level 4 URI templates in #1583.
1 parent 2cac83f commit 0712563

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/main/java/org/springframework/hateoas/UriTemplate.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.hateoas.TemplateVariable.VariableType;
3333
import org.springframework.lang.Nullable;
3434
import org.springframework.util.Assert;
35+
import org.springframework.util.MultiValueMap;
3536
import org.springframework.util.StringUtils;
3637
import org.springframework.web.util.UriBuilderFactory;
3738
import org.springframework.web.util.UriComponents;
@@ -177,8 +178,9 @@ public UriTemplate with(TemplateVariables variables) {
177178

178179
for (TemplateVariable variable : variables) {
179180

181+
MultiValueMap<String, String> parameters = components.getQueryParams();
180182
boolean isRequestParam = variable.isRequestParameterVariable();
181-
boolean alreadyPresent = components.getQueryParams().containsKey(variable.getName());
183+
boolean alreadyPresent = parameters.containsKey(variable.getName());
182184

183185
if (isRequestParam && alreadyPresent) {
184186
continue;
@@ -188,6 +190,11 @@ public UriTemplate with(TemplateVariables variables) {
188190
continue;
189191
}
190192

193+
// Use request parameter continuation if base contains parameters already
194+
if (!parameters.isEmpty() && variable.getType().equals(VariableType.REQUEST_PARAM)) {
195+
variable = variable.withType(VariableType.REQUEST_PARAM_CONTINUED);
196+
}
197+
191198
ExpandGroup existing = groups.findLastExpandGroupOfType(variable.getType());
192199
ExpandGroup group = new ExpandGroup(Collections.singletonList(variable));
193200

src/test/java/org/springframework/hateoas/UriTemplateUnitTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,15 @@ void expandsCompositePaths() {
366366
assertThat(uri).isEqualTo(URI.create("/foo/first/second"));
367367
}
368368

369+
@Test // #1696
370+
void adaptsRequestParamVariableToContinuationIfBaseUriContainsParameter() {
371+
372+
UriTemplate template = UriTemplate.of("/path/{bar}/foo.zip?type=foo")
373+
.with(new TemplateVariable("foobar", VariableType.REQUEST_PARAM));
374+
375+
assertThat(template.toString()).isEqualTo("/path/{bar}/foo.zip?type=foo{&foobar}");
376+
}
377+
369378
private static void assertVariables(UriTemplate template, TemplateVariable... variables) {
370379
assertVariables(template, Arrays.asList(variables));
371380
}

0 commit comments

Comments
 (0)