Skip to content

Commit

Permalink
Polishing contribution
Browse files Browse the repository at this point in the history
Closes gh-30294
  • Loading branch information
rstoyanchev committed Apr 12, 2023
1 parent a8f31f5 commit 5f22648
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
* additional properties. Subclasses can use the protected copy constructor to
* re-create an existing {@code ProblemDetail} instance as the subclass, e.g.
* from an {@code @ControllerAdvice} such as
* {@link org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler}.
* {@link org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler} or
* {@link org.springframework.web.reactive.result.method.annotation.ResponseEntityExceptionHandler}.
*
* @author Rossen Stoyanchev
* @author Juergen Hoeller
* @author Yanming Zhou
* @since 6.0
* @see <a href="https://datatracker.ietf.org/doc/html/rfc7807">RFC 7807</a>
* @see org.springframework.web.ErrorResponse
Expand Down Expand Up @@ -239,8 +239,8 @@ public boolean equals(@Nullable Object other) {
if (!(other instanceof ProblemDetail otherDetail)) {
return false;
}
return (this.type.equals(otherDetail.type) &&
ObjectUtils.nullSafeEquals(this.getTitle(), otherDetail.getTitle()) &&
return (getType().equals(otherDetail.getType()) &&
ObjectUtils.nullSafeEquals(getTitle(), otherDetail.getTitle()) &&
this.status == otherDetail.status &&
ObjectUtils.nullSafeEquals(this.detail, otherDetail.detail) &&
ObjectUtils.nullSafeEquals(this.instance, otherDetail.instance) &&
Expand All @@ -250,7 +250,7 @@ public boolean equals(@Nullable Object other) {
@Override
public int hashCode() {
int result = this.type.hashCode();
result = 31 * result + ObjectUtils.nullSafeHashCode(this.getTitle());
result = 31 * result + ObjectUtils.nullSafeHashCode(getTitle());
result = 31 * result + this.status;
result = 31 * result + ObjectUtils.nullSafeHashCode(this.detail);
result = 31 * result + ObjectUtils.nullSafeHashCode(this.instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ default Object[] getDetailMessageArguments(MessageSource messageSource, Locale l
/**
* Return a code to use to resolve the problem "title" for this exception
* through a {@link MessageSource}.
* <p>By default this is initialized via
* {@link #getDefaultTitleMessageCode(Class, String)}.
* <p>By default this is initialized via {@link #getDefaultTitleMessageCode(Class)}.
*/
default String getTitleMessageCode() {
return getDefaultTitleMessageCode(getClass());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,7 +30,7 @@
class ProblemDetailTests {

@Test
void equalsAndHashCode() throws Exception {
void equalsAndHashCode() {
ProblemDetail pd1 = ProblemDetail.forStatus(500);
ProblemDetail pd2 = ProblemDetail.forStatus(HttpStatus.INTERNAL_SERVER_ERROR);
ProblemDetail pd3 = ProblemDetail.forStatus(HttpStatus.NOT_FOUND);
Expand All @@ -50,12 +50,19 @@ void equalsAndHashCode() throws Exception {
assertThat(pd2).isNotEqualTo(pd4);
assertThat(pd1.hashCode()).isNotEqualTo(pd3.hashCode());
assertThat(pd1.hashCode()).isNotEqualTo(pd4.hashCode());
}

@Test // gh-30294
void equalsAndHashCodeWithDeserialization() throws Exception {
ProblemDetail originalDetail = ProblemDetail.forStatus(500);

ObjectMapper mapper = new ObjectMapper();
byte[] bytes = mapper.writeValueAsBytes(originalDetail);
ProblemDetail deserializedDetail = mapper.readValue(bytes, ProblemDetail.class);

ObjectMapper om = new ObjectMapper();
ProblemDetail pd5 = om.readValue(om.writeValueAsBytes(pd1), ProblemDetail.class);
assertThat(pd1).isEqualTo(pd5);
assertThat(pd5).isEqualTo(pd1);
assertThat(pd1.hashCode()).isEqualTo(pd5.hashCode());
assertThat(originalDetail).isEqualTo(deserializedDetail);
assertThat(deserializedDetail).isEqualTo(originalDetail);
assertThat(originalDetail.hashCode()).isEqualTo(deserializedDetail.hashCode());
}

}

0 comments on commit 5f22648

Please sign in to comment.