Spring-hateoas: ControllerLinkBuilder link expansion leads to multiple url-encoding of parameters

Created on 21 Mar 2017  路  4Comments  路  Source: spring-projects/spring-hateoas

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

Most helpful comment

This bug makes the ControllerLinkBuilder pretty much useless for creating non-templated links. Any idea on the progress of this PR?

All 4 comments

Same issue here.

Experiencing problems with this issue as well.

Approaching a year on this bug. Anybody looking into it? or found a viable workaround?

This bug makes the ControllerLinkBuilder pretty much useless for creating non-templated links. Any idea on the progress of this PR?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ravishrathod picture ravishrathod  路  5Comments

reda-alaoui picture reda-alaoui  路  7Comments

onacit picture onacit  路  6Comments

pmihalcin picture pmihalcin  路  3Comments

odrotbohm picture odrotbohm  路  6Comments