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

Do not register SpringDataWebSettings if pageSerializationMode is default #3054

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
Expand Up @@ -69,6 +69,7 @@
* @see SpringDataWebConfiguration
* @see HateoasAwareSpringDataWebConfiguration
* @author Oliver Gierke
* @author Yanming Zhou
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE })
Expand Down Expand Up @@ -190,8 +191,16 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B
return;
}

Object pageSerializationMode = attributes.get("pageSerializationMode");

if (pageSerializationMode == PageSerializationMode.DIRECT) {
// do not register SpringDataWebSettings if pageSerializationMode is default
// allow application to register their own SpringDataWebSettings without @Primary
return;
}

AbstractBeanDefinition definition = BeanDefinitionBuilder.rootBeanDefinition(SpringDataWebSettings.class)
.addConstructorArgValue(attributes.get("pageSerializationMode"))
.addConstructorArgValue(pageSerializationMode)
.getBeanDefinition();

String beanName = importBeanNameGenerator.generateBeanName(definition, registry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* JavaConfig class to export Jackson specific configuration.
*
* @author Oliver Gierke
* @author Yanming Zhou
*/
public class SpringDataJacksonConfiguration implements SpringDataJacksonModules {

Expand Down Expand Up @@ -84,7 +85,7 @@ public PageModule(@Nullable SpringDataWebSettings settings) {

addSerializer(UNPAGED_TYPE, new UnpagedAsInstanceSerializer());

if (settings != null && settings.pageSerializationMode() == PageSerializationMode.DIRECT) {
if (settings == null || settings.pageSerializationMode() == PageSerializationMode.DIRECT) {
setMixInAnnotation(PageImpl.class, WarningMixing.class);
} else {
setMixInAnnotation(PageImpl.class, WrappingMixing.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -67,7 +68,7 @@ class EnableSpringDataWebSupportIntegrationTests {

@Configuration
@EnableWebMvc
@EnableSpringDataWebSupport
@EnableSpringDataWebSupport(pageSerializationMode = EnableSpringDataWebSupport.PageSerializationMode.VIA_DTO)
static class SampleConfig {

@Bean
Expand Down Expand Up @@ -304,6 +305,10 @@ void registersSpringDataWebSettingsBean() {
void usesDirectPageSerializationMode() throws Exception {

var applicationContext = WebTestUtils.createApplicationContext(PageSampleConfigWithDirect.class);

// SpringDataWebSettings shouldn't be registered if pageSerializationMode is default
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> applicationContext.getBean(SpringDataWebSettings.class));

var mvc = MockMvcBuilders.webAppContextSetup(applicationContext).build();

mvc.perform(post("/page"))//
Expand Down
Loading