Bläddra i källkod

Add Equals and HashCode methods for better comparison.

Closes gh-16394

Signed-off-by: Maximilian Klose <maximilian.klose@adesso.de>
Maximilian Klose 3 månader sedan
förälder
incheckning
ec05e65668

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

@@ -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.
@@ -24,6 +24,7 @@ import java.util.Collections;
 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;
@@ -188,6 +189,33 @@ public final class OAuth2AuthorizationRequest implements Serializable {
 		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.hash(this.authorizationUri, this.clientId, this.authorizationGrantType, this.responseType,
+				this.redirectUri, this.scopes, this.state, this.additionalParameters, this.authorizationRequestUri,
+				this.attributes);
+	}
+
 	/**
 	 * Returns a new {@link Builder}, initialized with the values from the provided
 	 * {@code authorizationRequest}.

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

@@ -365,4 +365,27 @@ public class OAuth2AuthorizationRequestTests {
 					+ "item1=null&item2=value2");
 	}
 
+	@Test
+	public void equalsWhenAllFieldsEqualEqualsThenTrue() {
+		OAuth2AuthorizationRequest authorizationRequest1 = TestOAuth2AuthorizationRequests.allFields().build();
+
+		OAuth2AuthorizationRequest authorizationRequest2 = TestOAuth2AuthorizationRequests.allFields().build();
+
+		assertThat(authorizationRequest1).isNotSameAs(authorizationRequest2);
+		assertThat(authorizationRequest1).isEqualTo(authorizationRequest2);
+	}
+
+	@Test
+	public void hashCodeWhenAllFieldsEqualThenHashCodesAreEqual() {
+		OAuth2AuthorizationRequest authorizationRequest1 = TestOAuth2AuthorizationRequests.allFields().build();
+
+		OAuth2AuthorizationRequest authorizationRequest2 = TestOAuth2AuthorizationRequests.allFields().build();
+
+		int authorizationRequest1HashCode = authorizationRequest1.hashCode();
+		int authorizationRequest2HashCode = authorizationRequest2.hashCode();
+
+		assertThat(authorizationRequest1).isNotSameAs(authorizationRequest2);
+		assertThat(authorizationRequest1HashCode).isEqualTo(authorizationRequest2HashCode);
+	}
+
 }

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

@@ -47,4 +47,15 @@ public final class TestOAuth2AuthorizationRequests {
 		return request().scope("openid");
 	}
 
+	public static OAuth2AuthorizationRequest.Builder allFields() {
+		// @formatter:off
+		return request()
+				.authorizationRequestUri("https://example.com")
+				.additionalParameters(Map.of("someAdditionalParameterKey", "someAdditionalParameterValue"))
+				.parameters((parametersMap) ->
+						parametersMap.put("someParameterKey", "someParameterValue"))
+				.scope("someScope");
+		// @formatter:on
+	}
+
 }