Skip to content

Commit 0ba7b86

Browse files
committed
Add Equals and HashCode methods for better comparison.
Closes gh-16394 Signed-off-by: Maximilian Klose <[email protected]>
1 parent 95e04fa commit 0ba7b86

File tree

2 files changed

+38
-69
lines changed

2 files changed

+38
-69
lines changed

oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequest.java

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.LinkedHashMap;
2525
import java.util.LinkedHashSet;
2626
import java.util.Map;
27+
import java.util.Objects;
2728
import java.util.Set;
2829
import java.util.function.Consumer;
2930
import java.util.function.Function;
@@ -198,59 +199,23 @@ public boolean equals(Object obj) {
198199
}
199200
OAuth2AuthorizationRequest that = (OAuth2AuthorizationRequest) obj;
200201

201-
if (!this.authorizationUri.equals(that.authorizationUri)) {
202-
return false;
203-
}
204-
205-
if (!this.authorizationGrantType.equals(that.authorizationGrantType)) {
206-
return false;
207-
}
208-
209-
if (this.responseType != that.responseType) {
210-
return false;
211-
}
212-
213-
if (!this.clientId.equals(that.clientId)) {
214-
return false;
215-
}
216-
217-
if (!this.redirectUri.equals(that.redirectUri)) {
218-
return false;
219-
}
220-
221-
if (!this.scopes.equals(that.scopes)) {
222-
return false;
223-
}
224-
225-
if (!this.state.equals(that.state)) {
226-
return false;
227-
}
228-
229-
if (!this.additionalParameters.equals(that.additionalParameters)) {
230-
return false;
231-
}
232-
233-
if (!this.authorizationRequestUri.equals(that.authorizationRequestUri)) {
234-
return false;
235-
}
236-
237-
return this.attributes.equals(that.attributes);
202+
return Objects.equals(this.authorizationUri, that.authorizationUri)
203+
&& Objects.equals(this.authorizationGrantType, that.authorizationGrantType)
204+
&& Objects.equals(this.responseType, that.responseType) && Objects.equals(this.clientId, that.clientId)
205+
&& Objects.equals(this.redirectUri, that.redirectUri) && Objects.equals(this.scopes, that.scopes)
206+
&& Objects.equals(this.state, that.state)
207+
&& Objects.equals(this.additionalParameters, that.additionalParameters)
208+
&& Objects.equals(this.authorizationRequestUri, that.authorizationRequestUri)
209+
&& Objects.equals(this.attributes, that.attributes);
238210
}
239211

240212
@Override
241213
public int hashCode() {
242-
int result = this.authorizationUri.hashCode();
243-
result = 31 * result + this.clientId.hashCode();
244-
result = 31 * result + ((this.authorizationGrantType == null) ? 0 : this.authorizationGrantType.hashCode());
245-
result = 31 * result + ((this.responseType == null) ? 0 : this.responseType.hashCode());
246-
result = 31 * result + ((this.redirectUri == null) ? 0 : this.redirectUri.hashCode());
247-
result = 31 * result + ((this.scopes == null) ? 0 : this.scopes.hashCode());
248-
result = 31 * result + ((this.state == null) ? 0 : this.state.hashCode());
249-
result = 31 * result + ((this.additionalParameters == null) ? 0 : this.additionalParameters.hashCode());
250-
result = 31 * result + ((this.authorizationRequestUri == null) ? 0 : this.authorizationRequestUri.hashCode());
251-
result = 31 * result + ((this.attributes == null) ? 0 : this.attributes.hashCode());
252-
253-
return result;
214+
return Objects.hashCode(this.authorizationUri) + Objects.hashCode(this.clientId)
215+
+ Objects.hashCode(this.authorizationGrantType) + Objects.hashCode(this.responseType)
216+
+ Objects.hashCode(this.redirectUri) + Objects.hashCode(this.scopes) + Objects.hashCode(this.state)
217+
+ Objects.hashCode(this.additionalParameters) + Objects.hashCode(this.authorizationRequestUri)
218+
+ Objects.hashCode(this.attributes);
254219
}
255220

256221
/**

oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequestTests.java

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -368,37 +368,41 @@ public void buildWhenAdditionalParametersContainsNullThenAuthorizationRequestUri
368368
@Test
369369
public void equalsTrueTest() {
370370
OAuth2AuthorizationRequest authorizationRequest1 = TestOAuth2AuthorizationRequests.request()
371-
.authorizationRequestUri("http://example.com")
372-
.additionalParameters(Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue"))
373-
.parameters(parametersMap -> parametersMap.put("someParameterKey", "someParameterValue"))
374-
.scope("someScope")
375-
.build();
371+
.authorizationRequestUri("https://example.com")
372+
.additionalParameters(
373+
Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue"))
374+
.parameters((parametersMap) -> parametersMap.put("someParameterKey", "someParameterValue"))
375+
.scope("someScope")
376+
.build();
376377

377378
OAuth2AuthorizationRequest authorizationRequest2 = TestOAuth2AuthorizationRequests.request()
378-
.authorizationRequestUri("http://example.com")
379-
.additionalParameters(Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue"))
380-
.parameters(parametersMap -> parametersMap.put("someParameterKey", "someParameterValue"))
381-
.scope("someScope")
382-
.build();
379+
.authorizationRequestUri("https://example.com")
380+
.additionalParameters(
381+
Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue"))
382+
.parameters((parametersMap) -> parametersMap.put("someParameterKey", "someParameterValue"))
383+
.scope("someScope")
384+
.build();
383385

384386
assertThat(authorizationRequest1).isEqualTo(authorizationRequest2);
385387
}
386388

387389
@Test
388390
public void hashCodeTest() {
389391
OAuth2AuthorizationRequest authorizationRequest1 = TestOAuth2AuthorizationRequests.request()
390-
.authorizationRequestUri("http://example.com")
391-
.additionalParameters(Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue"))
392-
.parameters(parametersMap -> parametersMap.put("someParameterKey", "someParameterValue"))
393-
.scope("someScope")
394-
.build();
392+
.authorizationRequestUri("https://example.com")
393+
.additionalParameters(
394+
Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue"))
395+
.parameters((parametersMap) -> parametersMap.put("someParameterKey", "someParameterValue"))
396+
.scope("someScope")
397+
.build();
395398

396399
OAuth2AuthorizationRequest authorizationRequest2 = TestOAuth2AuthorizationRequests.request()
397-
.authorizationRequestUri("http://example.com")
398-
.additionalParameters(Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue"))
399-
.parameters(parametersMap -> parametersMap.put("someParameterKey", "someParameterValue"))
400-
.scope("someScope")
401-
.build();
400+
.authorizationRequestUri("https://example.com")
401+
.additionalParameters(
402+
Collections.singletonMap("someAdditionalParameterKey", "someAdditionalParameterValue"))
403+
.parameters((parametersMap) -> parametersMap.put("someParameterKey", "someParameterValue"))
404+
.scope("someScope")
405+
.build();
402406

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

0 commit comments

Comments
 (0)