spring-projects / spring-hateoas

Spring HATEOAS - Library to support implementing representations for hyper-text driven REST web services.
https://spring.io/projects/spring-hateoas
Apache License 2.0
1.03k stars 476 forks source link

Request parameters are omitted from templated url when they are required and have a default value. #545

Open manjushetkar opened 7 years ago

manjushetkar commented 7 years ago
@RequestMapping(path="/greet")
public HttpEntity<ResourceSupport> greeting(
            @RequestParam(value = "param1", defaultValue = "hello") String param1,
            @RequestParam(value = "param2", defaultValue = "default2") String param2) {
ResourceSupport resource = new ResourceSupport();
resource.add(linkTo(methodOn(ExampleController.class).greeting(null, null).withSelfRel());
return new ResponseEntity<ResourceSupport>(resource, HttpStatus.OK);
}

I am getting,

{
    "_links": {
        "self": {
            "href": "http://localhost:8082/greet"
        }
    }
}

while I am expecting,

{
    "_links": {
        "self": {
            "href": "http://localhost:8082/greet?param1={param1}&param2={param2}",
            "templated": true
        }
    }
}

is this the expected behaviour or am I missing something?

169

Templating just works fine when I have optional request params with default values, but they are omitted when they are required and have default values.

rworsnop commented 7 years ago

I would think that, given the parameters are effectively optional, you should expect this:

"href": "http://localhost:8082/greet{?param1,param2}"
kurbatov commented 6 years ago

I experience the same issue.

Javadoc of RequestParam#defaultValue states that

Supplying a default value implicitly sets required to false.

However in order to get those params templated I have to explicitly specify required = false in each and every RequestParam annotation with defaultValue.

It would be great to know whether this issue is taken into consideration.

f-cramer commented 3 years ago

We ran into this issue as well. Shouldn't fixing it be as easy as calling !isRequired() || parameter.isOptional() instead of !(annotation != null && annotation.required()) || parameter.isOptional() in org.springframework.hateoas.server.core.WebHandler.RequestParamParameter#getVerifiedValue(Object[])