Skip to content

Commit 3dc325c

Browse files
committed
Minor corrections on the nullability declarations in relation to Spring HATEOAS.
Related ticket #2450.
1 parent 12bdaf9 commit 3dc325c

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

spring-data-rest-core/src/main/java/org/springframework/data/rest/core/support/DefaultSelfLinkProvider.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.List;
1919

2020
import org.jspecify.annotations.Nullable;
21-
2221
import org.springframework.core.convert.ConversionService;
2322
import org.springframework.data.mapping.PersistentEntity;
2423
import org.springframework.data.mapping.PersistentProperty;
@@ -74,7 +73,14 @@ public Link createSelfLinkFor(Object instance) {
7473
public Link createSelfLinkFor(Class<?> type, Object reference) {
7574

7675
if (type.isInstance(reference)) {
77-
return entityLinks.linkToItemResource(type, getResourceId(type, reference));
76+
77+
var identifier = getResourceId(type, reference);
78+
79+
if (identifier == null) {
80+
throw new IllegalArgumentException("Cannot resolve identifier from reference %s".formatted(reference));
81+
}
82+
83+
return entityLinks.linkToItemResource(type, identifier);
7884
}
7985

8086
PersistentEntity<?, ?> entity = entities.getRequiredPersistentEntity(type);
@@ -86,19 +92,25 @@ public Link createSelfLinkFor(Class<?> type, Object reference) {
8692
identifier = getResourceId(type, conversionService.convert(identifier, type));
8793
}
8894

95+
if (identifier == null) {
96+
throw new IllegalArgumentException("Cannot resolve identifier from reference %s".formatted(reference));
97+
}
98+
8999
return entityLinks.linkToItemResource(type, identifier);
90100
}
91101

92102
/**
93103
* Returns the identifier to be used to create the self link URI.
94104
*
95-
* @param reference must not be {@literal null}.
96-
* @return
105+
* @param reference
106+
* @return can be {@literal null}.
97107
*/
98108
@Nullable
99109
private Object getResourceId(Class<?> type, @Nullable Object reference) {
100110

101-
Assert.notNull(reference, "Reference must not be null");
111+
if (reference == null) {
112+
return null;
113+
}
102114

103115
if (!lookups.hasPluginFor(type)) {
104116
return entityIdentifierOrNull(reference);

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@
1515
*/
1616
package org.springframework.data.rest.webmvc.support;
1717

18-
import java.net.URI;
1918
import java.util.Collections;
2019
import java.util.List;
2120

21+
import org.jspecify.annotations.Nullable;
2222
import org.springframework.data.mapping.PersistentProperty;
2323
import org.springframework.data.rest.core.mapping.ResourceMetadata;
2424
import org.springframework.data.rest.webmvc.BaseUri;
2525
import org.springframework.hateoas.Affordance;
2626
import org.springframework.hateoas.Link;
27-
import org.springframework.hateoas.server.LinkBuilder;
2827
import org.springframework.hateoas.server.core.LinkBuilderSupport;
2928
import org.springframework.util.Assert;
3029
import org.springframework.web.util.UriComponents;
@@ -64,7 +63,7 @@ private RepositoryLinkBuilder(ResourceMetadata metadata, UriComponents component
6463
}
6564

6665
@Override
67-
public RepositoryLinkBuilder slash(Object object) {
66+
public RepositoryLinkBuilder slash(@Nullable Object object) {
6867

6968
return PersistentProperty.class.isInstance(object) //
7069
? slash((PersistentProperty<?>) object)

0 commit comments

Comments
 (0)