From 88c97a600dd415438585e62e8659f2f44ec9b871 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 7 Jan 2016 21:37:43 +0100 Subject: [PATCH] DATAREST-743 - Made ProjectionResourceContentSerializer immutable. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ProjectionResourceContentSerializer.unwrappingSerializer(…) now returns a new unwrapping instance instead of mutating the current instance to prevent the source one from answering subsequent calls to isUnwrappingSerializer() with true. This allows the source instance to be reused and produce reliable results on multiple serialization attempts. Related tickets: DATAREST-716. --- .../json/PersistentEntityJackson2Module.java | 26 +++++++------------ 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java index 7f9de9648..9873e4b15 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java @@ -113,8 +113,8 @@ public class PersistentEntityJackson2Module extends SimpleModule { LinkCollector collector = new LinkCollector(entities, linkProvider, associationLinks); addSerializer(new PersistentEntityResourceSerializer(collector)); - addSerializer(new ProjectionSerializer(collector, mappings)); - addSerializer(new ProjectionResourceContentSerializer()); + addSerializer(new ProjectionSerializer(collector, mappings, false)); + addSerializer(new ProjectionResourceContentSerializer(false)); setSerializerModifier(new AssociationOmittingSerializerModifier(entities, associationLinks, config)); setDeserializerModifier(new AssociationUriResolvingDeserializerModifier(entities, converter, associationLinks)); @@ -437,16 +437,6 @@ public class PersistentEntityJackson2Module extends SimpleModule { private final ResourceMappings mappings; private final boolean unwrapping; - /** - * Creates a new {@link ProjectionSerializer} for the given {@link LinkCollector} and {@link ResourceMappings}. - * - * @param collector must not be {@literal null}. - * @param mappings must not be {@literal null}. - */ - public ProjectionSerializer(LinkCollector collector, ResourceMappings mappings) { - this(collector, mappings, false); - } - /** * Creates a new {@link ProjectionSerializer} for the given {@link LinkCollector}, {@link ResourceMappings} whether * to be in unwrapping mode or not. @@ -542,13 +532,17 @@ public class PersistentEntityJackson2Module extends SimpleModule { @SuppressWarnings("serial") private static class ProjectionResourceContentSerializer extends StdSerializer { - private boolean unwrapping; + private final boolean unwrapping; /** * Creates a new {@link ProjectionResourceContentSerializer}. + * + * @param unwrapping whether to expose the unwrapping state. */ - public ProjectionResourceContentSerializer() { + public ProjectionResourceContentSerializer(boolean unwrapping) { + super(ProjectionResourceContent.class); + this.unwrapping = unwrapping; } /* @@ -580,9 +574,7 @@ public class PersistentEntityJackson2Module extends SimpleModule { */ @Override public JsonSerializer unwrappingSerializer(NameTransformer unwrapper) { - - this.unwrapping = true; - return this; + return new ProjectionResourceContentSerializer(true); } }