Skip to content

Commit 83881f5

Browse files
committed
[JENKINS-75188] NPE in StandardUsernameCredentials.hashCode when has empty credentials
Fix equals and hashCode method of StandardUsernameCredentials implementation that throw NPE when stored credentials is null.
1 parent 7686eb7 commit 83881f5

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/impl/extension/GitClientAuthenticatorExtension.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public String getUrl() {
6666

6767
@Override
6868
public int hashCode() {
69-
return Objects.hash(credentials.getId(), url);
69+
return Objects.hash(credentials != null ? credentials.getId() : null, credentials);
7070
}
7171

7272
@Override
@@ -81,7 +81,7 @@ public boolean equals(Object obj) {
8181
return false;
8282
}
8383
GitClientAuthenticatorExtension other = (GitClientAuthenticatorExtension) obj;
84-
return Objects.equals(credentials.getId(), other.credentials.getId())
84+
return Objects.equals(credentials != null ? credentials.getId() : credentials, other.credentials != null ? other.credentials.getId() : other.credentials)
8585
&& Objects.equals(url, other.url);
8686
}
8787

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.cloudbees.jenkins.plugins.bitbucket.impl.extension;
2+
3+
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
4+
import hudson.plugins.git.extensions.GitSCMExtension;
5+
import org.junit.jupiter.api.Test;
6+
import org.jvnet.hudson.test.Issue;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
class GitClientAuthenticatorExtensionTest {
11+
12+
@Issue("JENKINS-75188")
13+
@Test
14+
void test_equals_hashCode() throws Exception {
15+
GitSCMExtension extension = new GitClientAuthenticatorExtension("url", null);
16+
assertThat(extension.hashCode()).isNotZero();
17+
assertThat(extension).isEqualTo(new GitClientAuthenticatorExtension("url", null));
18+
19+
extension = new GitClientAuthenticatorExtension("url", new UsernamePasswordCredentialsImpl(null, "id", null, null, null));
20+
assertThat(extension.hashCode()).isNotZero();
21+
assertThat(extension).isNotEqualTo(new GitClientAuthenticatorExtension("url", null));
22+
23+
extension = new GitClientAuthenticatorExtension("url", new UsernamePasswordCredentialsImpl(null, null, null, null, null));
24+
assertThat(extension.hashCode()).isNotZero();
25+
assertThat(extension).isNotEqualTo(new GitClientAuthenticatorExtension("url", new UsernamePasswordCredentialsImpl(null, "some-id", null, null, null)));
26+
}
27+
}

0 commit comments

Comments
 (0)