From a8f271a67d693de4a8678c191bbbca4876ad9a5b 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 5f5e4f395..6dff28f3b 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 @@ -112,8 +112,8 @@ public class PersistentEntityJackson2Module extends SimpleModule { LinkCollector collector = new LinkCollector(entities, entityLinks, 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)); @@ -436,16 +436,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. @@ -541,13 +531,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; } /* @@ -579,9 +573,7 @@ public class PersistentEntityJackson2Module extends SimpleModule { */ @Override public JsonSerializer unwrappingSerializer(NameTransformer unwrapper) { - - this.unwrapping = true; - return this; + return new ProjectionResourceContentSerializer(true); } }