Skip to content

HateoasSortHandlerMethodArgumentResolver should append * to the 'sort' parameter template variable. #2531

Closed
@dayre

Description

@dayre

The Spring docs (see Table 1) specify that multiple sort parameters should be present in the request params for Pageable instances when sorting by multiple fields. (e.g. sort=field1,asc&sort=field2,desc).

Spring HATEOAS via the data commons HateoasSortHandlerMethodArgumentResolver class generates a URI template variable for the 'sort' param without the URI template explode modifier (*).

http://localhost:8080/api/clients{?page,size,sort,projection}

For client side libraries performing URI expansion based on rfc6570, in conditions where there are multiple sort parameters, the template provided by Spring without the modifier will generate URLs of the following format:

// using https://www.npmjs.com/package/uri-template based on rfc6570
const templ = uriTemplate.parse("http://localhost:8080/api/clients{?page,size,sort,projection}");
const expanded = templ.expand({sort : ["id,asc", "lastModifiedDate,desc"]});

// BAD -> http://localhost:8080/api/clients?sort=id%2Casc,lastModifiedDate%2Cdesc

This URL isn't in the correct format outlined in the Spring docs (see Table 1), and a format not expected by the handlers resulting in incorrect parsing (results in two order by's, but direction is DESC for both).

The explode modifier added to sort would allow rfc6570 compliant libraries to generate the correct URLs expected by Spring and allow correct sorting criteria to be generated:

const templ = uriTemplate.parse("http://localhost:8080/api/clients{?page,size,sort*,projection}");
const expanded = templ.expand({sort : ["id,asc", "lastModifiedDate,desc"]});

// GOOD -> http://localhost:8080/api/clients?sort=id%2Casc&sort=lastModifiedDate%2Cdesc

There is an existing related issue #1242, which deals with multiple values and generated query method templates... which i can see needs addressing deeper in the HATEOAS codebase, but to fix sorting properly by anything Pageable, it seems like a simple fix would be to append "*" to the sort param generated by this class.

org.springframework.data.web.HateoasSortHandlerMethodArgumentResolver#getSortTemplateVariables

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions