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
This commit is contained in:
Oliver Gierke
2014-06-30 13:16:36 +02:00
parent 71b8b275d1
commit e6949f8a46
2 changed files with 43 additions and 8 deletions

View File

@@ -235,6 +235,7 @@ public class PersistentEntityJackson2Module extends SimpleModule {
PersistentProperty<?> persistentProperty = findProperty(writer.getName(), entity, beanDesc); PersistentProperty<?> persistentProperty = findProperty(writer.getName(), entity, beanDesc);
if (persistentProperty == null) { if (persistentProperty == null) {
result.add(writer);
continue; continue;
} }

View File

@@ -20,6 +20,7 @@ import static org.junit.Assert.*;
import java.util.Arrays; import java.util.Arrays;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; 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.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.module.SimpleModule;
import com.jayway.jsonpath.JsonPath;
/** /**
* Unit tests for {@link PersistentEntityJackson2Module}. * Unit tests for {@link PersistentEntityJackson2Module}.
@@ -43,27 +45,59 @@ public class PersistentEntityJackson2ModuleUnitTests {
@Mock AssociationLinks associationLinks; @Mock AssociationLinks associationLinks;
/** ObjectMapper mapper;
* @see DATAREST-328, DATAREST-320
*/ @Before
@Test public void setUp() {
public void doesNotDropPropertiesWithCustomizedNames() throws Exception {
MongoMappingContext mappingContext = new MongoMappingContext(); MongoMappingContext mappingContext = new MongoMappingContext();
mappingContext.getPersistentEntity(Sample.class); mappingContext.getPersistentEntity(Sample.class);
mappingContext.getPersistentEntity(SampleWithAdditionalGetters.class);
PersistentEntities persistentEntities = new PersistentEntities(Arrays.asList(mappingContext)); PersistentEntities persistentEntities = new PersistentEntities(Arrays.asList(mappingContext));
SimpleModule module = new SimpleModule(); SimpleModule module = new SimpleModule();
module.setSerializerModifier(new PersistentEntityJackson2Module.AssociationOmittingSerializerModifier( module.setSerializerModifier(new PersistentEntityJackson2Module.AssociationOmittingSerializerModifier(
persistentEntities, associationLinks, new RepositoryRestConfiguration())); persistentEntities, associationLinks, new RepositoryRestConfiguration()));
ObjectMapper mapper = new ObjectMapper(); this.mapper = new ObjectMapper();
mapper.registerModule(module); 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 { static class Sample {
public @JsonProperty("foo") String name; public @JsonProperty("foo") String name;
} }
static class SampleWithAdditionalGetters extends Sample {
public int getNumber() {
return 5;
}
}
} }