Skip to content

Commit 142b315

Browse files
committed
#1980 - RepositoryEntityLinks now overrides ….linkForItemResource(…).
1 parent b5e3f73 commit 142b315

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/support/RepositoryEntityLinksIntegrationTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import static org.assertj.core.api.Assertions.*;
1919

2020
import org.junit.Test;
21-
2221
import org.springframework.beans.factory.annotation.Autowired;
2322
import org.springframework.data.domain.PageRequest;
2423
import org.springframework.data.domain.Sort;
@@ -157,4 +156,12 @@ public void addsProjectVariableToSearchResourceIfAvailable() {
157156
assertThat(link.getVariableNames()).contains("projection");
158157
}
159158
}
159+
160+
@Test // #1980
161+
public void considersIdConverterInLinkForItemResource() {
162+
163+
Link link = entityLinks.linkForItemResource(Book.class, 7L).withSelfRel();
164+
165+
assertThat(link.getHref()).endsWith("/books/7-7-7-7-7-7-7");
166+
}
160167
}

spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/RepositoryEntityLinks.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,26 @@ public Link linkToItemResource(Class<?> type, Object id) {
136136
Assert.isInstanceOf(Serializable.class, id, "Id must be assignable to Serializable!");
137137

138138
ResourceMetadata metadata = mappings.getMetadataFor(type);
139+
Link link = linkForItemResource(type, id).withRel(metadata.getItemResourceRel());
140+
141+
return Link.of(UriTemplate.of(link.getHref()).with(getProjectionVariable(type)).toString(),
142+
metadata.getItemResourceRel());
143+
}
144+
145+
/*
146+
* (non-Javadoc)
147+
* @see org.springframework.hateoas.server.core.AbstractEntityLinks#linkForItemResource(java.lang.Class, java.lang.Object)
148+
*/
149+
@Override
150+
public LinkBuilder linkForItemResource(Class<?> type, Object id) {
151+
152+
Assert.isInstanceOf(Serializable.class, id, "Id must be assignable to Serializable!");
153+
139154
String mappedId = idConverters.getPluginFor(type)//
140155
.orElse(DefaultIdConverter.INSTANCE)//
141156
.toRequestId((Serializable) id, type);
142157

143-
Link link = linkFor(type).slash(mappedId).withRel(metadata.getItemResourceRel());
144-
return Link.of(UriTemplate.of(link.getHref()).with(getProjectionVariable(type)).toString(),
145-
metadata.getItemResourceRel());
158+
return linkFor(type).slash(mappedId);
146159
}
147160

148161
/**

0 commit comments

Comments
 (0)