From e6949f8a46bbf88da6c94b8cefbcb80ce2aed079 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 30 Jun 2014 13:16:36 +0200 Subject: [PATCH] DATAREST-340 - AssociationOmittingSerializerModifier now keeps non-persistent property writers. AssociationOmittingSerializerModifier previously dropped the writer fro a particular property if it couldn't find a PersistentProperty for the bean property. We now keep those writers to make sure that non-persistent, additional fields are written as well. Related issues: https://github.com/spring-projects/spring-boot/issues/1190 --- .../json/PersistentEntityJackson2Module.java | 1 + ...rsistentEntityJackson2ModuleUnitTests.java | 50 ++++++++++++++++--- 2 files changed, 43 insertions(+), 8 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 a235b9ca7..70d0b925c 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 @@ -235,6 +235,7 @@ public class PersistentEntityJackson2Module extends SimpleModule { PersistentProperty persistentProperty = findProperty(writer.getName(), entity, beanDesc); if (persistentProperty == null) { + result.add(writer); continue; } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2ModuleUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2ModuleUnitTests.java index 48d99d229..abdcbd4a4 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2ModuleUnitTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2ModuleUnitTests.java @@ -20,6 +20,7 @@ import static org.junit.Assert.*; import java.util.Arrays; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -32,6 +33,7 @@ import org.springframework.data.rest.webmvc.mapping.AssociationLinks; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; +import com.jayway.jsonpath.JsonPath; /** * Unit tests for {@link PersistentEntityJackson2Module}. @@ -43,27 +45,59 @@ public class PersistentEntityJackson2ModuleUnitTests { @Mock AssociationLinks associationLinks; - /** - * @see DATAREST-328, DATAREST-320 - */ - @Test - public void doesNotDropPropertiesWithCustomizedNames() throws Exception { + ObjectMapper mapper; + + @Before + public void setUp() { MongoMappingContext mappingContext = new MongoMappingContext(); mappingContext.getPersistentEntity(Sample.class); + mappingContext.getPersistentEntity(SampleWithAdditionalGetters.class); + PersistentEntities persistentEntities = new PersistentEntities(Arrays.asList(mappingContext)); SimpleModule module = new SimpleModule(); module.setSerializerModifier(new PersistentEntityJackson2Module.AssociationOmittingSerializerModifier( persistentEntities, associationLinks, new RepositoryRestConfiguration())); - ObjectMapper mapper = new ObjectMapper(); - mapper.registerModule(module); + this.mapper = new ObjectMapper(); + this.mapper.registerModule(module); + } - assertThat(mapper.writeValueAsString(new Sample()), is(notNullValue())); + /** + * @see DATAREST-328, DATAREST-320 + */ + @Test + public void doesNotDropPropertiesWithCustomizedNames() throws Exception { + + Sample sample = new Sample(); + sample.name = "bar"; + + String result = mapper.writeValueAsString(sample); + + assertThat(JsonPath.read(result, "$.foo"), is((Object) "bar")); + } + + /** + * @see DATAREST-340 + */ + @Test + public void rendersAdditionalJsonPropertiesNotBackedByAPersistentField() throws Exception { + + SampleWithAdditionalGetters sample = new SampleWithAdditionalGetters(); + + String result = mapper.writeValueAsString(sample); + assertThat(JsonPath.read(result, "$.number"), is((Object) 5)); } static class Sample { public @JsonProperty("foo") String name; } + + static class SampleWithAdditionalGetters extends Sample { + + public int getNumber() { + return 5; + } + } }