Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Equals and HashCode methods for better comparison. #16842

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

GrmpfNarf
Copy link

Closes gh-16394

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 28, 2025
@jzheaux jzheaux added type: enhancement A general enhancement in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 31, 2025
Copy link
Member

@sjohnr sjohnr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @GrmpfNarf! I have included some feedback below. Also please note that you will need to add a Signed-off-by trailer in the commit, per contributing guidelines.

}
OAuth2AuthorizationRequest that = (OAuth2AuthorizationRequest) obj;

if (!this.authorizationUri.equals(that.authorizationUri)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be nice to just use Objects.equals(a, b) for each of these checks and chain them together with &&. Would you mind doing that to make the entire method more concise while still being null-safe?

return Objects.equals(a, b)
    && Objects.equals(...)
    ...

@Override
public int hashCode() {
int result = this.authorizationUri.hashCode();
result = 31 * result + this.clientId.hashCode();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to equals, it would be nicer to use Objects.hashCode() for each field to make the method more concise. Would you mind updating that?

@@ -28,8 +29,7 @@

import org.springframework.security.oauth2.core.AuthorizationGrantType;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend updating import settings for your IDE to not collapse imports, as this style of import will fail the build.

@@ -365,4 +365,45 @@ public void buildWhenAdditionalParametersContainsNullThenAuthorizationRequestUri
+ "item1=null&item2=value2");
}

@Test
public void equalsTrueTest() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you please ensure this test populates every available field to ensure they are all accounted for in the equals method? Also, could you change the method name to something like equalsWhenAllFieldsEqualThenTrue?

}

@Test
public void hashCodeTest() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you please ensure this test populates every available field to ensure they are all accounted for in the hashCode method? Also, could you change the method name to something like hashCodeWhenAllFieldsEqualThenHashCodesAreEqual?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add proper equals (and hashCode) methods to OAuth2AuthorizationRequest
4 participants