Skip to content

Commit

Permalink
Avoid affecting serialization of custom Page implementations in legac…
Browse files Browse the repository at this point in the history
…y mode.

Registering a StdConverter with Jackson to log a warning  about the Page serialization mode causes the target serializer to be only built for Page losing additional properties defined on extensions. We now instead register a no-op BeanSerializerModifier that issues the warning and doesn't affect the serializer selection.

Fixes GH-3137.
  • Loading branch information
odrotbohm committed Aug 15, 2024
1 parent 2cc8d04 commit ecc4005
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.springframework.data.web.config;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -27,8 +29,12 @@
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializerBase;
import com.fasterxml.jackson.databind.util.StdConverter;

Expand Down Expand Up @@ -83,7 +89,8 @@ public PageModule(@Nullable SpringDataWebSettings settings) {
addSerializer(UNPAGED_TYPE, new UnpagedAsInstanceSerializer());

if (settings == null || settings.pageSerializationMode() == PageSerializationMode.DIRECT) {
setMixInAnnotation(PageImpl.class, WarningMixing.class);
setSerializerModifier(new WarningLoggingModifier());

} else {
setMixInAnnotation(PageImpl.class, WrappingMixing.class);
}
Expand All @@ -109,14 +116,6 @@ public String valueToString(@Nullable Object value) {
}
}

/**
* A mixin for PageImpl to register a converter issuing the serialization warning.
*
* @author Oliver Drotbohm
*/
@JsonSerialize(converter = PlainPageSerializationWarning.class)
abstract class WarningMixing {}

@JsonSerialize(converter = PageModelConverter.class)
abstract class WrappingMixing {}

Expand All @@ -129,27 +128,35 @@ public PagedModel<?> convert(@Nullable Page<?> value) {
}
}

static class PlainPageSerializationWarning extends StdConverter<Page<?>, Page<?>> {
/**
* A {@link BeanSerializerModifier} that logs a warning message if an instance of {@link Page} will be rendered.
*
* @author Oliver Drotbohm
*/
static class WarningLoggingModifier extends BeanSerializerModifier {

private static final Logger LOGGER = LoggerFactory.getLogger(PlainPageSerializationWarning.class);
private static final Logger LOGGER = LoggerFactory.getLogger(WarningLoggingModifier.class);
private static final String MESSAGE = """
Serializing PageImpl instances as-is is not supported, meaning that there is no guarantee about the stability of the resulting JSON structure!
For a stable JSON structure, please use Spring Data's PagedModel (globally via @EnableSpringDataWebSupport(pageSerializationMode = VIA_DTO))
or Spring HATEOAS and Spring Data's PagedResourcesAssembler as documented in https://docs.spring.io/spring-data/commons/reference/repositories/core-extensions.html#core.web.pageables.
""";

private static final long serialVersionUID = 954857444010009875L;

private boolean warningRendered = false;

@Nullable
@Override
public Page<?> convert(@Nullable Page<?> value) {
public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc,
List<BeanPropertyWriter> beanProperties) {

if (Page.class.isAssignableFrom(beanDesc.getBeanClass()) && !warningRendered) {

if (!warningRendered) {
this.warningRendered = true;
LOGGER.warn(MESSAGE);
}

return value;
return super.changeProperties(config, beanDesc, beanProperties);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,16 @@ void serializesPageImplAsPagedModel() {
assertJsonRendering(PageSerializationMode.VIA_DTO, "$.content", "$.page");
}

@Test // GH-3137
void serializesCustomPageAsPageImpl() {
assertJsonRendering(PageSerializationMode.DIRECT, new Extension<>("header"), "$.pageable", "$.last", "$.first");
}

private static void assertJsonRendering(PageSerializationMode mode, String... jsonPaths) {
assertJsonRendering(mode, new PageImpl<>(Collections.emptyList()), jsonPaths);
}

private static void assertJsonRendering(PageSerializationMode mode, PageImpl<?> page, String... jsonPaths) {

SpringDataWebSettings settings = new SpringDataWebSettings(mode);

Expand All @@ -54,11 +63,24 @@ private static void assertJsonRendering(PageSerializationMode mode, String... js

assertThatNoException().isThrownBy(() -> {

String result = mapper.writeValueAsString(new PageImpl<>(Collections.emptyList()));
String result = mapper.writeValueAsString(page);

for (String jsonPath : jsonPaths) {
assertThat(JsonPath.<Object> read(result, jsonPath)).isNotNull();
}
});
}

static class Extension<T> extends PageImpl<T> {

private Object header;

public Extension(Object header) {
super(Collections.emptyList());
}

public Object getHeader() {
return header;
}
}
}

This file was deleted.

0 comments on commit ecc4005

Please sign in to comment.