DATAREST-200 - Embedded associations get rendered again.

Removed too strong guard that prevented managed but not-exported types to be rendered at all.
This commit is contained in:
Oliver Gierke
2013-12-04 21:41:57 +01:00
parent 73a2fd7430
commit 0667e0bfb2
5 changed files with 83 additions and 7 deletions

View File

@@ -307,13 +307,10 @@ public class PersistentEntityJackson2Module extends SimpleModule implements Init
PersistentProperty<?> property = association.getInverse();
if (!mappings.isMapped(property)) {
return;
}
if (maybeAddAssociationLink(builder, mappings, property, links)) {
return;
}
// Association Link was not added, probably because this isn't a managed type. Add value of property inline.
Object propertyValue = wrapper.getProperty(property);
try {

View File

@@ -104,6 +104,20 @@ public class JpaWebTests extends AbstractWebIntegrationTests {
MockHttpServletResponse orders = request(ordersLink);
Link creatorLink = assertHasContentLinkWithRel("creator", orders);
assertThat(request(creatorLink), is(notNullValue()));
}
/**
* @see DATAREST-200
*/
@Test
public void exposesInlinedEntities() throws Exception {
MockHttpServletResponse response = request("/");
Link ordersLink = assertHasLinkWithRel("orders", response);
MockHttpServletResponse orders = request(ordersLink);
assertHasJsonPathValue("$..lineItems", orders);
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.webmvc.jpa;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* @author Oliver Gierke
*/
@Entity
public class LineItem {
@Id @GeneratedValue//
private Long id;
private String name;
public LineItem(String name) {
this.name = name;
}
protected LineItem() {
}
public String getName() {
return name;
}
}

View File

@@ -15,11 +15,16 @@
*/
package org.springframework.data.rest.webmvc.jpa;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
@@ -29,8 +34,12 @@ import javax.persistence.Table;
@Table(name = "ORDERS")
public class Order {
@Id @GeneratedValue private Long id;
@ManyToOne(fetch = FetchType.LAZY) private Person creator;
@Id @GeneratedValue//
private Long id;
@ManyToOne(fetch = FetchType.LAZY)//
private Person creator;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)//
private Set<LineItem> lineItems = new HashSet<LineItem>();
public Order(Person creator) {
this.creator = creator;
@@ -47,4 +56,15 @@ public class Order {
public Person getCreator() {
return creator;
}
/**
* @return the lineItems
*/
public Set<LineItem> getLineItems() {
return lineItems;
}
public void add(LineItem item) {
this.lineItems.add(item);
}
}

View File

@@ -34,7 +34,9 @@ public class TestDataPopulator {
Person person = people.findAll().iterator().next();
orders.save(new Order(person));
Order order = new Order(person);
order.add(new LineItem("Java Chip"));
orders.save(order);
}
private void populatePeople() {