Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a configuration property for Spring Data Web's serialization mode #39797

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Down Expand Up @@ -31,6 +31,7 @@
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 +43,7 @@
*
* @author Andy Wilkinson
* @author Vedran Pavic
* @author Yanming Zhou
* @since 1.2.0
*/
@AutoConfiguration(after = RepositoryRestMvcAutoConfiguration.class)
Expand Down Expand Up @@ -79,4 +81,10 @@ public SortHandlerMethodArgumentResolverCustomizer sortCustomizer() {
return (resolver) -> resolver.setSortParameter(this.properties.getSort().getSortParameter());
}

@Bean
@ConditionalOnMissingBean
public SpringDataWebSettings springDataWebSettings() {
quaff marked this conversation as resolved.
Show resolved Hide resolved
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,11 @@ public static class Pageable {
*/
private int maxPageSize = 2000;

/**
* Configures how to render spring data Pageable instances.
*/
private PageSerializationMode serializationMode = PageSerializationMode.DIRECT;
quaff marked this conversation as resolved.
Show resolved Hide resolved

public String getPageParameter() {
return this.pageParameter;
}
Expand Down Expand Up @@ -137,6 +144,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 @@ -21,9 +21,13 @@
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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 +37,7 @@
* @author Andy Wilkinson
* @author Vedran Pavic
* @author Stephane Nicoll
* @author Yanming Zhou
*/
class SpringDataWebAutoConfigurationTests {

Expand All @@ -53,20 +58,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 +85,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 +98,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 +111,32 @@ void customizeSort() {
});
}

@Test
void customizePageSerializationModeViaConfigProps() {
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);
});
}

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

@Configuration
static class AppConfiguration {

@Bean
SpringDataWebSettings springDataWebSettings() {
return new SpringDataWebSettings(PageSerializationMode.DIRECT);
}

}

}
Loading