Skip to content

Add Equals and HashCode methods for better comparison. #16842

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -188,6 +189,35 @@ public static Builder authorizationCode() {
return new Builder(AuthorizationGrantType.AUTHORIZATION_CODE);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || this.getClass() != obj.getClass()) {
return false;
}
OAuth2AuthorizationRequest that = (OAuth2AuthorizationRequest) obj;

return Objects.equals(this.authorizationUri, that.authorizationUri)
&& Objects.equals(this.authorizationGrantType, that.authorizationGrantType)
&& Objects.equals(this.responseType, that.responseType) && Objects.equals(this.clientId, that.clientId)
&& Objects.equals(this.redirectUri, that.redirectUri) && Objects.equals(this.scopes, that.scopes)
&& Objects.equals(this.state, that.state)
&& Objects.equals(this.additionalParameters, that.additionalParameters)
&& Objects.equals(this.authorizationRequestUri, that.authorizationRequestUri)
&& Objects.equals(this.attributes, that.attributes);
}

@Override
public int hashCode() {
return Objects.hashCode(this.authorizationUri) + Objects.hashCode(this.clientId)
+ Objects.hashCode(this.authorizationGrantType) + Objects.hashCode(this.responseType)
+ Objects.hashCode(this.redirectUri) + Objects.hashCode(this.scopes) + Objects.hashCode(this.state)
+ Objects.hashCode(this.additionalParameters) + Objects.hashCode(this.authorizationRequestUri)
+ Objects.hashCode(this.attributes);
}

/**
* Returns a new {@link Builder}, initialized with the values from the provided
* {@code authorizationRequest}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
Expand All @@ -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.

Copy link
Author

Choose a reason for hiding this comment

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

I can do that but it is the desired behavior as stated here


/**
* Tests for {@link OAuth2AuthorizationRequest}.
Expand Down Expand Up @@ -365,4 +365,49 @@ 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?

Copy link
Author

Choose a reason for hiding this comment

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

Hi,
how should I ensure that all fields are accounted ? Via Reflections ?

OAuth2AuthorizationRequest authorizationRequest1 = TestOAuth2AuthorizationRequests.request()
.authorizationRequestUri("https://example.com")
.additionalParameters(
Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue"))
.parameters((parametersMap) -> parametersMap.put("someParameterKey", "someParameterValue"))
.scope("someScope")
.build();

OAuth2AuthorizationRequest authorizationRequest2 = TestOAuth2AuthorizationRequests.request()
.authorizationRequestUri("https://example.com")
.additionalParameters(
Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue"))
.parameters((parametersMap) -> parametersMap.put("someParameterKey", "someParameterValue"))
.scope("someScope")
.build();

assertThat(authorizationRequest1).isEqualTo(authorizationRequest2);
}

@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?

OAuth2AuthorizationRequest authorizationRequest1 = TestOAuth2AuthorizationRequests.request()
.authorizationRequestUri("https://example.com")
.additionalParameters(
Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue"))
.parameters((parametersMap) -> parametersMap.put("someParameterKey", "someParameterValue"))
.scope("someScope")
.build();

OAuth2AuthorizationRequest authorizationRequest2 = TestOAuth2AuthorizationRequests.request()
.authorizationRequestUri("https://example.com")
.additionalParameters(
Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue"))
.parameters((parametersMap) -> parametersMap.put("someParameterKey", "someParameterValue"))
.scope("someScope")
.build();

int authorizationRequest1HashCode = authorizationRequest1.hashCode();
int authorizationRequest2HashCode = authorizationRequest2.hashCode();

assertThat(authorizationRequest1HashCode).isEqualTo(authorizationRequest2HashCode);
}

}