Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces support for custom universe domains in OAuth2CredentialsWithRefresh by adding a universeDomain field, corresponding builder methods, and updating the equals and hashCode implementations. It also adds comprehensive unit tests for these changes. The feedback suggests a critical improvement to the equals and hashCode methods: excluding the refreshHandler functional interface from these checks, as comparing lambdas for equality is fragile. Additionally, the feedback recommends using the getUniverseDomain() getter instead of the direct field access to ensure consistency, along with corresponding updates to the unit tests.
| @Override | ||
| public boolean equals(@Nullable Object obj) { | ||
| if (!(obj instanceof OAuth2CredentialsWithRefresh)) { | ||
| return false; | ||
| } | ||
| OAuth2CredentialsWithRefresh other = (OAuth2CredentialsWithRefresh) obj; | ||
| return super.equals(other) | ||
| && Objects.equals(this.refreshHandler, other.refreshHandler) | ||
| && Objects.equals(this.universeDomain, other.universeDomain); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(super.hashCode(), this.refreshHandler, this.universeDomain); | ||
| } |
There was a problem hiding this comment.
Comparing functional interfaces/callbacks (like OAuth2RefreshHandler) in equals and hashCode is fragile. Since lambdas do not implement equals in a meaningful way (defaulting to reference equality), two credentials with identical tokens and domains but different lambda instances for the refresh handler will be considered unequal. Excluding refreshHandler from equals and hashCode makes the equality check more robust and correct. Additionally, to maintain consistency between deserialized and newly constructed instances, ensure that equals() and hashCode() use the getter method getUniverseDomain() (or apply the same fallback logic) rather than accessing the field directly.
@Override
public boolean equals(@Nullable Object obj) {
if (!(obj instanceof OAuth2CredentialsWithRefresh)) {
return false;
}
OAuth2CredentialsWithRefresh other = (OAuth2CredentialsWithRefresh) obj;
return super.equals(other)
&& Objects.equals(this.getUniverseDomain(), other.getUniverseDomain());
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), this.getUniverseDomain());
}References
- When adding a new field to a serializable class that may be null when deserializing older versions, and providing a getter with a default fallback value, ensure that equals() and hashCode() use the getter method (or apply the same fallback logic) rather than accessing the field directly, to maintain consistency between deserialized and newly constructed instances.
| @Test | ||
| void equals_true() { | ||
| OAuth2CredentialsWithRefresh.OAuth2RefreshHandler handler = () -> ACCESS_TOKEN; | ||
| OAuth2CredentialsWithRefresh credentials = | ||
| createTestBuilder(handler).setUniverseDomain("example.com").build(); | ||
| OAuth2CredentialsWithRefresh other = | ||
| createTestBuilder(handler).setUniverseDomain("example.com").build(); | ||
|
|
||
| assertEquals(credentials, credentials); | ||
| assertEquals(credentials, other); | ||
| assertEquals(other, credentials); | ||
| assertEquals(credentials.hashCode(), other.hashCode()); | ||
| } |
There was a problem hiding this comment.
Update the test to use two different lambda instances for the refresh handler. This demonstrates that different handler instances do not affect equality when the tokens and domains are identical.
| @Test | |
| void equals_true() { | |
| OAuth2CredentialsWithRefresh.OAuth2RefreshHandler handler = () -> ACCESS_TOKEN; | |
| OAuth2CredentialsWithRefresh credentials = | |
| createTestBuilder(handler).setUniverseDomain("example.com").build(); | |
| OAuth2CredentialsWithRefresh other = | |
| createTestBuilder(handler).setUniverseDomain("example.com").build(); | |
| assertEquals(credentials, credentials); | |
| assertEquals(credentials, other); | |
| assertEquals(other, credentials); | |
| assertEquals(credentials.hashCode(), other.hashCode()); | |
| } | |
| @Test | |
| void equals_true() { | |
| OAuth2CredentialsWithRefresh.OAuth2RefreshHandler handler1 = () -> ACCESS_TOKEN; | |
| OAuth2CredentialsWithRefresh.OAuth2RefreshHandler handler2 = () -> ACCESS_TOKEN; | |
| OAuth2CredentialsWithRefresh credentials = | |
| createTestBuilder(handler1).setUniverseDomain("example.com").build(); | |
| OAuth2CredentialsWithRefresh other = | |
| createTestBuilder(handler2).setUniverseDomain("example.com").build(); | |
| assertEquals(credentials, credentials); | |
| assertEquals(credentials, other); | |
| assertEquals(other, credentials); | |
| assertEquals(credentials.hashCode(), other.hashCode()); | |
| } |
| void equals_false() { | ||
| OAuth2CredentialsWithRefresh.OAuth2RefreshHandler handler = () -> ACCESS_TOKEN; | ||
| OAuth2CredentialsWithRefresh.OAuth2RefreshHandler otherHandler = () -> ACCESS_TOKEN; | ||
| AccessToken otherToken = | ||
| new AccessToken("otherAccessToken", new Date(ACCESS_TOKEN.getExpirationTime().getTime())); | ||
| OAuth2CredentialsWithRefresh baseline = | ||
| createTestBuilder(handler).setUniverseDomain("example.com").build(); | ||
|
|
||
| List<Consumer<OAuth2CredentialsWithRefresh.Builder>> mutations = | ||
| Arrays.asList( | ||
| b -> b.setAccessToken(otherToken), | ||
| b -> b.setRefreshHandler(otherHandler), | ||
| b -> b.setUniverseDomain("other.example.com")); | ||
|
|
||
| assertNotEquals(baseline, null); | ||
| assertNotEquals(baseline, "not-a-credential"); | ||
| for (Consumer<OAuth2CredentialsWithRefresh.Builder> mutation : mutations) { | ||
| OAuth2CredentialsWithRefresh.Builder builder = | ||
| createTestBuilder(handler).setUniverseDomain("example.com"); | ||
| mutation.accept(builder); | ||
| OAuth2CredentialsWithRefresh other = builder.build(); | ||
| assertNotEquals(baseline, other); | ||
| assertNotEquals(other, baseline); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Remove the refreshHandler mutation from the list of mutations since the refresh handler is excluded from equals and hashCode comparisons.
@Test
void equals_false() {
OAuth2CredentialsWithRefresh.OAuth2RefreshHandler handler = () -> ACCESS_TOKEN;
AccessToken otherToken =
new AccessToken("otherAccessToken", new Date(ACCESS_TOKEN.getExpirationTime().getTime()));
OAuth2CredentialsWithRefresh baseline =
createTestBuilder(handler).setUniverseDomain("example.com").build();
List<Consumer<OAuth2CredentialsWithRefresh.Builder>> mutations =
Arrays.asList(
b -> b.setAccessToken(otherToken),
b -> b.setUniverseDomain("other.example.com"));
assertNotEquals(baseline, null);
assertNotEquals(baseline, "not-a-credential");
for (Consumer<OAuth2CredentialsWithRefresh.Builder> mutation : mutations) {
OAuth2CredentialsWithRefresh.Builder builder =
createTestBuilder(handler).setUniverseDomain("example.com");
mutation.accept(builder);
OAuth2CredentialsWithRefresh other = builder.build();
assertNotEquals(baseline, other);
assertNotEquals(other, baseline);
}
}
Adds
universeDomainsupport toOAuth2CredentialsWithRefresh:universeDomainfield (defaulting null or blank values togoogleapis.com), overridesgetUniverseDomain(), addsBuilder.setUniverseDomain()andBuilder.getUniverseDomain(), and includesuniverseDomaininequals()andhashCode().OAuth2Credentials.create(AccessToken)Javadoc to note thegoogleapis.comdefault and point custom universe domain callers toGoogleCredentials.create(String, AccessToken).OAuth2CredentialsWithRefreshTestcovering builder and resolved universe domains, protected constructor default, and field-levelequals()/hashCode()coverage.