Commit 38d44409 authored by Stephane Nicoll's avatar Stephane Nicoll

Merge pull request #11403 from marceloverdijk:set_one_indexed_parameters

* pr/11403:
  Polish "Improve Spring Data Web configuration properties"
  Improve Spring Data Web configuration properties
parents 8d1bd7eb cfa3cab9
......@@ -65,10 +65,14 @@ public class SpringDataWebAutoConfiguration {
public PageableHandlerMethodArgumentResolverCustomizer pageableCustomizer() {
return (resolver) -> {
Pageable pageable = this.properties.getPageable();
resolver.setFallbackPageable(
PageRequest.of(0, pageable.getDefaultPageSize()));
resolver.setPageParameterName(pageable.getPageParameter());
resolver.setSizeParameterName(pageable.getSizeParameter());
resolver.setOneIndexedParameters(pageable.isOneIndexedParameters());
resolver.setPrefix(pageable.getPrefix());
resolver.setQualifierDelimiter(pageable.getQualifierDelimiter());
resolver.setFallbackPageable(
PageRequest.of(0, pageable.getDefaultPageSize()));
resolver.setMaxPageSize(pageable.getMaxPageSize());
};
}
......
......@@ -54,11 +54,33 @@ public class SpringDataWebProperties {
*/
private String sizeParameter = "size";
/**
* Whether to expose and assume 1-based page number indexes. Defaults to "false",
* meaning a page number of 0 in the request equals the first page.
*/
private boolean oneIndexedParameters = false;
/**
* General prefix to be prepended to the page number and page size parameters.
*/
private String prefix = "";
/**
* Delimiter to be used between the qualifier and the actual page number and size
* properties.
*/
private String qualifierDelimiter = "_";
/**
* Default page size.
*/
private int defaultPageSize = 20;
/**
* Maximum page size to be accepted.
*/
private int maxPageSize = 2000;
public String getPageParameter() {
return this.pageParameter;
}
......@@ -75,6 +97,30 @@ public class SpringDataWebProperties {
this.sizeParameter = sizeParameter;
}
public boolean isOneIndexedParameters() {
return this.oneIndexedParameters;
}
public void setOneIndexedParameters(boolean oneIndexedParameters) {
this.oneIndexedParameters = oneIndexedParameters;
}
public String getPrefix() {
return this.prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getQualifierDelimiter() {
return this.qualifierDelimiter;
}
public void setQualifierDelimiter(String qualifierDelimiter) {
this.qualifierDelimiter = qualifierDelimiter;
}
public int getDefaultPageSize() {
return this.defaultPageSize;
}
......@@ -82,6 +128,15 @@ public class SpringDataWebProperties {
public void setDefaultPageSize(int defaultPageSize) {
this.defaultPageSize = defaultPageSize;
}
public int getMaxPageSize() {
return this.maxPageSize;
}
public void setMaxPageSize(int maxPageSize) {
this.maxPageSize = maxPageSize;
}
}
/**
......
......@@ -78,15 +78,49 @@ public class SpringDataWebAutoConfigurationTests {
public void customizePageable() {
load("spring.data.web.pageable.page-parameter=p",
"spring.data.web.pageable.size-parameter=s",
"spring.data.web.pageable.default-page-size=10");
"spring.data.web.pageable.default-page-size=10",
"spring.data.web.pageable.prefix=abc",
"spring.data.web.pageable.qualifier-delimiter=__",
"spring.data.web.pageable.max-page-size=100",
"spring.data.web.pageable.one-indexed-parameters=true");
PageableHandlerMethodArgumentResolver argumentResolver = this.context
.getBean(PageableHandlerMethodArgumentResolver.class);
assertThat(ReflectionTestUtils.getField(argumentResolver, "pageParameterName"))
.isEqualTo("p");
assertThat(ReflectionTestUtils.getField(argumentResolver, "sizeParameterName"))
.isEqualTo("s");
assertThat(ReflectionTestUtils.getField(argumentResolver, "oneIndexedParameters"))
.isEqualTo(true);
assertThat(ReflectionTestUtils.getField(argumentResolver, "prefix"))
.isEqualTo("abc");
assertThat(ReflectionTestUtils.getField(argumentResolver, "qualifierDelimiter"))
.isEqualTo("__");
assertThat(ReflectionTestUtils.getField(argumentResolver, "fallbackPageable"))
.isEqualTo(PageRequest.of(0, 10));
assertThat(ReflectionTestUtils.getField(argumentResolver, "maxPageSize"))
.isEqualTo(100);
}
@Test
public void defaultPageable() {
load();
PageableHandlerMethodArgumentResolver argumentResolver = this.context
.getBean(PageableHandlerMethodArgumentResolver.class);
SpringDataWebProperties.Pageable properties = new SpringDataWebProperties().getPageable();
assertThat(ReflectionTestUtils.getField(argumentResolver, "pageParameterName"))
.isEqualTo(properties.getPageParameter());
assertThat(ReflectionTestUtils.getField(argumentResolver, "sizeParameterName"))
.isEqualTo(properties.getSizeParameter());
assertThat(ReflectionTestUtils.getField(argumentResolver, "oneIndexedParameters"))
.isEqualTo(properties.isOneIndexedParameters());
assertThat(ReflectionTestUtils.getField(argumentResolver, "prefix"))
.isEqualTo(properties.getPrefix());
assertThat(ReflectionTestUtils.getField(argumentResolver, "qualifierDelimiter"))
.isEqualTo(properties.getQualifierDelimiter());
assertThat(ReflectionTestUtils.getField(argumentResolver, "fallbackPageable"))
.isEqualTo(PageRequest.of(0, properties.getDefaultPageSize()));
assertThat(ReflectionTestUtils.getField(argumentResolver, "maxPageSize"))
.isEqualTo(properties.getMaxPageSize());
}
@Test
......
......@@ -646,7 +646,11 @@ content into your application. Rather, pick only the properties that you need.
# DATA WEB ({sc-spring-boot-autoconfigure}/data/web/SpringDataWebProperties.{sc-ext}[SpringDataWebProperties])
spring.data.web.pageable.default-page-size=20 # Default page size.
spring.data.web.pageable.max-page-size=2000 # Maximum page size to be accepted.
spring.data.web.pageable.one-indexed-parameters=false # Whether to expose and assume 1-based page number indexes.
spring.data.web.pageable.page-parameter=page # Page index parameter name.
spring.data.web.pageable.prefix= # General prefix to be prepended to the page number and page size parameters.
spring.data.web.pageable.qualifier-delimiter=_ # Delimiter to be used between the qualifier and the actual page number and size properties.
spring.data.web.pageable.size-parameter=size # Page size parameter name.
spring.data.web.sort.sort-parameter=sort # Sort parameter name.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment