Skip to content

Commit 4b01370

Browse files
committed
UriTemplateRequestEntity overrides equals/hashCode
Closes gh-27531
1 parent b6111d0 commit 4b01370

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

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

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -194,15 +194,15 @@ public boolean equals(@Nullable Object other) {
194194
return false;
195195
}
196196
RequestEntity<?> otherEntity = (RequestEntity<?>) other;
197-
return (ObjectUtils.nullSafeEquals(getMethod(), otherEntity.getMethod()) &&
198-
ObjectUtils.nullSafeEquals(getUrl(), otherEntity.getUrl()));
197+
return (ObjectUtils.nullSafeEquals(this.method, otherEntity.method) &&
198+
ObjectUtils.nullSafeEquals(this.url, otherEntity.url));
199199
}
200200

201201
@Override
202202
public int hashCode() {
203203
int hashCode = super.hashCode();
204204
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.method);
205-
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getUrl());
205+
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.url);
206206
return hashCode;
207207
}
208208

@@ -544,13 +544,13 @@ private static class DefaultBodyBuilder implements BodyBuilder {
544544
private final URI uri;
545545

546546
@Nullable
547-
String uriTemplate;
547+
private final String uriTemplate;
548548

549549
@Nullable
550-
private Object[] uriVarsArray;
550+
private final Object[] uriVarsArray;
551551

552552
@Nullable
553-
Map<String, ?> uriVarsMap;
553+
private final Map<String, ?> uriVarsMap;
554554

555555
DefaultBodyBuilder(HttpMethod method, URI url) {
556556
this.method = method;
@@ -661,7 +661,7 @@ public <T> RequestEntity<T> body(T body, Type type) {
661661
return buildInternal(body, type);
662662
}
663663

664-
private <T> RequestEntity<T> buildInternal(@Nullable T body, @Nullable Type type) {
664+
private <T> RequestEntity<T> buildInternal(@Nullable T body, @Nullable Type type) {
665665
if (this.uri != null) {
666666
return new RequestEntity<>(body, this.headers, this.method, this.uri, type);
667667
}
@@ -716,6 +716,25 @@ public Object[] getVars() {
716716
return this.uriVarsMap;
717717
}
718718

719+
@Override
720+
public boolean equals(@Nullable Object other) {
721+
if (this == other) {
722+
return true;
723+
}
724+
if (!super.equals(other)) {
725+
return false;
726+
}
727+
UriTemplateRequestEntity<?> otherEntity = (UriTemplateRequestEntity<?>) other;
728+
return (ObjectUtils.nullSafeEquals(this.uriTemplate, otherEntity.uriTemplate) &&
729+
ObjectUtils.nullSafeEquals(this.uriVarsArray, otherEntity.uriVarsArray) &&
730+
ObjectUtils.nullSafeEquals(this.uriVarsMap, otherEntity.uriVarsMap));
731+
}
732+
733+
@Override
734+
public int hashCode() {
735+
return (29 * super.hashCode() + ObjectUtils.nullSafeHashCode(this.uriTemplate));
736+
}
737+
719738
@Override
720739
public String toString() {
721740
return format(getMethod(), getUriTemplate(), getBody(), getHeaders());

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -20,6 +20,7 @@
2020
import java.net.URISyntaxException;
2121
import java.nio.charset.StandardCharsets;
2222
import java.util.Arrays;
23+
import java.util.Collections;
2324
import java.util.HashMap;
2425
import java.util.List;
2526
import java.util.Map;
@@ -175,4 +176,37 @@ void types() throws URISyntaxException {
175176
assertThat(entity.getType()).isEqualTo(typeReference.getType());
176177
}
177178

179+
@Test
180+
void equalityWithUrl() {
181+
RequestEntity<Void> requestEntity1 = RequestEntity.method(HttpMethod.GET, "http://test.api/path/").build();
182+
RequestEntity<Void> requestEntity2 = RequestEntity.method(HttpMethod.GET, "http://test.api/path/").build();
183+
RequestEntity<Void> requestEntity3 = RequestEntity.method(HttpMethod.GET, "http://test.api/pathX/").build();
184+
185+
assertThat(requestEntity1).isEqualTo(requestEntity2);
186+
assertThat(requestEntity2).isEqualTo(requestEntity1);
187+
assertThat(requestEntity1).isNotEqualTo(requestEntity3);
188+
assertThat(requestEntity3).isNotEqualTo(requestEntity2);
189+
assertThat(requestEntity1.hashCode()).isEqualTo(requestEntity2.hashCode());
190+
assertThat(requestEntity1.hashCode()).isNotEqualTo(requestEntity3.hashCode());
191+
}
192+
193+
@Test // gh-27531
194+
void equalityWithUriTemplate() {
195+
Map<String, Object> vars = Collections.singletonMap("id", "1");
196+
197+
RequestEntity<Void> requestEntity1 =
198+
RequestEntity.method(HttpMethod.GET, "http://test.api/path/{id}", vars).build();
199+
RequestEntity<Void> requestEntity2 =
200+
RequestEntity.method(HttpMethod.GET, "http://test.api/path/{id}", vars).build();
201+
RequestEntity<Void> requestEntity3 =
202+
RequestEntity.method(HttpMethod.GET, "http://test.api/pathX/{id}", vars).build();
203+
204+
assertThat(requestEntity1).isEqualTo(requestEntity2);
205+
assertThat(requestEntity2).isEqualTo(requestEntity1);
206+
assertThat(requestEntity1).isNotEqualTo(requestEntity3);
207+
assertThat(requestEntity3).isNotEqualTo(requestEntity2);
208+
assertThat(requestEntity1.hashCode()).isEqualTo(requestEntity2.hashCode());
209+
assertThat(requestEntity1.hashCode()).isNotEqualTo(requestEntity3.hashCode());
210+
}
211+
178212
}

0 commit comments

Comments
 (0)