Description
When building a link with methodOn
, and supplying values for some parameters but not all, a link template with variables for the remaining parameters is created, while the supplied values are already expanded. When expanding the resulting template, the initially supplied values are url-encoded again, leading to invalid URLs. This affects explicitly calling expand()
of course, but also the pagination links of the PagedResourcesAssembler
from Spring Data. Here is some code example:
@GetMapping("/")
public Resource test(
@RequestParam(name = "date1", required = false) @DateTimeFormat(iso = DATE_TIME) Date date1,
@RequestParam(name = "date2", required = false) @DateTimeFormat(iso = DATE_TIME) Date date2) {
Link template = linkTo(methodOn(TestController.class).test(date1, null)).withRel("template");
Link expanded = linkTo(methodOn(TestController.class).test(date1, null)).withRel("expanded").expand();
return new Resource("", template, expanded);
}
This leads to following response:
{
"_links": {
"expanded": {
"href": "http://localhost:8080/?date1=2017-03-21T19:20:20.000%25252B0000"
},
"template": {
"href": "http://localhost:8080/?date1=2017-03-21T19:20:20.000%2B0000{&date2}",
"templated": true
}
},
"content": ""
}
As you see, the +
in the date has been encoded correctly in the template, but the %
-sign has been encoded again in the expanded link, even twice as shown by the additional 25
.
I have a runnable example with a test case in this repository