Skip to content

Commit

Permalink
spring-projects#2168 No TemplateVariable generated for null Pageable
Browse files Browse the repository at this point in the history
  • Loading branch information
reda-alaoui committed Apr 18, 2022
1 parent fa1b78c commit adc32b2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<xmlbeam>1.4.22</xmlbeam>

<java-module-name>spring.data.commons</java-module-name>

<spring-hateoas>1.1.0.0.cosium</spring-hateoas>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* @since 1.6
* @author Oliver Gierke
* @author Nick Williams
* @author Réda Housni Alaoui
*/
@SuppressWarnings("null")
public class HateoasPageableHandlerMethodArgumentResolver extends PageableHandlerMethodArgumentResolver
Expand Down Expand Up @@ -129,6 +130,11 @@ public void enhance(UriComponentsBuilder builder, @Nullable MethodParameter para
this.sortResolver.enhance(builder, parameter, pageable.getSort());
}

@Override
public TemplateVariables enhance(TemplateVariables templateVariables, UriComponents uriComponents, MethodParameter parameter) {
return templateVariables.concat(getPaginationTemplateVariables(parameter, uriComponents));
}

private static HateoasSortHandlerMethodArgumentResolver getDefaultedSortResolver(
@Nullable HateoasSortHandlerMethodArgumentResolver sortResolver) {
return sortResolver == null ? DEFAULT_SORT_RESOLVER : sortResolver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* @author Oliver Gierke
* @author Thomas Darimont
* @author Nick Williams
* @author Réda Housni Alaoui
*/
public class HateoasSortHandlerMethodArgumentResolver extends SortHandlerMethodArgumentResolver
implements UriComponentsContributor {
Expand Down Expand Up @@ -82,4 +83,9 @@ public void enhance(UriComponentsBuilder builder, @Nullable MethodParameter para
builder.queryParam(sortParameter, expression);
}
}

@Override
public TemplateVariables enhance(TemplateVariables templateVariables, UriComponents uriComponents, MethodParameter parameter) {
return templateVariables.concat(getSortTemplateVariables(parameter, uriComponents));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.core.MethodParameter;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.hateoas.TemplateVariables;
import org.springframework.hateoas.server.mvc.UriComponentsContributor;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponents;
Expand All @@ -32,6 +33,7 @@
* Unit tests for {@link HateoasPageableHandlerMethodArgumentResolver}.
*
* @author Oliver Gierke
* @author Réda Housni Alaoui
*/
class HateoasPageableHandlerMethodArgumentResolverUnitTests
extends PageableHandlerMethodArgumentResolverUnitTests {
Expand Down Expand Up @@ -119,6 +121,18 @@ void enhancesUnpaged() {
assertThat(builder).isEqualTo(builder);
}

@Test // DATACMNS-1752
void enhanceTemplateVariables() {

UriComponents uriComponents = UriComponentsBuilder.fromPath("/foo").build();

HateoasPageableHandlerMethodArgumentResolver resolver = getResolver();
resolver.setPageParameterName("foo");
String variables = resolver.enhance(TemplateVariables.NONE, uriComponents, null).toString();

assertThat(variables).isEqualTo("{?foo,size,sort}");
}

@Override
protected HateoasPageableHandlerMethodArgumentResolver getResolver() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
import org.junit.jupiter.api.Test;
import org.springframework.core.MethodParameter;
import org.springframework.data.domain.Sort;
import org.springframework.hateoas.TemplateVariables;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

/**
* Unit tests for {@link HateoasSortHandlerMethodArgumentResolver}
*
* @author Oliver Gierke
* @author Réda Housni Alaoui
*/
class HateoasSortHandlerMethodArgumentResolverUnitTests extends SortHandlerMethodArgumentResolverUnitTests {

Expand Down Expand Up @@ -57,6 +59,15 @@ void returnCorrectTemplateVariables() {
assertThat(resolver.getSortTemplateVariables(null, uriComponents).toString()).isEqualTo("{?sort}");
}

@Test // DATACMNS-1752
void enhanceTemplateVariables(){

UriComponents uriComponents = UriComponentsBuilder.fromPath("/").build();

HateoasSortHandlerMethodArgumentResolver resolver = new HateoasSortHandlerMethodArgumentResolver();
assertThat(resolver.enhance(TemplateVariables.NONE, uriComponents, null).toString()).isEqualTo("{?sort}");
}

private void assertUriStringFor(Sort sort, String expected) throws Exception {
assertUriStringFor(sort, expected, "/");
}
Expand Down

0 comments on commit adc32b2

Please sign in to comment.