Skip to content

Commit 12efa9a

Browse files
committed
Fix potential IllegalStateException in Limit.
Fixes GH-3023.
1 parent 5dd7b32 commit 12efa9a

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

Diff for: src/main/java/org/springframework/data/domain/Limit.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import org.springframework.data.domain.Limit.Limited;
1919
import org.springframework.data.domain.Limit.Unlimited;
20-
import org.springframework.util.ClassUtils;
20+
import org.springframework.lang.Nullable;
2121

2222
/**
2323
* {@link Limit} represents the maximum value up to which an operation should continue processing. It may be used for
@@ -94,14 +94,17 @@ public boolean equals(Object obj) {
9494
if (obj == null) {
9595
return false;
9696
}
97-
if (!ClassUtils.isAssignable(Limit.class, obj.getClass())) {
97+
98+
if (!(obj instanceof Limit that)) {
9899
return false;
99100
}
100-
Limit that = (Limit) obj;
101-
if (this.isUnlimited() && that.isUnlimited()) {
102-
return true;
101+
102+
if (this.isUnlimited() ^ that.isUnlimited()) {
103+
return false;
103104
}
104-
return max() == that.max();
105+
106+
return this.isUnlimited() && that.isUnlimited()
107+
|| max() == that.max();
105108
}
106109

107110
@Override

Diff for: src/test/java/org/springframework/data/domain/LimitUnitTests.java

+11
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,15 @@ void isUnlimitedReturnsFalseIfLimited(int value) {
5555
void unlimitedErrorsOnMax() {
5656
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> Limit.unlimited().max());
5757
}
58+
59+
@Test // GH-3023
60+
void equalsProperly() {
61+
62+
Limit unlimited = Limit.unlimited();
63+
Limit limited = Limit.of(5);
64+
65+
assertThat(limited.equals(unlimited)).isFalse();
66+
assertThat(unlimited.equals(limited)).isFalse();
67+
assertThat(unlimited.equals(unlimited)).isTrue();
68+
}
5869
}

0 commit comments

Comments
 (0)