Skip to content

Commit 415095b

Browse files
committed
DATAREST-1205 - Use Affordances API
Leverage the Affordances API while making other media types work.
1 parent d9fdaab commit 415095b

File tree

20 files changed

+611
-61
lines changed

20 files changed

+611
-61
lines changed

spring-data-rest-core/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
</parent>
1717

1818
<properties>
19-
<springplugin>1.2.0.RELEASE</springplugin>
19+
<springplugin>2.0.0.BUILD-SNAPSHOT</springplugin>
2020
<evoinflector>1.2.2</evoinflector>
21+
<spring-hateoas>1.0.0.SDR-SNAPSHOT</spring-hateoas>
2122
<java-module-name>spring.data.rest.core</java-module-name>
2223
<project.root>${basedir}/..</project.root>
2324
</properties>

spring-data-rest-core/src/main/java/org/springframework/data/rest/core/util/Java8PluginRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static <T extends Plugin<S>, S> Java8PluginRegistry<T, S> empty() {
4646
}
4747

4848
public Optional<T> getPluginFor(S delimiter) {
49-
return Optional.ofNullable(registry.getPluginFor(delimiter));
49+
return registry.getPluginFor(delimiter);
5050
}
5151

5252
public T getPluginOrDefaultFor(S delimiter, T fallback) {

spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/ResourceTester.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public Link assertHasLinkEndingWith(String rel, String hrefEnd) {
8383

8484
private final Link assertHasLinkMatching(String rel, Matcher<String> hrefMatcher) {
8585

86-
Link link = resource.getLink(rel);
86+
Link link = resource.getRequiredLink(rel);
8787
assertThat("Expected link with rel '" + rel + "' but didn't find it in " + resource.getLinks(), link,
8888
is(notNullValue()));
8989

spring-data-rest-tests/spring-data-rest-tests-jpa/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@
4747
<scope>test</scope>
4848
</dependency>
4949

50+
<dependency>
51+
<groupId>org.springframework</groupId>
52+
<artifactId>spring-web</artifactId>
53+
<version>${spring}</version>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>org.springframework.hateoas</groupId>
58+
<artifactId>spring-hateoas</artifactId>
59+
<version>1.0.0.SDR-SNAPSHOT</version>
60+
<scope>test</scope>
61+
</dependency>
62+
5063
<!-- Jackson Hibernate -->
5164

5265
<dependency>

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

Lines changed: 385 additions & 0 deletions
Large diffs are not rendered by default.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void returnsLinksToSearchResources() {
9999

100100
assertThat(links.hasLink("firstname")).isTrue();
101101

102-
Link firstnameLink = links.getLink("firstname");
102+
Link firstnameLink = links.getLink("firstname").orElse(null);
103103
assertThat(firstnameLink.isTemplated()).isTrue();
104104
assertThat(firstnameLink.getVariableNames()).contains("page", "size");
105105
}

spring-data-rest-tests/spring-data-rest-tests-mongodb/src/test/java/org/springframework/data/rest/webmvc/PersistentEntityResourceAssemblerIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void addsSelfAndSingleResourceLinkToResourceByDefault() throws Exception
6868
Links links = new Links(resource.getLinks());
6969

7070
assertThat(links).hasSize(2);
71-
assertThat(links.getLink("self").getVariables()).isEmpty();
72-
assertThat(links.getLink("user").getVariableNames()).contains("projection");
71+
assertThat(links.getLink("self").orElseThrow(() -> new RuntimeException("Unable to find 'self' link")).getVariables()).isEmpty();
72+
assertThat(links.getLink("user").orElseThrow(() -> new RuntimeException("Unable to find 'user' link")).getVariableNames()).contains("projection");
7373
}
7474
}

spring-data-rest-webmvc/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@
124124
<scope>test</scope>
125125
</dependency>
126126

127+
<dependency>
128+
<groupId>org.springframework.data</groupId>
129+
<artifactId>spring-data-jpa</artifactId>
130+
<version>${springdata.jpa}</version>
131+
<scope>test</scope>
132+
</dependency>
133+
127134
</dependencies>
128135

129136
</project>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected Link resourceLink(RootResourceInformation resourceLink, Resource resou
6363

6464
ResourceMetadata repoMapping = resourceLink.getResourceMetadata();
6565

66-
Link selfLink = resource.getLink("self");
66+
Link selfLink = resource.getRequiredLink(Link.REL_SELF);
6767
String rel = repoMapping.getItemResourceRel();
6868

6969
return new Link(selfLink.getHref(), rel);

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import lombok.NonNull;
1919
import lombok.RequiredArgsConstructor;
2020

21+
import java.util.Collections;
22+
23+
import org.springframework.core.ResolvableType;
2124
import org.springframework.data.mapping.PersistentEntity;
2225
import org.springframework.data.mapping.context.PersistentEntities;
2326
import org.springframework.data.rest.core.support.SelfLinkProvider;
@@ -28,6 +31,7 @@
2831
import org.springframework.hateoas.ResourceAssembler;
2932
import org.springframework.hateoas.core.EmbeddedWrapper;
3033
import org.springframework.hateoas.core.EmbeddedWrappers;
34+
import org.springframework.http.HttpMethod;
3135
import org.springframework.util.Assert;
3236

3337
/**
@@ -72,9 +76,11 @@ private Builder wrap(Object instance, Object source) {
7276
PersistentEntity<?, ?> entity = entities.getRequiredPersistentEntity(source.getClass());
7377

7478
return PersistentEntityResource.build(instance, entity).//
75-
withEmbedded(getEmbeddedResources(source)).//
76-
withLink(getSelfLinkFor(source)).//
77-
withLink(linkProvider.createSelfLinkFor(source));
79+
withEmbedded(getEmbeddedResources(source)).//
80+
withLink(getExpandedSelfLink(source)
81+
.andAffordance(HttpMethod.PUT, source.getClass(), Collections.emptyList(), null)
82+
.andAffordance(HttpMethod.PATCH, source.getClass(), Collections.emptyList(), null)).//
83+
withLink(linkProvider.createSelfLinkFor(source));
7884
}
7985

8086
/**
@@ -94,7 +100,7 @@ private Iterable<EmbeddedWrapper> getEmbeddedResources(Object instance) {
94100
* @param instance must be a managed entity, not {@literal null}.
95101
* @return
96102
*/
97-
public Link getSelfLinkFor(Object instance) {
103+
public Link getExpandedSelfLink(Object instance) {
98104

99105
Link link = linkProvider.createSelfLinkFor(instance);
100106
return new Link(link.expand().getHref(), Link.REL_SELF);

0 commit comments

Comments
 (0)