|
46 | 46 | import org.springframework.data.rest.core.support.EntityLookup; |
47 | 47 | import org.springframework.data.rest.core.support.SelfLinkProvider; |
48 | 48 | import org.springframework.data.rest.webmvc.EmbeddedResourcesAssembler; |
| 49 | +import org.springframework.data.rest.webmvc.PersistentEntityResource; |
49 | 50 | import org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module.AssociationOmittingSerializerModifier; |
50 | 51 | import org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module.AssociationUriResolvingDeserializerModifier; |
51 | 52 | import org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module.LookupObjectSerializer; |
52 | 53 | import org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module.NestedEntitySerializer; |
53 | 54 | import org.springframework.data.rest.webmvc.mapping.Associations; |
54 | 55 | import org.springframework.data.rest.webmvc.support.ExcerptProjector; |
| 56 | +import org.springframework.hateoas.EntityModel; |
55 | 57 | import org.springframework.hateoas.UriTemplate; |
56 | 58 | import org.springframework.hateoas.server.EntityLinks; |
57 | 59 | import org.springframework.hateoas.server.mvc.RepresentationModelProcessorInvoker; |
|
62 | 64 | import com.fasterxml.jackson.annotation.JsonProperty; |
63 | 65 | import com.fasterxml.jackson.annotation.JsonTypeInfo; |
64 | 66 | import com.fasterxml.jackson.annotation.JsonValue; |
| 67 | +import com.fasterxml.jackson.core.JsonGenerator; |
65 | 68 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 69 | +import com.fasterxml.jackson.databind.SerializerProvider; |
66 | 70 | import com.fasterxml.jackson.databind.module.SimpleModule; |
| 71 | +import com.fasterxml.jackson.databind.ser.std.StdSerializer; |
67 | 72 | import com.jayway.jsonpath.JsonPath; |
68 | 73 |
|
69 | 74 | /** |
@@ -95,19 +100,21 @@ void setUp() { |
95 | 100 | mappingContext.getPersistentEntity(PersistentEntityJackson2ModuleUnitTests.PetOwner.class); |
96 | 101 | mappingContext.getPersistentEntity(Immutable.class); |
97 | 102 | mappingContext.getPersistentEntity(Wrapper.class); |
| 103 | + mappingContext.getPersistentEntity(Surrounding.class); |
98 | 104 |
|
99 | 105 | this.persistentEntities = new PersistentEntities(Arrays.asList(mappingContext)); |
100 | 106 |
|
101 | 107 | RepresentationModelProcessorInvoker invoker = new RepresentationModelProcessorInvoker(Collections.emptyList()); |
102 | 108 |
|
103 | 109 | NestedEntitySerializer nestedEntitySerializer = new NestedEntitySerializer(persistentEntities, |
104 | 110 | new EmbeddedResourcesAssembler(persistentEntities, associations, mock(ExcerptProjector.class)), invoker); |
105 | | - SimpleModule module = new SimpleModule(); |
106 | 111 |
|
| 112 | + SimpleModule module = new SimpleModule(); |
107 | 113 | module.setSerializerModifier(new AssociationOmittingSerializerModifier(persistentEntities, associations, |
108 | 114 | nestedEntitySerializer, new LookupObjectSerializer(PluginRegistry.of(new HomeLookup())))); |
109 | 115 | module.setDeserializerModifier( |
110 | 116 | new AssociationUriResolvingDeserializerModifier(persistentEntities, associations, converter, factory)); |
| 117 | + module.addSerializer(new CustomTypeSerializer()); |
111 | 118 |
|
112 | 119 | this.mapper = new ObjectMapper(); |
113 | 120 | this.mapper.registerModule(module); |
@@ -213,6 +220,14 @@ void doesNotWrapJsonValueTypesIntoEntityModel() throws Exception { |
213 | 220 | assertThat(mapper.writeValueAsString(wrapper)).isEqualTo("{\"value\":\"sample\"}"); |
214 | 221 | } |
215 | 222 |
|
| 223 | + @Test // GH-2056 |
| 224 | + void doesNotWrapValuesWithoutUnwrappingSerializer() { |
| 225 | + |
| 226 | + EntityModel<Surrounding> model = PersistentEntityResource.of(new Surrounding()); |
| 227 | + |
| 228 | + assertThatNoException().isThrownBy(() -> mapper.writeValueAsString(model)); |
| 229 | + } |
| 230 | + |
216 | 231 | /** |
217 | 232 | * @author Oliver Gierke |
218 | 233 | */ |
@@ -284,4 +299,35 @@ static class Wrapper { |
284 | 299 | static class ValueType { |
285 | 300 | @JsonValue String value; |
286 | 301 | } |
| 302 | + |
| 303 | + // GH-2056 |
| 304 | + |
| 305 | + @Data |
| 306 | + static class Surrounding { |
| 307 | + CustomType custom = new CustomType(); |
| 308 | + } |
| 309 | + |
| 310 | + static class CustomType {} |
| 311 | + |
| 312 | + static class CustomTypeSerializer extends StdSerializer<CustomType> { |
| 313 | + |
| 314 | + private static final long serialVersionUID = -3841651446883968079L; |
| 315 | + |
| 316 | + public CustomTypeSerializer() { |
| 317 | + super(CustomType.class); |
| 318 | + } |
| 319 | + |
| 320 | + /* |
| 321 | + * (non-Javadoc) |
| 322 | + * @see com.fasterxml.jackson.databind.ser.std.StdSerializer#serialize(java.lang.Object, com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.databind.SerializerProvider) |
| 323 | + */ |
| 324 | + @Override |
| 325 | + public void serialize(CustomType value, JsonGenerator gen, SerializerProvider provider) throws IOException { |
| 326 | + |
| 327 | + gen.writeStartObject(); |
| 328 | + gen.writeFieldName("foo"); |
| 329 | + gen.writeString("bar"); |
| 330 | + gen.writeEndObject(); |
| 331 | + } |
| 332 | + } |
287 | 333 | } |
0 commit comments