spring-hateoas icon indicating copy to clipboard operation
spring-hateoas copied to clipboard

WebMvcLinkBuilder fails with a non-optional request parameter when calling toUriComponentsBuilder()

Open spstorey opened this issue 3 years ago • 2 comments
trafficstars

When using WebMvcLinkBuilder to build a link to a controller method with a non-optional request parameter, invoving toUriComponentsBuilder() fails.

Version 1.5.0 is fine, 1.5.1 has the issue.

I've created a demo here

https://github.com/spstorey/linkissue/blob/main/src/main/java/com/example/linkissue/RootController.java

spstorey avatar Sep 03 '22 13:09 spstorey

What is the expected outcome of this? It's a non-optional request parameter, and you provide null. That doesn't really make sense, does it? You would create a link that wouldn't actually be resolved to the method it points to.

odrotbohm avatar Sep 14 '22 15:09 odrotbohm

Sorry for the delay in responding, our use case is from one resource, say the application root, to provide a navigable link to another resource. e.g.

{    
   "_links": {
      "self": {
         "href": "https://myservice"
      },
      "myresource": {
         "href": "http://myservice/myresource?requiredparam={requiredparam}{&optionalparam}"
      }
   }
}

I've implemented this in the example demo app. Adding the Link to a RepresentationModel works fine but pointing at that same method if you want to get the UriComponentsBuilder from the WebMvcLinkBuilder instance the new version of hateoas blows up. I think the underlaying library has changed and it no longer tolerates the {} chars.

I get the following stack

java.net.URISyntaxException: Illegal character in query at index 47: http://localhost:8080/myresource?requiredparam={requiredparam}
	at java.base/java.net.URI$Parser.fail(URI.java:2974) ~[na:na]
	at java.base/java.net.URI$Parser.checkChars(URI.java:3145) ~[na:na]
	at java.base/java.net.URI$Parser.parseHierarchical(URI.java:3233) ~[na:na]
	at java.base/java.net.URI$Parser.parse(URI.java:3175) ~[na:na]
	at java.base/java.net.URI.<init>(URI.java:623) ~[na:na]
	at java.base/java.net.URI.create(URI.java:904) ~[na:na]
	at org.springframework.hateoas.server.core.LinkBuilderSupport.toUri(LinkBuilderSupport.java:122) ~[spring-hateoas-1.5.1.jar:1.5.1]
	at org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.toUriComponentsBuilder(WebMvcLinkBuilder.java:255) ~[spring-hateoas-1.5.1.jar:1.5.1]
	at com.example.linkissue.RootController.root(RootController.java:19) ~[classes/:na]...

shaunstorey avatar Sep 27 '22 22:09 shaunstorey