Skip to content

Commit e9bc98a

Browse files
committed
#1980 - RepositoryEntityLinks now overrides ….linkForItemResource(…).
1 parent cb75eae commit e9bc98a

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

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

+8-1
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
}

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

+16-3
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,26 @@ public Link linkToItemResource(Class<?> type, Object id) {
156156
Assert.isInstanceOf(Serializable.class, id, "Id must be assignable to Serializable!");
157157

158158
ResourceMetadata metadata = mappings.getMetadataFor(type);
159+
Link link = linkForItemResource(type, id).withRel(metadata.getItemResourceRel());
160+
161+
return Link.of(UriTemplate.of(link.getHref()).with(getProjectionVariable(type)).toString(),
162+
metadata.getItemResourceRel());
163+
}
164+
165+
/*
166+
* (non-Javadoc)
167+
* @see org.springframework.hateoas.server.core.AbstractEntityLinks#linkForItemResource(java.lang.Class, java.lang.Object)
168+
*/
169+
@Override
170+
public LinkBuilder linkForItemResource(Class<?> type, Object id) {
171+
172+
Assert.isInstanceOf(Serializable.class, id, "Id must be assignable to Serializable!");
173+
159174
String mappedId = idConverters.getPluginFor(type)//
160175
.orElse(DefaultIdConverter.INSTANCE)//
161176
.toRequestId((Serializable) id, type);
162177

163-
Link link = linkFor(type).slash(mappedId).withRel(metadata.getItemResourceRel());
164-
return Link.of(UriTemplate.of(link.getHref()).with(getProjectionVariable(type)).toString(),
165-
metadata.getItemResourceRel());
178+
return linkFor(type).slash(mappedId);
166179
}
167180

168181
/**

0 commit comments

Comments
 (0)