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

$ Conflicts:
$	src/test/java/org/springframework/hateoas/server/mvc/WebMvcLinkBuilderUnitTest.java
This commit is contained in:
Oliver Drotbohm
2021-12-10 10:37:06 +01:00
parent 183d3dc2e6
commit 9266b6accd
2 changed files with 58 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

@@ -42,6 +42,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;
@@ -683,6 +684,19 @@ class WebMvcLinkBuilderUnitTest extends TestUtils {
.forEach(it -> it.withMessageContaining("Expected 1, got 0"));
}
@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();
}
@@ -768,6 +782,12 @@ class WebMvcLinkBuilderUnitTest extends TestUtils {
HttpEntity<Void> methodWithMapRequestParam(@RequestParam Map<String, String> params) {
return null;
}
// #1729
@RequestMapping(method = RequestMethod.POST, path = "/with-request-body")
HttpEntity<Void> methodWithRequestBody(@RequestBody Person param) {
return null;
}
}
@RequestMapping("/parent")