Fix IllegalArgumentException in equals and hashcodes. Fixes #2304 (#2307)

Co-authored-by: Ryan Baxter <524254+ryanjbaxter@users.noreply.github.com>
This commit is contained in:
Ryan Baxter
2023-07-16 19:14:22 -04:00
committed by GitHub
parent d1ec6e56fc
commit 3910fbff56
2 changed files with 28 additions and 7 deletions

View File

@@ -123,17 +123,27 @@ public class ConfigServerConfigDataResource extends ConfigDataResource {
}
private boolean uriEqual(String thisUriString, String thatUriString) {
UriComponents thisUri = UriComponentsBuilder.fromHttpUrl(thisUriString).build();
UriComponents thatUri = UriComponentsBuilder.fromHttpUrl(thatUriString).build();
return Objects.equals(thisUri.getHost(), thatUri.getHost())
&& Objects.equals(thisUri.getPort(), thatUri.getPort())
&& Objects.equals(thisUri.getPath(), thatUri.getPath());
try {
UriComponents thisUri = UriComponentsBuilder.fromHttpUrl(thisUriString).build();
UriComponents thatUri = UriComponentsBuilder.fromHttpUrl(thatUriString).build();
return Objects.equals(thisUri.getHost(), thatUri.getHost())
&& Objects.equals(thisUri.getPort(), thatUri.getPort())
&& Objects.equals(thisUri.getPath(), thatUri.getPath());
}
catch (Exception e) {
return Objects.equals(thisUriString, thatUriString);
}
}
private int urisHashCode(String[] uris) {
return Arrays.stream(uris).mapToInt(uriString -> {
UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(uriString).build();
return Objects.hash(uriComponents.getHost(), uriComponents.getPath(), uriComponents.getPort());
try {
UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(uriString).build();
return Objects.hash(uriComponents.getHost(), uriComponents.getPath(), uriComponents.getPort());
}
catch (Exception e) {
return Arrays.hashCode(uris);
}
}).sum();
}

View File

@@ -318,4 +318,15 @@ class ConfigServerConfigDataResourceTests {
assertThat(r1.hashCode()).isNotEqualTo(r2.hashCode());
}
@Test
void testInvalidUri() {
ConfigClientProperties r1Properties = new ConfigClientProperties();
r1Properties.setUri(new String[] { "//" });
ConfigServerConfigDataResource r1 = new ConfigServerConfigDataResource(r1Properties, true, null);
ConfigClientProperties r2Properties = new ConfigClientProperties();
r2Properties.setUri(new String[] { "//" });
ConfigServerConfigDataResource r2 = new ConfigServerConfigDataResource(r2Properties, true, null);
assertThat(r1).isEqualTo(r2);
}
}