Skip to content

Commit

Permalink
GH-2987 - Issue a warning log when serializing PageImpl to JSON direc…
Browse files Browse the repository at this point in the history
…tly.

We now issue a one-time warning log if Jackson gets handed a PageImpl to serialize as the Jackson structure might need to change for arbitrary other reasons.
  • Loading branch information
odrotbohm committed Jan 11, 2024
1 parent 69b149e commit 03710fc
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public static class PageModule extends SimpleModule {
}

public PageModule() {

addSerializer(UNPAGED_TYPE, new UnpagedAsInstanceSerializer());
setMixInAnnotation(PageImpl.class, PageImplMixin.class);
}

/**
Expand All @@ -80,5 +82,36 @@ public String valueToString(@Nullable Object value) {
return "INSTANCE";
}
}

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

static class PlainPageSerializationWarning extends StdConverter<Page<?>, Page<?>> {

private static final Logger LOGGER = LoggerFactory.getLogger(PlainPageSerializationWarning.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 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 boolean warningRendered = false;

@Nullable
@Override
public Page<?> convert(@Nullable Page<?> value) {

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

return value;
}
}
}
}

0 comments on commit 03710fc

Please sign in to comment.