From 2697c6a2fa481f172218bbff603b7385e4bf85b2 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 2 Apr 2015 16:03:32 +0200 Subject: [PATCH] DATAREST-302 - Rendering projections includes/keeps links. We now treat embedded projections as Resource instances and equip them with a self link. This should also allow to register ResourceProcessor instances for projection types to even add projection specific links if necessary. Also removed the explicit removal of the association link in case a projection is rendered for an association. --- .../rest/webmvc/PersistentEntityResource.java | 23 ------------- .../PersistentEntityResourceAssembler.java | 15 ++++++-- .../json/PersistentEntityJackson2Module.java | 4 +-- .../PersistentEntityResourceUnitTests.java | 34 ------------------- .../data/rest/webmvc/jpa/JpaWebTests.java | 5 +-- 5 files changed, 17 insertions(+), 64 deletions(-) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResource.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResource.java index ec173ae08..45569df50 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResource.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResource.java @@ -95,29 +95,6 @@ public class PersistentEntityResource extends Resource { return embeddeds; } - /** - * Returns whether the given {@link Link} shall be rendered for the resource. - * - * @param link must not be {@literal null}. - * @return - */ - public boolean shouldRenderLink(Link link) { - - Assert.notNull(link, "Link must not be null!"); - - if (enforceAssociationLinks) { - return true; - } - - for (EmbeddedWrapper wrapper : embeddeds) { - if (wrapper.hasRel(link.getRel())) { - return false; - } - } - - return true; - } - /** * Creates a new {@link Builder} to create {@link PersistentEntityResource}s eventually. * diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResourceAssembler.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResourceAssembler.java index ccb4aebe1..6795a4c3c 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResourceAssembler.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResourceAssembler.java @@ -32,6 +32,7 @@ import org.springframework.data.rest.webmvc.mapping.AssociationLinks; import org.springframework.data.rest.webmvc.support.Projector; import org.springframework.hateoas.EntityLinks; import org.springframework.hateoas.Link; +import org.springframework.hateoas.Resource; import org.springframework.hateoas.ResourceAssembler; import org.springframework.hateoas.core.EmbeddedWrapper; import org.springframework.hateoas.core.EmbeddedWrappers; @@ -164,14 +165,14 @@ public class PersistentEntityResourceAssembler implements ResourceAssembler getExcerptResource(Object entity) { + return new Resource(projector.projectExcerpt(entity), getSelfLinkFor(entity)); + } } 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 70d0b925c..ea314b45d 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 @@ -165,9 +165,7 @@ public class PersistentEntityJackson2Module extends SimpleModule { resource.getPersistentEntity().doWithAssociations(associationHandler); for (Link link : associationHandler.getLinks()) { - if (resource.shouldRenderLink(link)) { - links.add(link); - } + links.add(link); } Resource resourceToRender = new Resource(resource.getContent(), links) { diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/PersistentEntityResourceUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/PersistentEntityResourceUnitTests.java index c7e13958a..2136b68a5 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/PersistentEntityResourceUnitTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/PersistentEntityResourceUnitTests.java @@ -80,38 +80,4 @@ public class PersistentEntityResourceUnitTests { assertThat(resource.getEmbeddeds(), is(notNullValue())); assertThat(resource.getEmbeddeds(), is(emptyIterable())); } - - /** - * @see DATAREST-317 - */ - @Test - public void doesNotRenderAssociationLinksIfEmbeddedWithRelPresent() { - - PersistentEntityResource resource = PersistentEntityResource.build(payload, entity).// - withEmbedded(resources).build(); - - assertThat(resource.shouldRenderLink(link), is(false)); - } - - /** - * @see DATAREST-317 - */ - @Test - public void rendersAssociationLinksIfEvenIfEmbeddedWithRelPresentButLinksEnforced() { - - PersistentEntityResource resource = PersistentEntityResource.build(payload, entity).// - withEmbedded(resources).renderAllAssociationLinks().build(); - - assertThat(resource.shouldRenderLink(link), is(true)); - } - - /** - * @see DATAREST-317 - */ - @Test - public void rendersAssociationIfNoEmbeddedPresent() { - - PersistentEntityResource resource = PersistentEntityResource.build(payload, entity).build(); - assertThat(resource.shouldRenderLink(link), is(true)); - } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java index 1c6cceb6a..b9ffd6966 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java @@ -535,9 +535,10 @@ public class JpaWebTests extends CommonWebTests { // Has main content assertHasJsonPathValue(firstAuthorPath.concat(".name"), response); - // Embeddes content of related entity but no link to it + // Embeddes content of related entity, self link and keeps relation link assertHasJsonPathValue(firstAuthorPath.concat("._embedded.books[0].title"), response); - assertJsonPathDoesntExist(firstAuthorPath.concat("._links.books"), response); + assertHasJsonPathValue(firstAuthorPath.concat("._embedded.books[0]._links.self"), response); + assertHasJsonPathValue(firstAuthorPath.concat("._links.books"), response); // Access item resource and expect link to related resource present String content = response.getContentAsString();