From e9bc98a1e93d6dc43a977c5502981ac07c7f446e Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Wed, 24 Feb 2021 17:55:23 +0100 Subject: [PATCH] =?UTF-8?q?#1980=20-=20RepositoryEntityLinks=20now=20overr?= =?UTF-8?q?ides=20=E2=80=A6.linkForItemResource(=E2=80=A6).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...RepositoryEntityLinksIntegrationTests.java | 9 ++++++++- .../webmvc/support/RepositoryEntityLinks.java | 19 ++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/support/RepositoryEntityLinksIntegrationTests.java b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/support/RepositoryEntityLinksIntegrationTests.java index 2863ab312..e8fcfbe87 100755 --- a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/support/RepositoryEntityLinksIntegrationTests.java +++ b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/support/RepositoryEntityLinksIntegrationTests.java @@ -18,7 +18,6 @@ package org.springframework.data.rest.webmvc.support; import static org.assertj.core.api.Assertions.*; import org.junit.Test; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; @@ -157,4 +156,12 @@ public class RepositoryEntityLinksIntegrationTests extends AbstractControllerInt assertThat(link.getVariableNames()).contains("projection"); } } + + @Test // #1980 + public void considersIdConverterInLinkForItemResource() { + + Link link = entityLinks.linkForItemResource(Book.class, 7L).withSelfRel(); + + assertThat(link.getHref()).endsWith("/books/7-7-7-7-7-7-7"); + } } diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/RepositoryEntityLinks.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/RepositoryEntityLinks.java index c148f7688..c2f08b523 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/RepositoryEntityLinks.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/RepositoryEntityLinks.java @@ -156,13 +156,26 @@ public class RepositoryEntityLinks extends AbstractEntityLinks { Assert.isInstanceOf(Serializable.class, id, "Id must be assignable to Serializable!"); ResourceMetadata metadata = mappings.getMetadataFor(type); + Link link = linkForItemResource(type, id).withRel(metadata.getItemResourceRel()); + + return Link.of(UriTemplate.of(link.getHref()).with(getProjectionVariable(type)).toString(), + metadata.getItemResourceRel()); + } + + /* + * (non-Javadoc) + * @see org.springframework.hateoas.server.core.AbstractEntityLinks#linkForItemResource(java.lang.Class, java.lang.Object) + */ + @Override + public LinkBuilder linkForItemResource(Class type, Object id) { + + Assert.isInstanceOf(Serializable.class, id, "Id must be assignable to Serializable!"); + String mappedId = idConverters.getPluginFor(type)// .orElse(DefaultIdConverter.INSTANCE)// .toRequestId((Serializable) id, type); - Link link = linkFor(type).slash(mappedId).withRel(metadata.getItemResourceRel()); - return Link.of(UriTemplate.of(link.getHref()).with(getProjectionVariable(type)).toString(), - metadata.getItemResourceRel()); + return linkFor(type).slash(mappedId); } /**