From d748e866d099663d702cdf2868c451a02b2e0cd7 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Thu, 30 May 2024 23:34:43 +0200 Subject: [PATCH] Use legacy page serialization mode if no SpringDataWebSettings are present. Fixes GH-3101. --- .../SpringDataJacksonConfiguration.java | 6 +-- ...ringDataJacksonConfigurationUnitTests.java | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 src/test/java/org/springframework/data/web/config/SpringDataJacksonConfigurationUnitTests.java diff --git a/src/main/java/org/springframework/data/web/config/SpringDataJacksonConfiguration.java b/src/main/java/org/springframework/data/web/config/SpringDataJacksonConfiguration.java index 131357db59..4d87e14c2e 100644 --- a/src/main/java/org/springframework/data/web/config/SpringDataJacksonConfiguration.java +++ b/src/main/java/org/springframework/data/web/config/SpringDataJacksonConfiguration.java @@ -17,7 +17,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.data.domain.Page; @@ -40,8 +39,7 @@ */ public class SpringDataJacksonConfiguration implements SpringDataJacksonModules { - @Nullable - @Autowired(required = false) SpringDataWebSettings settings; + @Nullable @Autowired(required = false) SpringDataWebSettings settings; @Bean public GeoModule jacksonGeoModule() { @@ -84,7 +82,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); diff --git a/src/test/java/org/springframework/data/web/config/SpringDataJacksonConfigurationUnitTests.java b/src/test/java/org/springframework/data/web/config/SpringDataJacksonConfigurationUnitTests.java new file mode 100644 index 0000000000..386c63c888 --- /dev/null +++ b/src/test/java/org/springframework/data/web/config/SpringDataJacksonConfigurationUnitTests.java @@ -0,0 +1,42 @@ +/* + * Copyright 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.web.config; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.web.config.SpringDataJacksonConfiguration.PageModule; +import org.springframework.data.web.config.SpringDataJacksonConfiguration.PageModule.WarningMixing; + +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * Unit tests for {@link SpringDataJacksonConfiguration}. + * + * @author Oliver Drotbohm + */ +class SpringDataJacksonConfigurationUnitTests { + + @Test // GH-3101 + void usesDirectRenderingIfNoSpringDataWebSettingsArePresent() { + + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new PageModule(null)); + + assertThat(mapper.getSerializationConfig().findMixInClassFor(PageImpl.class)).isEqualTo(WarningMixing.class); + } +}