#1729 - Added equals(…) and hashCode() methods to TypeBasedPayloadMetadata.

This commit is contained in:
Oliver Drotbohm
2021-12-10 10:37:06 +01:00
parent 313c9d3c9b
commit e65f296f6a
2 changed files with 59 additions and 0 deletions

View File

@@ -115,4 +115,42 @@ class TypeBasedPayloadMetadata implements InputPayloadMetadata {
public List<MediaType> getMediaTypes() {
return mediaTypes;
}
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof TypeBasedPayloadMetadata)) {
return false;
}
TypeBasedPayloadMetadata that = (TypeBasedPayloadMetadata) obj;
return this.type.equals(that.type)
&& this.properties.equals(that.properties)
&& this.mediaTypes.equals(that.mediaTypes);
}
/*
* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
int result = 31;
result += 17 * type.hashCode();
result += 17 * properties.hashCode();
result += 17 * mediaTypes.hashCode();
return result;
}
}

View File

@@ -53,6 +53,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
@@ -693,6 +694,19 @@ class WebMvcLinkBuilderUnitTest extends TestUtils {
).forEach(assertThatNoException()::isThrownBy);
}
@Test // #1729
@SuppressWarnings("null")
void linksPointingToTheSameMethodAreEqual() {
Link first = linkTo(methodOn(ControllerWithMethods.class).methodWithRequestBody(null)).withSelfRel();
Link second = linkTo(methodOn(ControllerWithMethods.class).methodWithRequestBody(null)).withSelfRel();
assertThat(first).isEqualTo(second);
assertThat(second).isEqualTo(first);
assertThat(first.hashCode()).isEqualTo(second.hashCode());
assertThat(second.hashCode()).isEqualTo(first.hashCode());
}
private static UriComponents toComponents(Link link) {
return UriComponentsBuilder.fromUriString(link.expand().getHref()).build();
}
@@ -798,6 +812,12 @@ class WebMvcLinkBuilderUnitTest extends TestUtils {
HttpEntity<Void> methodWithCustomEnum(@RequestParam List<Sample> param) {
return null;
}
// #1729
@RequestMapping(method = RequestMethod.POST, path = "/with-request-body")
HttpEntity<Void> methodWithRequestBody(@RequestBody Person param) {
return null;
}
}
@RequestMapping("/parent")
@@ -878,4 +898,5 @@ class WebMvcLinkBuilderUnitTest extends TestUtils {
}
}
}
}