Skip to content

Commit ec05e65

Browse files
GrmpfNarfjzheaux
authored andcommitted
Add Equals and HashCode methods for better comparison.
Closes gh-16394 Signed-off-by: Maximilian Klose <[email protected]>
1 parent bf05b8b commit ec05e65

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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;
@@ -188,6 +189,33 @@ public static Builder authorizationCode() {
188189
return new Builder(AuthorizationGrantType.AUTHORIZATION_CODE);
189190
}
190191

192+
@Override
193+
public boolean equals(Object obj) {
194+
if (this == obj) {
195+
return true;
196+
}
197+
if (obj == null || this.getClass() != obj.getClass()) {
198+
return false;
199+
}
200+
OAuth2AuthorizationRequest that = (OAuth2AuthorizationRequest) obj;
201+
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);
210+
}
211+
212+
@Override
213+
public int hashCode() {
214+
return Objects.hash(this.authorizationUri, this.clientId, this.authorizationGrantType, this.responseType,
215+
this.redirectUri, this.scopes, this.state, this.additionalParameters, this.authorizationRequestUri,
216+
this.attributes);
217+
}
218+
191219
/**
192220
* Returns a new {@link Builder}, initialized with the values from the provided
193221
* {@code authorizationRequest}.

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,27 @@ public void buildWhenAdditionalParametersContainsNullThenAuthorizationRequestUri
365365
+ "item1=null&item2=value2");
366366
}
367367

368+
@Test
369+
public void equalsWhenAllFieldsEqualEqualsThenTrue() {
370+
OAuth2AuthorizationRequest authorizationRequest1 = TestOAuth2AuthorizationRequests.allFields().build();
371+
372+
OAuth2AuthorizationRequest authorizationRequest2 = TestOAuth2AuthorizationRequests.allFields().build();
373+
374+
assertThat(authorizationRequest1).isNotSameAs(authorizationRequest2);
375+
assertThat(authorizationRequest1).isEqualTo(authorizationRequest2);
376+
}
377+
378+
@Test
379+
public void hashCodeWhenAllFieldsEqualThenHashCodesAreEqual() {
380+
OAuth2AuthorizationRequest authorizationRequest1 = TestOAuth2AuthorizationRequests.allFields().build();
381+
382+
OAuth2AuthorizationRequest authorizationRequest2 = TestOAuth2AuthorizationRequests.allFields().build();
383+
384+
int authorizationRequest1HashCode = authorizationRequest1.hashCode();
385+
int authorizationRequest2HashCode = authorizationRequest2.hashCode();
386+
387+
assertThat(authorizationRequest1).isNotSameAs(authorizationRequest2);
388+
assertThat(authorizationRequest1HashCode).isEqualTo(authorizationRequest2HashCode);
389+
}
390+
368391
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,15 @@ public static OAuth2AuthorizationRequest.Builder oidcRequest() {
4747
return request().scope("openid");
4848
}
4949

50+
public static OAuth2AuthorizationRequest.Builder allFields() {
51+
// @formatter:off
52+
return request()
53+
.authorizationRequestUri("https://example.com")
54+
.additionalParameters(Map.of("someAdditionalParameterKey", "someAdditionalParameterValue"))
55+
.parameters((parametersMap) ->
56+
parametersMap.put("someParameterKey", "someParameterValue"))
57+
.scope("someScope");
58+
// @formatter:on
59+
}
60+
5061
}

0 commit comments

Comments
 (0)