DATAREST-697 - Fixed unwrapping mode in ProjectionSerializer.

Projection serializer now only creates a nested JSON document if it's not in unwrapping mode. Switched to a completely immutable implementation as we were previously mutating state in the request for an unwrapping instance as we were assuming the instances not being reused.
This commit is contained in:
Oliver Gierke
2015-12-10 13:20:47 +01:00
parent 959ddf8e15
commit 6709d1fd92
2 changed files with 43 additions and 7 deletions

View File

@@ -435,19 +435,33 @@ public class PersistentEntityJackson2Module extends SimpleModule {
private final LinkCollector collector;
private final ResourceMappings mappings;
private boolean unwrapping;
private final boolean unwrapping;
/**
* Creates a new {@link ProjectionSerializer} for the given {@link LinkCollector}.
* Creates a new {@link ProjectionSerializer} for the given {@link LinkCollector} and {@link ResourceMappings}.
*
* @param collector
* @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.
*
* @param collector must not be {@literal null}.
* @param mappings must not be {@literal null}.
* @param unwrapping
*/
private ProjectionSerializer(LinkCollector collector, ResourceMappings mappings, boolean unwrapping) {
super(TargetAware.class);
this.collector = collector;
this.mappings = mappings;
this.unwrapping = unwrapping;
}
/*
@@ -462,14 +476,18 @@ public class PersistentEntityJackson2Module extends SimpleModule {
Links links = mappings.getMetadataFor(value.getTargetClass()).isExported() ? collector.getLinksFor(target)
: new Links();
jgen.writeStartObject();
if (!unwrapping) {
jgen.writeStartObject();
}
provider.//
findValueSerializer(ProjectionResource.class, null).//
unwrappingSerializer(null).//
serialize(new ProjectionResource(value, links), jgen, provider);
jgen.writeEndObject();
if (!unwrapping) {
jgen.writeEndObject();
}
}
/*
@@ -487,8 +505,7 @@ public class PersistentEntityJackson2Module extends SimpleModule {
*/
@Override
public JsonSerializer<TargetAware> unwrappingSerializer(NameTransformer unwrapper) {
this.unwrapping = true;
return this;
return new ProjectionSerializer(collector, mappings, true);
}
}