Skip to content

Commit 1a0a882

Browse files
ngocnhan-tran1996mp911de
authored andcommitted
Remove Assert#notNull for nullable Link.
Signed-off-by: Tran Ngoc Nhan <[email protected]> Closes #3452 Original pull request: #3454
1 parent 198e942 commit 1a0a882

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/main/java/org/springframework/data/web/SlicedResourcesAssembler.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,11 @@ public <R extends RepresentationModel<?>> SlicedModel<R> toModel(Slice<T> slice,
143143
* @param slice must not be {@literal null}.
144144
* @param assembler must not be {@literal null}.
145145
* @param link must not be {@literal null}.
146-
* @return
146+
* @return a {@link SlicedModel} must not be {@literal null}.
147147
*/
148148
public <R extends RepresentationModel<?>> SlicedModel<R> toModel(Slice<T> slice,
149149
RepresentationModelAssembler<T, R> assembler, Link link) {
150+
Assert.notNull(link, "Link must not be null");
150151
return createModel(slice, assembler, link);
151152
}
152153

@@ -166,15 +167,14 @@ public SlicedModel<?> toEmptyModel(Slice<?> slice, Class<?> type) {
166167
*
167168
* @param slice must not be {@literal null}, content must be empty.
168169
* @param type must not be {@literal null}.
169-
* @param link must not be {@literal null}.
170-
* @return
170+
* @param link can be {@literal null}.
171+
* @return a {@link SlicedModel} must not be {@literal null}.
171172
*/
172173
public SlicedModel<?> toEmptyModel(Slice<?> slice, Class<?> type, @Nullable Link link) {
173174

174175
Assert.notNull(slice, "Slice must not be null");
175176
Assert.isTrue(!slice.hasContent(), "Slice must not have any content");
176177
Assert.notNull(type, "Type must not be null");
177-
Assert.notNull(link, "Link must not be null");
178178

179179
SliceMetadata metadata = asSliceMetadata(slice);
180180

@@ -187,6 +187,7 @@ public SlicedModel<?> toEmptyModel(Slice<?> slice, Class<?> type, @Nullable Link
187187

188188
@Deprecated
189189
public SlicedModel<?> toEmptyModel(Slice<?> slice, Class<?> type, Optional<Link> link) {
190+
Assert.notNull(link, "Link must not be null");
190191
return toEmptyModel(slice, type, link.orElse(null));
191192
}
192193

src/test/java/org/springframework/data/web/SlicedResourcesAssemblerUnitTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.List;
2424
import java.util.Map;
2525

26+
import java.util.Optional;
2627
import org.junit.jupiter.api.BeforeEach;
2728
import org.junit.jupiter.api.Test;
2829
import org.springframework.data.domain.AbstractPageRequest;
@@ -259,6 +260,32 @@ void keepsRequestParametersOfOriginalRequestUri() {
259260
.isEqualTo("http://localhost/sample?foo=bar&page=0&size=1");
260261
}
261262

263+
@Test // GH-3452
264+
void emptySliceCreatorNullLink() {
265+
266+
var result = assembler.toEmptyModel(EMPTY_SLICE, Person.class, (Link) null);
267+
268+
var content = result.getContent();
269+
assertThat(content).hasSize(1);
270+
271+
var element = content.iterator().next();
272+
assertThat(element).isInstanceOf(EmbeddedWrapper.class);
273+
assertThat(((EmbeddedWrapper) element).getRelTargetType()).isEqualTo(Person.class);
274+
}
275+
276+
@Test // GH-3452
277+
@SuppressWarnings("deprecation")
278+
void emptySliceCreatorRejectsNullOptionalLink() {
279+
280+
assertThatIllegalArgumentException()
281+
.isThrownBy(() -> assembler.toEmptyModel(EMPTY_SLICE, Person.class, (Optional<Link>) null));
282+
}
283+
284+
@Test // GH-3452
285+
void createsSliceRejectsNullLink() {
286+
assertThatIllegalArgumentException().isThrownBy(() -> assembler.toModel(createSlice(1), (Link) null));
287+
}
288+
262289
private static Slice<Person> createSlice(int index) {
263290

264291
Pageable request = PageRequest.of(index, 1);

0 commit comments

Comments
 (0)