From 893f2c8c41d0c06a70d6562e3fb9f83c9c1ab914 Mon Sep 17 00:00:00 2001 From: Greg Turnquist Date: Fri, 18 Jul 2014 14:12:26 -0500 Subject: [PATCH] @Entity with @Embedded List causes it to not render propertly This test case exposes a glitch in Spring Data REST, probably because it confused when handling more than one item when rendering a link. In the test case, change LineItem inside Order to List to simulate multiple entries, and the the line items themselves won't render the target property, probably because the target property needs to move from top level _links to an embedded link for each line Item --- .../rest/webmvc/jpa/DataRest262Tests.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/DataRest262Tests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/DataRest262Tests.java index cd3a887bc..5c0f13f73 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/DataRest262Tests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/DataRest262Tests.java @@ -24,8 +24,11 @@ import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToOne; +import javax.persistence.OneToOne; import javax.validation.constraints.NotNull; +import java.io.IOException; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -117,11 +120,39 @@ public class DataRest262Tests { String result = mapper.writeValueAsString(resource); + System.out.println(result); + assertThat(JsonPath.read(result, "$_links.self"), is(notNullValue())); assertThat(JsonPath.read(result, "$_links.airport"), is(notNullValue())); assertThat(JsonPath.read(result, "$_links.originOrDestinationAirport"), is(notNullValue())); } + @Test + public void serializeCircularReference() throws IOException, InterruptedException { + + Order order = new Order(); + order.name = "Shopping basket #1"; + order.id = 1L; + //relatedOrder.lineItems = new ArrayList(); + + LineItem lineItem = new LineItem(); + lineItem.description = "iPhone case"; + lineItem.target = order; + + //relatedOrder.lineItems.add(lineItem); + order.lineItem = lineItem; + + JpaPersistentEntity persistentEntity = mappingContext.getPersistentEntity(Order.class); + + Resource resource = PersistentEntityResource.build(order, persistentEntity).// + withLink(new Link("/api/relatedOrder/" + order.id)).// + build(); + + String result = mapper.writeValueAsString(resource); + + System.out.println(result); + } + public interface AircraftMovementRepository extends CrudRepository { } @@ -130,6 +161,10 @@ public class DataRest262Tests { } + public interface OrderRepository extends CrudRepository { + + } + @Entity(name = "aircraftmovement") public static class AircraftMovement { @@ -140,11 +175,29 @@ public class DataRest262Tests { @Embeddable public static class FlightPart { + @ManyToOne Airport airport; } @Entity(name = "airport") public static class Airport { + @Id @GeneratedValue Long id; } + + @Entity(name = "system") + public static class Order { + + @Id @GeneratedValue Long id; + String name; + // Change lineItem to List, and the whole field gets dropped + @Embedded LineItem lineItem; + } + + @Embeddable + public static class LineItem { + + @OneToOne Order target; + String description; + } }