Fix #3236: AsyncPredicate and GatewayPredicate do not override equals…#4122
Open
stbattula-research wants to merge 1 commit intospring-cloud:mainfrom
Open
Conversation
…rride equals/hashCode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: AsyncPredicate and GatewayPredicate do not override equals/hashCode
Fixes #3236 — Identical Routes are not considered equal: AsyncPredicate does not override equals()
Problem
Spring Cloud Gateway builds route predicates as trees of
AsyncPredicateandGatewayPredicateobjects. When two routes are configured identically—same predicates, same filters—they should be considered equal so the gateway can deduplicate, cache, or diff route definitions efficiently.However, none of the inner classes in
AsyncPredicateorGatewayPredicateoverrodeequals()orhashCode(). They all fell back tojava.lang.Object.equals(), which uses reference/identity comparison. Two separately constructed but logically identical predicates were therefore never equal:This meant that two
Routeobjects built from identical configuration could never satisfy a value-equality check even when every field was the same.Root Cause
The following eight inner classes were missing
equals()andhashCode():AsyncPredicateDefaultAsyncPredicateNegateAsyncPredicateAndAsyncPredicateOrAsyncPredicateGatewayPredicateGatewayPredicateWrapperNegateGatewayPredicateAndGatewayPredicateOrGatewayPredicateFix
Added
equals()andhashCode()to all eight inner classes following the standard Java contract:DefaultAsyncPredicate,GatewayPredicateWrapper): equality delegates to the wrapped predicate.NegateAsyncPredicate,NegateGatewayPredicate): equality compares the inner predicate.AndAsyncPredicate,OrAsyncPredicate,AndGatewayPredicate,OrGatewayPredicate): equality compares bothleftandrightcomponents.All implementations use
java.util.Objects.equals()/Objects.hash()for null-safe delegation.Example — DefaultAsyncPredicate (before → after)
Before: inherits
Object.equals()— identity comparison only.After:
The same pattern is applied consistently to all eight classes.
Files Changed
Tests Added
AsyncPredicateEqualsTestscovers:DefaultAsyncPredicate: same instance, same delegate, different delegate, nullNegateAsyncPredicate: same inner predicate, different inner predicateAndAsyncPredicate: same components, different left, different rightOrAsyncPredicate: same components, different componentsGatewayPredicateinner classes:NegateGatewayPredicate,AndGatewayPredicate,OrGatewayPredicateequality.and(),.or(),.negate()builder methodsNote on End-to-End Route Equality
This fix is a necessary prerequisite for full route equality. For two routes built from separate factory invocations to be equal end-to-end, individual predicate factory implementations (e.g.
PathRoutePredicateFactory) must also implementequals()andhashCode()based on their configuration objects. That is a follow-on concern tracked separately; this PR addresses the predicate composition layer which was the immediate bug reported.Checklist
equals()/hashCode()contract is consistent (a.equals(b)⟹a.hashCode() == b.hashCode())