Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
spstorey opened this issue Sep 3, 2022 · 2 comments

Comments

@spstorey
Copy link

spstorey commented Sep 3, 2022

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

@odrotbohm
Copy link
Member

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.

@shaunstorey
Copy link

shaunstorey commented Sep 27, 2022

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]...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants