#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.
This commit is contained in:
@@ -32,6 +32,7 @@ import java.util.stream.Collectors;
|
||||
import org.springframework.hateoas.TemplateVariable.VariableType;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.util.UriBuilderFactory;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
@@ -177,8 +178,9 @@ public class UriTemplate implements Iterable<TemplateVariable>, Serializable {
|
||||
|
||||
for (TemplateVariable variable : variables) {
|
||||
|
||||
MultiValueMap<String, String> parameters = components.getQueryParams();
|
||||
boolean isRequestParam = variable.isRequestParameterVariable();
|
||||
boolean alreadyPresent = components.getQueryParams().containsKey(variable.getName());
|
||||
boolean alreadyPresent = parameters.containsKey(variable.getName());
|
||||
|
||||
if (isRequestParam && alreadyPresent) {
|
||||
continue;
|
||||
@@ -188,6 +190,11 @@ public class UriTemplate implements Iterable<TemplateVariable>, Serializable {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Use request parameter continuation if base contains parameters already
|
||||
if (!parameters.isEmpty() && variable.getType().equals(VariableType.REQUEST_PARAM)) {
|
||||
variable = variable.withType(VariableType.REQUEST_PARAM_CONTINUED);
|
||||
}
|
||||
|
||||
ExpandGroup existing = groups.findLastExpandGroupOfType(variable.getType());
|
||||
ExpandGroup group = new ExpandGroup(Collections.singletonList(variable));
|
||||
|
||||
|
||||
@@ -366,6 +366,15 @@ class UriTemplateUnitTest {
|
||||
assertThat(uri).isEqualTo(URI.create("/foo/first/second"));
|
||||
}
|
||||
|
||||
@Test // #1696
|
||||
void adaptsRequestParamVariableToContinuationIfBaseUriContainsParameter() {
|
||||
|
||||
UriTemplate template = UriTemplate.of("/path/{bar}/foo.zip?type=foo")
|
||||
.with(new TemplateVariable("foobar", VariableType.REQUEST_PARAM));
|
||||
|
||||
assertThat(template.toString()).isEqualTo("/path/{bar}/foo.zip?type=foo{&foobar}");
|
||||
}
|
||||
|
||||
private static void assertVariables(UriTemplate template, TemplateVariable... variables) {
|
||||
assertVariables(template, Arrays.asList(variables));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user