spring-projects / spring-data-commons

Spring Data Commons. Interfaces and code shared between the various datastore specific implementations.
https://spring.io/projects/spring-data
Apache License 2.0
777 stars 675 forks source link

No TemplateVariable generated for null Pageable [DATACMNS-1752] #2168

Open spring-projects-issues opened 4 years ago

spring-projects-issues commented 4 years ago

Réda Housni Alaoui opened DATACMNS-1752 and commented

We use:

HateoasPageableHandlerMethodArgumentResolver is registered in the Spring context.

Given the following controller:

 @Controller
  @RequestMapping("/")
  public static class MyController {

    private final WebMvcLinkBuilderFactory webMvcLinkBuilderFactory;

    public MyController(WebMvcLinkBuilderFactory webMvcLinkBuilderFactory) {
      this.webMvcLinkBuilderFactory = webMvcLinkBuilderFactory;
    }

    @GetMapping
    public ResponseEntity<?> links() {
      return ResponseEntity.ok(
          new RepresentationModel<>(
              Collections.singletonList(
                  webMvcLinkBuilderFactory
                      .linkTo(methodOn(MyController.class).pageable(null))
                      .withRel("pageable"))));
    }

    @GetMapping("/pageable")
    public ResponseEntity<?> pageable(Pageable pageable) {
      return ResponseEntity.noContent().build();
    }
}

Querying / with accept: application/hal+json produces:

{
  "_links": {
    "pageable": {
      "href": "http://localhost/pageable"
    }
  }
}

We were expecting the link to be templated with the following optional variables:

The issue on the Spring HATEOAS side is https://github.com/spring-projects/spring-hateoas/issues/706

How to fix that?


Affects: 2.3.1 (Neumann SR1)

reda-alaoui commented 2 years ago

Please note that https://github.com/spring-projects/spring-hateoas/issues/706 can be fixed by applying https://github.com/spring-projects/spring-hateoas/pull/1312 . After that, a pull request inspired by https://github.com/Cosium/spring-data-commons/commit/b9600b18b8bd94fefb10ef85ee0c72ff9b4225c0 fixes the current issue.