-
Notifications
You must be signed in to change notification settings - Fork 6k
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
base: main
Are you sure you want to change the base?
Conversation
Closes spring-projectsgh-16394 Signed-off-by: Maximilian Klose <[email protected]>
There was a problem hiding this 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)) { |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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.*; |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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
?
Closes gh-16394