Skip to content

Commit 5f22648

Browse files
committed
Polishing contribution
Closes gh-30294
1 parent a8f31f5 commit 5f22648

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@
3838
* additional properties. Subclasses can use the protected copy constructor to
3939
* re-create an existing {@code ProblemDetail} instance as the subclass, e.g.
4040
* from an {@code @ControllerAdvice} such as
41-
* {@link org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler}.
41+
* {@link org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler} or
42+
* {@link org.springframework.web.reactive.result.method.annotation.ResponseEntityExceptionHandler}.
4243
*
4344
* @author Rossen Stoyanchev
4445
* @author Juergen Hoeller
45-
* @author Yanming Zhou
4646
* @since 6.0
4747
* @see <a href="https://datatracker.ietf.org/doc/html/rfc7807">RFC 7807</a>
4848
* @see org.springframework.web.ErrorResponse
@@ -239,8 +239,8 @@ public boolean equals(@Nullable Object other) {
239239
if (!(other instanceof ProblemDetail otherDetail)) {
240240
return false;
241241
}
242-
return (this.type.equals(otherDetail.type) &&
243-
ObjectUtils.nullSafeEquals(this.getTitle(), otherDetail.getTitle()) &&
242+
return (getType().equals(otherDetail.getType()) &&
243+
ObjectUtils.nullSafeEquals(getTitle(), otherDetail.getTitle()) &&
244244
this.status == otherDetail.status &&
245245
ObjectUtils.nullSafeEquals(this.detail, otherDetail.detail) &&
246246
ObjectUtils.nullSafeEquals(this.instance, otherDetail.instance) &&
@@ -250,7 +250,7 @@ public boolean equals(@Nullable Object other) {
250250
@Override
251251
public int hashCode() {
252252
int result = this.type.hashCode();
253-
result = 31 * result + ObjectUtils.nullSafeHashCode(this.getTitle());
253+
result = 31 * result + ObjectUtils.nullSafeHashCode(getTitle());
254254
result = 31 * result + this.status;
255255
result = 31 * result + ObjectUtils.nullSafeHashCode(this.detail);
256256
result = 31 * result + ObjectUtils.nullSafeHashCode(this.instance);

spring-web/src/main/java/org/springframework/web/ErrorResponse.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ default Object[] getDetailMessageArguments(MessageSource messageSource, Locale l
102102
/**
103103
* Return a code to use to resolve the problem "title" for this exception
104104
* through a {@link MessageSource}.
105-
* <p>By default this is initialized via
106-
* {@link #getDefaultTitleMessageCode(Class, String)}.
105+
* <p>By default this is initialized via {@link #getDefaultTitleMessageCode(Class)}.
107106
*/
108107
default String getTitleMessageCode() {
109108
return getDefaultTitleMessageCode(getClass());

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,7 +30,7 @@
3030
class ProblemDetailTests {
3131

3232
@Test
33-
void equalsAndHashCode() throws Exception {
33+
void equalsAndHashCode() {
3434
ProblemDetail pd1 = ProblemDetail.forStatus(500);
3535
ProblemDetail pd2 = ProblemDetail.forStatus(HttpStatus.INTERNAL_SERVER_ERROR);
3636
ProblemDetail pd3 = ProblemDetail.forStatus(HttpStatus.NOT_FOUND);
@@ -50,12 +50,19 @@ void equalsAndHashCode() throws Exception {
5050
assertThat(pd2).isNotEqualTo(pd4);
5151
assertThat(pd1.hashCode()).isNotEqualTo(pd3.hashCode());
5252
assertThat(pd1.hashCode()).isNotEqualTo(pd4.hashCode());
53+
}
54+
55+
@Test // gh-30294
56+
void equalsAndHashCodeWithDeserialization() throws Exception {
57+
ProblemDetail originalDetail = ProblemDetail.forStatus(500);
58+
59+
ObjectMapper mapper = new ObjectMapper();
60+
byte[] bytes = mapper.writeValueAsBytes(originalDetail);
61+
ProblemDetail deserializedDetail = mapper.readValue(bytes, ProblemDetail.class);
5362

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());
63+
assertThat(originalDetail).isEqualTo(deserializedDetail);
64+
assertThat(deserializedDetail).isEqualTo(originalDetail);
65+
assertThat(originalDetail.hashCode()).isEqualTo(deserializedDetail.hashCode());
5966
}
6067

6168
}

0 commit comments

Comments
 (0)