diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntitySerializationTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntitySerializationTests.java index 013f2388b..f4844cf23 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntitySerializationTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntitySerializationTests.java @@ -20,6 +20,7 @@ import static org.hamcrest.Matchers.*; import java.io.IOException; import java.io.StringWriter; +import java.util.Arrays; import java.util.Collections; import org.junit.Before; @@ -29,15 +30,22 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.repository.support.Repositories; import org.springframework.data.rest.webmvc.PersistentEntityResource; +import org.springframework.data.rest.webmvc.jpa.LineItem; import org.springframework.data.rest.webmvc.jpa.Order; +import org.springframework.data.rest.webmvc.jpa.OrderRepository; import org.springframework.data.rest.webmvc.jpa.Person; import org.springframework.data.rest.webmvc.jpa.PersonRepository; +import org.springframework.data.rest.webmvc.mongodb.Address; +import org.springframework.data.rest.webmvc.mongodb.User; import org.springframework.data.rest.webmvc.util.TestUtils; import org.springframework.hateoas.Link; import org.springframework.hateoas.LinkDiscoverer; +import org.springframework.hateoas.PagedResources; +import org.springframework.hateoas.PagedResources.PageMetadata; import org.springframework.hateoas.hal.HalLinkDiscoverer; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.util.ReflectionTestUtils; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.util.UriTemplate; @@ -60,6 +68,7 @@ public class PersistentEntitySerializationTests { @Autowired ObjectMapper mapper; @Autowired Repositories repositories; @Autowired PersonRepository people; + @Autowired OrderRepository orders; LinkDiscoverer linkDiscoverer; @@ -158,4 +167,52 @@ public class PersistentEntitySerializationTests { Order order = mapper.readValue(content, Order.class); assertThat(order.getLineItems(), hasSize(2)); } + + /** + * @see DATAREST-250 + */ + @Test + @SuppressWarnings("unchecked") + public void serializesEmbeddedReferencesCorrectly() throws Exception { + + User user = new User(); + user.address = new Address(); + user.address.street = "Street"; + + PersistentEntityResource userResource = new PersistentEntityResource( + repositories.getPersistentEntity(User.class), user); + + PagedResources> persistentEntityResource = new PagedResources>( + Arrays.asList(userResource), new PageMetadata(1, 0, 10)); + + assertThat( + mapper.writeValueAsString(persistentEntityResource), + is("{\"_embedded\":{\"users\":[{\"address\":{\"street\":\"Street\"}}]},\"page\":{\"size\":1,\"totalElements\":10,\"totalPages\":10,\"number\":0}}")); + } + + /** + * @see DATAREST-250 + */ + @Test + public void serializesReferencesWithinPagedResourceCorrectly() throws Exception { + + Person creator = new Person("Dave", "Matthews"); + + Order order = new Order(creator); + ReflectionTestUtils.setField(order, "id", 1L); + order.add(new LineItem("first")); + order.add(new LineItem("second")); + + PersistentEntityResource orderResource = new PersistentEntityResource( + repositories.getPersistentEntity(Order.class), order); + + @SuppressWarnings("unchecked") + PagedResources> persistentEntityResource = new PagedResources>( + Arrays.asList(orderResource), new PageMetadata(1, 0, 10)); + + assertThat(mapper.writeValueAsString(persistentEntityResource), + is("{\"_embedded\":{\"orders\":[{\"lineItems\":[{\"name\":\"first\"},{\"name\":\"second\"}]" + + ",\"_links\":{\"creator\":{\"href\":\"http://localhost:8080/orders/1/creator\"}}}]},\"" + + "page\":{\"size\":1,\"totalElements\":10,\"totalPages\":10,\"number\":0}}")); + } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/RepositoryTestsConfig.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/RepositoryTestsConfig.java index 5832600aa..63886333f 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/RepositoryTestsConfig.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/RepositoryTestsConfig.java @@ -30,11 +30,13 @@ import org.springframework.data.rest.core.mapping.ResourceMappings; import org.springframework.data.rest.webmvc.jpa.JpaRepositoryConfig; import org.springframework.data.rest.webmvc.jpa.Person; import org.springframework.data.rest.webmvc.jpa.PersonRepository; +import org.springframework.data.rest.webmvc.mongodb.MongoDbRepositoryConfig; import org.springframework.format.support.DefaultFormattingConversionService; import org.springframework.hateoas.RelProvider; import org.springframework.hateoas.core.EvoInflectorRelProvider; import org.springframework.hateoas.hal.Jackson2HalModule; +import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.Module; import com.fasterxml.jackson.databind.ObjectMapper; @@ -45,7 +47,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; * @author Oliver Gierke */ @Configuration -@Import({ JpaRepositoryConfig.class }) +@Import({ JpaRepositoryConfig.class, MongoDbRepositoryConfig.class }) @SuppressWarnings("deprecation") public class RepositoryTestsConfig { @@ -106,6 +108,7 @@ public class RepositoryTestsConfig { mapper.registerModule(persistentEntityModule()); mapper.setHandlerInstantiator(new Jackson2HalModule.HalHandlerInstantiator(relProvider, null)); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + mapper.setSerializationInclusion(Include.NON_EMPTY); return mapper; }