Skip to content

Commit

Permalink
Add configuration key "spring.data.web.pageable.serialization-mode" d…
Browse files Browse the repository at this point in the history
  • Loading branch information
quaff committed Feb 29, 2024
1 parent 70769d9 commit 7cd5f4a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,11 +26,13 @@
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties.Pageable;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.data.web.config.EnableSpringDataWebSupport;
import org.springframework.data.web.config.PageableHandlerMethodArgumentResolverCustomizer;
import org.springframework.data.web.config.SortHandlerMethodArgumentResolverCustomizer;
import org.springframework.data.web.config.SpringDataWebSettings;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
Expand All @@ -42,6 +44,7 @@
*
* @author Andy Wilkinson
* @author Vedran Pavic
* @author Yanming Zhou
* @since 1.2.0
*/
@AutoConfiguration(after = RepositoryRestMvcAutoConfiguration.class)
Expand Down Expand Up @@ -79,4 +82,10 @@ public SortHandlerMethodArgumentResolverCustomizer sortCustomizer() {
return (resolver) -> resolver.setSortParameter(this.properties.getSort().getSortParameter());
}

@Primary // override bean created by @EnableSpringDataWebSupport
@Bean
public SpringDataWebSettings springDataWebSettings() {
return new SpringDataWebSettings(this.properties.getPageable().getSerializationMode());
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,11 +17,13 @@
package org.springframework.boot.autoconfigure.data.web;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.data.web.config.EnableSpringDataWebSupport.PageSerializationMode;

/**
* Configuration properties for Spring Data Web.
*
* @author Vedran Pavic
* @author Yanming Zhou
* @since 2.0.0
*/
@ConfigurationProperties("spring.data.web")
Expand Down Expand Up @@ -81,6 +83,19 @@ public static class Pageable {
*/
private int maxPageSize = 2000;

/**
* Configures how to render {@link org.springframework.data.domain.PageImpl}
* instances. Defaults to {@link PageSerializationMode#DIRECT} for backward
* compatibility reasons. Prefer explicitly setting this to
* {@link PageSerializationMode#VIA_DTO}, or manually convert
* {@link org.springframework.data.domain.PageImpl} instances before handing them
* out of a controller method, either by manually calling
* {@code new PagedModel<>(page)} or using Spring HATEOAS
* {@link org.springframework.hateoas.PagedModel} abstraction.
* @since 3.3
*/
private PageSerializationMode serializationMode = PageSerializationMode.DIRECT;

public String getPageParameter() {
return this.pageParameter;
}
Expand Down Expand Up @@ -137,6 +152,14 @@ public void setMaxPageSize(int maxPageSize) {
this.maxPageSize = maxPageSize;
}

public PageSerializationMode getSerializationMode() {
return this.serializationMode;
}

public void setSerializationMode(PageSerializationMode serializationMode) {
this.serializationMode = serializationMode;
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,8 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.data.web.SortHandlerMethodArgumentResolver;
import org.springframework.data.web.config.EnableSpringDataWebSupport.PageSerializationMode;
import org.springframework.data.web.config.SpringDataWebSettings;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -33,6 +35,7 @@
* @author Andy Wilkinson
* @author Vedran Pavic
* @author Stephane Nicoll
* @author Yanming Zhou
*/
class SpringDataWebAutoConfigurationTests {

Expand All @@ -53,20 +56,24 @@ void autoConfigurationBacksOffInNonWebApplicationContexts() {

@Test
void customizePageable() {
this.contextRunner.withPropertyValues("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.prefix=abc", "spring.data.web.pageable.qualifier-delimiter=__",
"spring.data.web.pageable.max-page-size=100", "spring.data.web.pageable.one-indexed-parameters=true")
this.contextRunner
.withPropertyValues("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.prefix=abc", "spring.data.web.pageable.qualifier-delimiter=__",
"spring.data.web.pageable.max-page-size=100", "spring.data.web.pageable.serialization-mode=VIA_DTO",
"spring.data.web.pageable.one-indexed-parameters=true")
.run((context) -> {
PageableHandlerMethodArgumentResolver argumentResolver = context
.getBean(PageableHandlerMethodArgumentResolver.class);
SpringDataWebSettings springDataWebSettings = context.getBean(SpringDataWebSettings.class);
assertThat(argumentResolver).hasFieldOrPropertyWithValue("pageParameterName", "p");
assertThat(argumentResolver).hasFieldOrPropertyWithValue("sizeParameterName", "s");
assertThat(argumentResolver).hasFieldOrPropertyWithValue("oneIndexedParameters", true);
assertThat(argumentResolver).hasFieldOrPropertyWithValue("prefix", "abc");
assertThat(argumentResolver).hasFieldOrPropertyWithValue("qualifierDelimiter", "__");
assertThat(argumentResolver).hasFieldOrPropertyWithValue("fallbackPageable", PageRequest.of(0, 10));
assertThat(argumentResolver).hasFieldOrPropertyWithValue("maxPageSize", 100);
assertThat(springDataWebSettings.pageSerializationMode()).isEqualTo(PageSerializationMode.VIA_DTO);
});
}

Expand All @@ -76,6 +83,7 @@ void defaultPageable() {
SpringDataWebProperties.Pageable properties = new SpringDataWebProperties().getPageable();
PageableHandlerMethodArgumentResolver argumentResolver = context
.getBean(PageableHandlerMethodArgumentResolver.class);
SpringDataWebSettings springDataWebSettings = context.getBean(SpringDataWebSettings.class);
assertThat(argumentResolver).hasFieldOrPropertyWithValue("pageParameterName",
properties.getPageParameter());
assertThat(argumentResolver).hasFieldOrPropertyWithValue("sizeParameterName",
Expand All @@ -88,6 +96,7 @@ void defaultPageable() {
assertThat(argumentResolver).hasFieldOrPropertyWithValue("fallbackPageable",
PageRequest.of(0, properties.getDefaultPageSize()));
assertThat(argumentResolver).hasFieldOrPropertyWithValue("maxPageSize", properties.getMaxPageSize());
assertThat(springDataWebSettings.pageSerializationMode()).isEqualTo(properties.getSerializationMode());
});
}

Expand All @@ -100,4 +109,12 @@ void customizeSort() {
});
}

@Test
void customizePageSerializationMode() {
this.contextRunner.withPropertyValues("spring.data.web.pageable.serialization-mode=VIA_DTO").run((context) -> {
SpringDataWebSettings springDataWebSettings = context.getBean(SpringDataWebSettings.class);
assertThat(springDataWebSettings.pageSerializationMode()).isEqualTo(PageSerializationMode.VIA_DTO);
});
}

}
2 changes: 1 addition & 1 deletion spring-boot-project/spring-boot-dependencies/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -1795,7 +1795,7 @@ bom {
releaseNotes("https://github.com/spring-projects/spring-batch/releases/tag/v{version}")
}
}
library("Spring Data Bom", "2023.1.3") {
library("Spring Data Bom", "2024.0.0-SNAPSHOT") {
considerSnapshots()
calendarName = "Spring Data Release"
group("org.springframework.data") {
Expand Down

0 comments on commit 7cd5f4a

Please sign in to comment.