Skip to content

Commit 6ff4faf

Browse files
authored
Fix: Return empty list when range exceeds collection size (#752)
1 parent 069bd75 commit 6ff4faf

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

core/src/main/java/org/kohsuke/stapler/export/Range.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Arrays;
44
import java.util.Collection;
5+
import java.util.Collections;
56
import java.util.Iterator;
67
import java.util.List;
78

@@ -28,6 +29,9 @@ public <T> List<T> apply(List<T> s) {
2829
s = s.subList(0, max);
2930
}
3031
if (min > 0) {
32+
if (min >= s.size()) {
33+
return Collections.emptyList();
34+
}
3135
s = s.subList(min, s.size());
3236
}
3337
return s;

core/src/test/java/org/kohsuke/stapler/export/RangeTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import static org.hamcrest.MatcherAssert.assertThat;
44
import static org.hamcrest.Matchers.contains;
5+
import static org.hamcrest.Matchers.empty;
6+
import static org.hamcrest.Matchers.emptyIterable;
57

68
import java.util.Arrays;
79
import java.util.LinkedHashSet;
@@ -38,4 +40,20 @@ void minOnlyRange() {
3840
assertThat(r.apply(list), contains("e", "f"));
3941
assertThat(r.apply(set), contains("e", "f"));
4042
}
43+
44+
@Test
45+
void rangeBeyondListSizeReturnsEmpty() {
46+
Range r = new Range(10, 20);
47+
assertThat(r.apply(array), empty());
48+
assertThat(r.apply(list), empty());
49+
assertThat(r.apply(set), emptyIterable());
50+
}
51+
52+
@Test
53+
void rangeExactlyAtBoundaryReturnsEmpty() {
54+
Range r = new Range(6, 10);
55+
assertThat(r.apply(array), empty());
56+
assertThat(r.apply(list), empty());
57+
assertThat(r.apply(set), emptyIterable());
58+
}
4159
}

0 commit comments

Comments
 (0)