Use legacy page serialization mode if no SpringDataWebSettings are present.

Fixes GH-3101.
This commit is contained in:
Oliver Drotbohm
2024-05-30 23:34:43 +02:00
parent 3bc2621e1d
commit d748e866d0
2 changed files with 44 additions and 4 deletions

View File

@@ -17,7 +17,6 @@ package org.springframework.data.web.config;
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 @@ import com.fasterxml.jackson.databind.util.StdConverter;
*/
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 class SpringDataJacksonConfiguration implements SpringDataJacksonModules
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);

View File

@@ -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);
}
}