DATAREST-880 - Prevent nested-entity-as-resource handling for unwrapped properties.

We now exclude properties that are marked as to be unwrapped from getting the nested-entity-as-resource behavour applied as it doesn't make any sense.
This commit is contained in:
Oliver Gierke
2016-08-29 19:26:07 +02:00
parent 72e4ef025d
commit 4cb0632518
3 changed files with 33 additions and 1 deletions

View File

@@ -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;
}
}

View File

@@ -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"));
}
}

View File

@@ -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);