Skip to content

Commit

Permalink
UriTemplateRequestEntity overrides equals/hashCode
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Oct 8, 2021
1 parent b6111d0 commit 4b01370
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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 @@ -194,15 +194,15 @@ public boolean equals(@Nullable Object other) {
return false;
}
RequestEntity<?> otherEntity = (RequestEntity<?>) other;
return (ObjectUtils.nullSafeEquals(getMethod(), otherEntity.getMethod()) &&
ObjectUtils.nullSafeEquals(getUrl(), otherEntity.getUrl()));
return (ObjectUtils.nullSafeEquals(this.method, otherEntity.method) &&
ObjectUtils.nullSafeEquals(this.url, otherEntity.url));
}

@Override
public int hashCode() {
int hashCode = super.hashCode();
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.method);
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getUrl());
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.url);
return hashCode;
}

Expand Down Expand Up @@ -544,13 +544,13 @@ private static class DefaultBodyBuilder implements BodyBuilder {
private final URI uri;

@Nullable
String uriTemplate;
private final String uriTemplate;

@Nullable
private Object[] uriVarsArray;
private final Object[] uriVarsArray;

@Nullable
Map<String, ?> uriVarsMap;
private final Map<String, ?> uriVarsMap;

DefaultBodyBuilder(HttpMethod method, URI url) {
this.method = method;
Expand Down Expand Up @@ -661,7 +661,7 @@ public <T> RequestEntity<T> body(T body, Type type) {
return buildInternal(body, type);
}

private <T> RequestEntity<T> buildInternal(@Nullable T body, @Nullable Type type) {
private <T> RequestEntity<T> buildInternal(@Nullable T body, @Nullable Type type) {
if (this.uri != null) {
return new RequestEntity<>(body, this.headers, this.method, this.uri, type);
}
Expand Down Expand Up @@ -716,6 +716,25 @@ public Object[] getVars() {
return this.uriVarsMap;
}

@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!super.equals(other)) {
return false;
}
UriTemplateRequestEntity<?> otherEntity = (UriTemplateRequestEntity<?>) other;
return (ObjectUtils.nullSafeEquals(this.uriTemplate, otherEntity.uriTemplate) &&
ObjectUtils.nullSafeEquals(this.uriVarsArray, otherEntity.uriVarsArray) &&
ObjectUtils.nullSafeEquals(this.uriVarsMap, otherEntity.uriVarsMap));
}

@Override
public int hashCode() {
return (29 * super.hashCode() + ObjectUtils.nullSafeHashCode(this.uriTemplate));
}

@Override
public String toString() {
return format(getMethod(), getUriTemplate(), getBody(), getHeaders());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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 All @@ -20,6 +20,7 @@
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -175,4 +176,37 @@ void types() throws URISyntaxException {
assertThat(entity.getType()).isEqualTo(typeReference.getType());
}

@Test
void equalityWithUrl() {
RequestEntity<Void> requestEntity1 = RequestEntity.method(HttpMethod.GET, "http://test.api/path/").build();
RequestEntity<Void> requestEntity2 = RequestEntity.method(HttpMethod.GET, "http://test.api/path/").build();
RequestEntity<Void> requestEntity3 = RequestEntity.method(HttpMethod.GET, "http://test.api/pathX/").build();

assertThat(requestEntity1).isEqualTo(requestEntity2);
assertThat(requestEntity2).isEqualTo(requestEntity1);
assertThat(requestEntity1).isNotEqualTo(requestEntity3);
assertThat(requestEntity3).isNotEqualTo(requestEntity2);
assertThat(requestEntity1.hashCode()).isEqualTo(requestEntity2.hashCode());
assertThat(requestEntity1.hashCode()).isNotEqualTo(requestEntity3.hashCode());
}

@Test // gh-27531
void equalityWithUriTemplate() {
Map<String, Object> vars = Collections.singletonMap("id", "1");

RequestEntity<Void> requestEntity1 =
RequestEntity.method(HttpMethod.GET, "http://test.api/path/{id}", vars).build();
RequestEntity<Void> requestEntity2 =
RequestEntity.method(HttpMethod.GET, "http://test.api/path/{id}", vars).build();
RequestEntity<Void> requestEntity3 =
RequestEntity.method(HttpMethod.GET, "http://test.api/pathX/{id}", vars).build();

assertThat(requestEntity1).isEqualTo(requestEntity2);
assertThat(requestEntity2).isEqualTo(requestEntity1);
assertThat(requestEntity1).isNotEqualTo(requestEntity3);
assertThat(requestEntity3).isNotEqualTo(requestEntity2);
assertThat(requestEntity1.hashCode()).isEqualTo(requestEntity2.hashCode());
assertThat(requestEntity1.hashCode()).isNotEqualTo(requestEntity3.hashCode());
}

}

0 comments on commit 4b01370

Please sign in to comment.