From 47d369608b6d072478c7f1aabe01d2ea141e424c 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. --- ...ceInformationToAlpsDescriptorConverter.java | 14 ++++++++++---- .../alps/AlpsControllerIntegrationTests.java | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 4 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 43de05ab1..41cd6d432 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 @@ -148,7 +148,7 @@ public class RootResourceInformationToAlpsDescriptorConverter { ResourceMetadata metadata = mappings.getMetadataFor(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())).// @@ -347,9 +347,11 @@ public class RootResourceInformationToAlpsDescriptorConverter { DescriptorBuilder builder = descriptor().// name(mapping.getRel()).doc(getDocFor(mapping.getDescription())); - ResourceMetadata targetTypeMapping = mappings.getMetadataFor(property.getActualType()); - String localPath = targetTypeMapping.getRel().concat("#").concat(targetTypeMapping.getItemResourceRel()); - Link link = ControllerLinkBuilder.linkTo(AlpsController.class).slash(localPath).withSelfRel(); + ResourceMetadata targetTypeMetadata = mappings.getMetadataFor(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).// @@ -414,6 +416,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 3a453666b..10f5ff5d4 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 @@ -141,6 +141,24 @@ public class AlpsControllerIntegrationTests extends AbstractControllerIntegratio 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 { MockHttpServletResponse response = mvc.perform(get(href)).//