From 28e0ef76d1a6492d31885d9c150f68abbc6eb7c1 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 13 Apr 2015 12:18:24 +0200 Subject: [PATCH] DATAREST-516 - Fixed link to ALPS representation descriptors of associations. Previously the link pointing to representation descriptors of associations neither pointed to the ALPS controller nor to the representation descriptor. --- ...eInformationToAlpsDescriptorConverter.java | 14 +++++++---- .../alps/AlpsControllerIntegrationTests.java | 23 ++++++++++++++++--- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/alps/RootResourceInformationToAlpsDescriptorConverter.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/alps/RootResourceInformationToAlpsDescriptorConverter.java index 2069c6691..a1ae746ea 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/alps/RootResourceInformationToAlpsDescriptorConverter.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/alps/RootResourceInformationToAlpsDescriptorConverter.java @@ -149,7 +149,7 @@ public class RootResourceInformationToAlpsDescriptorConverter { ResourceMetadata metadata = mappings.getMappingFor(type); return descriptor().// - id(metadata.getItemResourceRel().concat("-representation")).// + id(getRepresentationDescriptorId(metadata)).// href(entityLinks.linkFor(type).slash("schema").toString()).// doc(getDocFor(metadata.getItemResourceDescription())).// descriptors(buildPropertyDescriptors(type, metadata.getItemResourceRel())).// @@ -348,9 +348,11 @@ public class RootResourceInformationToAlpsDescriptorConverter { DescriptorBuilder builder = descriptor().// name(mapping.getRel()).doc(getDocFor(mapping.getDescription())); - ResourceMetadata targetTypeMapping = mappings.getMappingFor(property.getActualType()); - String localPath = targetTypeMapping.getRel().concat("#").concat(targetTypeMapping.getItemResourceRel()); - Link link = ControllerLinkBuilder.linkTo(AlpsController.class).slash(localPath).withSelfRel(); + ResourceMetadata targetTypeMetadata = mappings.getMappingFor(property.getActualType()); + String localPath = targetTypeMetadata.getRel().concat("#") + .concat(getRepresentationDescriptorId(targetTypeMetadata)); + Link link = ControllerLinkBuilder.linkTo(AlpsController.class).slash(AlpsController.ALPS_ROOT_MAPPING) + .slash(localPath).withSelfRel(); builder.// type(Type.SAFE).// @@ -415,6 +417,10 @@ public class RootResourceInformationToAlpsDescriptorConverter { } } + private static String getRepresentationDescriptorId(ResourceMetadata metadata) { + return metadata.getItemResourceRel().concat("-representation"); + } + private static String prefix(HttpMethod method) { switch (method) { diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/alps/AlpsControllerIntegrationTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/alps/AlpsControllerIntegrationTests.java index 427103b16..9d5fd651b 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/alps/AlpsControllerIntegrationTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/alps/AlpsControllerIntegrationTests.java @@ -36,7 +36,6 @@ import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.result.MockMvcResultHandlers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; @@ -138,8 +137,26 @@ public class AlpsControllerIntegrationTests extends AbstractControllerIntegratio assertThat(usersLink, is(notNullValue())); - mvc.perform(get(usersLink.getHref())).andDo(MockMvcResultHandlers.print()) - .andExpect(jsonPath("$.descriptors[?(@.id == 'item-representation')].href", is(notNullValue()))); + mvc.perform(get(usersLink.getHref())).andExpect( + jsonPath("$.descriptors[?(@.id == 'item-representation')].href", is(notNullValue()))); + } + + /** + * @see DATAREST-516 + */ + @Test + public void referenceToAssociatedEntityDesciptorPointsToRepresentationDescriptor() throws Exception { + + Link profileLink = discoverUnique("/", "profile"); + Link usersLink = discoverUnique(profileLink.getHref(), "people"); + + String jsonPath = "$."; // Root + jsonPath += "descriptors[?(@.id == 'person-representation')]."; // Representation descriptor + jsonPath += "descriptors[?(@.name == 'father')][0]."; // First father descriptor + jsonPath += "rt"; // Return type + + mvc.perform(get(usersLink.getHref())).andExpect( + jsonPath(jsonPath, allOf(containsString("alps"), endsWith("-representation")))); } private Link discoverUnique(String href, String rel) throws Exception {