Skip to content

Commit 768602b

Browse files
committed
Add Equals and HashCode methods for better comparison.
Closes spring-projectsgh-16394 Signed-off-by: Maximilian Klose <[email protected]>
1 parent fc19bf8 commit 768602b

File tree

2 files changed

+109
-3
lines changed

2 files changed

+109
-3
lines changed

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

+66-1
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.
@@ -188,6 +188,71 @@ public static Builder authorizationCode() {
188188
return new Builder(AuthorizationGrantType.AUTHORIZATION_CODE);
189189
}
190190

191+
@Override
192+
public boolean equals(Object obj) {
193+
if (this == obj) {
194+
return true;
195+
}
196+
if (obj == null || this.getClass() != obj.getClass()) {
197+
return false;
198+
}
199+
OAuth2AuthorizationRequest that = (OAuth2AuthorizationRequest) obj;
200+
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);
238+
}
239+
240+
@Override
241+
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;
254+
}
255+
191256
/**
192257
* Returns a new {@link Builder}, initialized with the values from the provided
193258
* {@code authorizationRequest}.

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

+43-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.net.URI;
2020
import java.util.Arrays;
21+
import java.util.Collections;
2122
import java.util.HashMap;
2223
import java.util.LinkedHashMap;
2324
import java.util.LinkedHashSet;
@@ -28,8 +29,7 @@
2829

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

31-
import static org.assertj.core.api.Assertions.assertThat;
32-
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
32+
import static org.assertj.core.api.Assertions.*;
3333

3434
/**
3535
* Tests for {@link OAuth2AuthorizationRequest}.
@@ -365,4 +365,45 @@ public void buildWhenAdditionalParametersContainsNullThenAuthorizationRequestUri
365365
+ "item1=null&item2=value2");
366366
}
367367

368+
@Test
369+
public void equalsTrueTest() {
370+
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();
376+
377+
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();
383+
384+
assertThat(authorizationRequest1).isEqualTo(authorizationRequest2);
385+
}
386+
387+
@Test
388+
public void hashCodeTest() {
389+
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();
395+
396+
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();
402+
403+
int authorizationRequest1HashCode = authorizationRequest1.hashCode();
404+
int authorizationRequest2HashCode = authorizationRequest2.hashCode();
405+
406+
assertThat(authorizationRequest1HashCode).isEqualTo(authorizationRequest2HashCode);
407+
}
408+
368409
}

0 commit comments

Comments
 (0)