Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,11 @@ public <R extends RepresentationModel<?>> SlicedModel<R> toModel(Slice<T> slice,
* @param slice must not be {@literal null}.
* @param assembler must not be {@literal null}.
* @param link must not be {@literal null}.
* @return
* @return a {@link SlicedModel} must not be {@literal null}.
*/
public <R extends RepresentationModel<?>> SlicedModel<R> toModel(Slice<T> slice,
RepresentationModelAssembler<T, R> assembler, Link link) {
Assert.notNull(link, "Link must not be null");
return createModel(slice, assembler, link);
}

Expand All @@ -166,15 +167,14 @@ public SlicedModel<?> toEmptyModel(Slice<?> slice, Class<?> type) {
*
* @param slice must not be {@literal null}, content must be empty.
* @param type must not be {@literal null}.
* @param link must not be {@literal null}.
* @return
* @param link can be {@literal null}.
* @return a {@link SlicedModel} must not be {@literal null}.
*/
public SlicedModel<?> toEmptyModel(Slice<?> slice, Class<?> type, @Nullable Link link) {

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

SliceMetadata metadata = asSliceMetadata(slice);

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;
import java.util.Map;

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

@Test // GH-3452
void emptySliceCreatorNullLink() {

var result = assembler.toEmptyModel(EMPTY_SLICE, Person.class, (Link) null);

var content = result.getContent();
assertThat(content).hasSize(1);

var element = content.iterator().next();
assertThat(element).isInstanceOf(EmbeddedWrapper.class);
assertThat(((EmbeddedWrapper) element).getRelTargetType()).isEqualTo(Person.class);
}

@Test // GH-3452
@SuppressWarnings("deprecation")
void emptySliceCreatorRejectsNullOptionalLink() {

assertThatIllegalArgumentException()
.isThrownBy(() -> assembler.toEmptyModel(EMPTY_SLICE, Person.class, (Optional<Link>) null));
}

@Test // GH-3452
void createsSliceRejectsNullLink() {
assertThatIllegalArgumentException().isThrownBy(() -> assembler.toModel(createSlice(1), (Link) null));
}

private static Slice<Person> createSlice(int index) {

Pageable request = PageRequest.of(index, 1);
Expand Down
Loading