Skip to content

Commit a8f31f5

Browse files
quaffrstoyanchev
authored andcommitted
Improve ProblemDetail equals and hashCode
Lazy computed title property should be taken into account See gh-30294
1 parent 90627b4 commit a8f31f5

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

spring-web/src/main/java/org/springframework/http/ProblemDetail.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
*
4343
* @author Rossen Stoyanchev
4444
* @author Juergen Hoeller
45+
* @author Yanming Zhou
4546
* @since 6.0
4647
* @see <a href="https://datatracker.ietf.org/doc/html/rfc7807">RFC 7807</a>
4748
* @see org.springframework.web.ErrorResponse
@@ -239,7 +240,7 @@ public boolean equals(@Nullable Object other) {
239240
return false;
240241
}
241242
return (this.type.equals(otherDetail.type) &&
242-
ObjectUtils.nullSafeEquals(this.title, otherDetail.title) &&
243+
ObjectUtils.nullSafeEquals(this.getTitle(), otherDetail.getTitle()) &&
243244
this.status == otherDetail.status &&
244245
ObjectUtils.nullSafeEquals(this.detail, otherDetail.detail) &&
245246
ObjectUtils.nullSafeEquals(this.instance, otherDetail.instance) &&
@@ -249,7 +250,7 @@ public boolean equals(@Nullable Object other) {
249250
@Override
250251
public int hashCode() {
251252
int result = this.type.hashCode();
252-
result = 31 * result + ObjectUtils.nullSafeHashCode(this.title);
253+
result = 31 * result + ObjectUtils.nullSafeHashCode(this.getTitle());
253254
result = 31 * result + this.status;
254255
result = 31 * result + ObjectUtils.nullSafeHashCode(this.detail);
255256
result = 31 * result + ObjectUtils.nullSafeHashCode(this.instance);

spring-web/src/test/java/org/springframework/http/ProblemDetailTests.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.http;
1818

19+
import com.fasterxml.jackson.databind.ObjectMapper;
1920
import org.junit.jupiter.api.Test;
2021

2122
import static org.assertj.core.api.Assertions.assertThat;
@@ -24,11 +25,12 @@
2425
* Unit tests for {@link ProblemDetail}.
2526
*
2627
* @author Juergen Hoeller
28+
* @author Yanming Zhou
2729
*/
2830
class ProblemDetailTests {
2931

3032
@Test
31-
void equalsAndHashCode() {
33+
void equalsAndHashCode() throws Exception {
3234
ProblemDetail pd1 = ProblemDetail.forStatus(500);
3335
ProblemDetail pd2 = ProblemDetail.forStatus(HttpStatus.INTERNAL_SERVER_ERROR);
3436
ProblemDetail pd3 = ProblemDetail.forStatus(HttpStatus.NOT_FOUND);
@@ -48,6 +50,12 @@ void equalsAndHashCode() {
4850
assertThat(pd2).isNotEqualTo(pd4);
4951
assertThat(pd1.hashCode()).isNotEqualTo(pd3.hashCode());
5052
assertThat(pd1.hashCode()).isNotEqualTo(pd4.hashCode());
53+
54+
ObjectMapper om = new ObjectMapper();
55+
ProblemDetail pd5 = om.readValue(om.writeValueAsBytes(pd1), ProblemDetail.class);
56+
assertThat(pd1).isEqualTo(pd5);
57+
assertThat(pd5).isEqualTo(pd1);
58+
assertThat(pd1.hashCode()).isEqualTo(pd5.hashCode());
5159
}
5260

5361
}

0 commit comments

Comments
 (0)