Skip to content

Commit 62f33d3

Browse files
ngocnhan-tran1996jzheaux
authored andcommitted
Add equals and hashCode to HttpMethodRequestMatcher
Closes gh-18911 Signed-off-by: Tran Ngoc Nhan <ngocnhan.tran1996@gmail.com>
1 parent 9fed1ac commit 62f33d3

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

web/src/main/java/org/springframework/security/web/servlet/util/matcher/PathPatternRequestMatcher.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,19 @@ public boolean matches(HttpServletRequest request) {
305305
return this.method.name().equals(request.getMethod());
306306
}
307307

308+
@Override
309+
public boolean equals(Object o) {
310+
if (!(o instanceof HttpMethodRequestMatcher that)) {
311+
return false;
312+
}
313+
return Objects.equals(this.method, that.method);
314+
}
315+
316+
@Override
317+
public int hashCode() {
318+
return Objects.hash(this.method);
319+
}
320+
308321
@Override
309322
public String toString() {
310323
return "HttpMethod [" + this.method + "]";

web/src/test/java/org/springframework/security/web/servlet/util/matcher/PathPatternRequestMatcherTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,17 @@ void matcherWhenBasePathIsRootThenNoDoubleSlash() {
145145
assertThat(matcher.matches(mock)).isTrue();
146146
}
147147

148+
// gh-18911
149+
@Test
150+
void testEqualsWithSameAndDifferentHttpMethod() {
151+
PathPatternRequestMatcher.Builder builder = PathPatternRequestMatcher.withDefaults();
152+
PathPatternRequestMatcher matcher1 = builder.matcher(HttpMethod.GET, "/foo");
153+
PathPatternRequestMatcher matcher2 = builder.matcher(HttpMethod.GET, "/foo");
154+
PathPatternRequestMatcher matcher3 = builder.matcher(HttpMethod.POST, "/foo");
155+
assertThat(matcher1).isEqualTo(matcher2);
156+
assertThat(matcher1).isNotEqualTo(matcher3);
157+
}
158+
148159
MockHttpServletRequest request(String uri) {
149160
MockHttpServletRequest request = new MockHttpServletRequest("GET", uri);
150161
ServletRequestPathUtils.parseAndCache(request);

0 commit comments

Comments
 (0)