diff --git a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/main/java/org/springframework/data/rest/webmvc/jpa/CreditCard.java b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/main/java/org/springframework/data/rest/webmvc/jpa/CreditCard.java index 6746d63b7..0c9da6e08 100644 --- a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/main/java/org/springframework/data/rest/webmvc/jpa/CreditCard.java +++ b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/main/java/org/springframework/data/rest/webmvc/jpa/CreditCard.java @@ -15,14 +15,34 @@ */ package org.springframework.data.rest.webmvc.jpa; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.RequiredArgsConstructor; + +import javax.persistence.Embeddable; import javax.persistence.Entity; import javax.persistence.Id; +import com.fasterxml.jackson.annotation.JsonUnwrapped; + /** * @author Oliver Gierke */ @Entity +@Getter +@NoArgsConstructor(force = true) +@RequiredArgsConstructor public class CreditCard { @Id Long id; + final @JsonUnwrapped CCN ccn; + + @Embeddable + @Getter + @NoArgsConstructor(force = true) + @AllArgsConstructor + public static class CCN { + String ccn; + } } diff --git a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntitySerializationTests.java b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntitySerializationTests.java index c06fb8d9b..ae2b911fd 100644 --- a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntitySerializationTests.java +++ b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntitySerializationTests.java @@ -36,6 +36,7 @@ import org.springframework.data.projection.ProjectionFactory; import org.springframework.data.projection.SpelAwareProxyProjectionFactory; import org.springframework.data.repository.support.Repositories; import org.springframework.data.rest.webmvc.PersistentEntityResource; +import org.springframework.data.rest.webmvc.jpa.CreditCard; import org.springframework.data.rest.webmvc.jpa.JpaRepositoryConfig; import org.springframework.data.rest.webmvc.jpa.LineItem; import org.springframework.data.rest.webmvc.jpa.Order; @@ -289,4 +290,15 @@ public class PersistentEntitySerializationTests { assertThat(JsonPath.read(result, "$._links.self"), is(notNullValue())); } + + /** + * @see DATAREST-880 + */ + @Test + public void unwrapsNestedTypeCorrectly() throws Exception { + + CreditCard creditCard = new CreditCard(new CreditCard.CCN("1234123412341234")); + + assertThat(JsonPath.read(mapper.writeValueAsString(creditCard), "$.ccn"), is("1234123412341234")); + } } 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 989aaaeb6..e520ae54a 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 @@ -273,7 +273,7 @@ public class PersistentEntityJackson2Module extends SimpleModule { continue; } - if (persistentProperty.isEntity()) { + if (persistentProperty.isEntity() && !writer.isUnwrapping()) { LOG.debug("Assigning nested entity serializer for {}.", persistentProperty);