From 99302fd6bd1549a0cafbe4b5605b5196d0806567 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 23 May 2019 09:54:45 -0400 Subject: [PATCH] AntPathMatcher#isPattern also checks URI vars Closes gh-22959 --- .../springframework/util/AntPathMatcher.java | 18 ++++++++++++++++-- .../util/AntPathMatcherTests.java | 12 +++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java b/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java index 40574a5a2d..0463e9e4bb 100644 --- a/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java +++ b/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -169,7 +169,21 @@ public class AntPathMatcher implements PathMatcher { @Override public boolean isPattern(String path) { - return (path.indexOf('*') != -1 || path.indexOf('?') != -1); + boolean uriVar = false; + for (int i = 0; i < path.length(); i++) { + char c = path.charAt(i); + if (c == '*' || c == '?') { + return true; + } + if (c == '{') { + uriVar = true; + continue; + } + if (c == '}' && uriVar) { + return true; + } + } + return false; } @Override diff --git a/spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java b/spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java index 9d5082a1fd..9117460041 100644 --- a/spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java +++ b/spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 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. @@ -681,4 +681,14 @@ public class AntPathMatcherTests { "/*.html.hotel.*", pathMatcher.combine("/*.html", "hotel.*")); } + @Test // gh-22959 + public void isPattern() { + assertTrue(pathMatcher.isPattern("/test/*")); + assertTrue(pathMatcher.isPattern("/test/**/name")); + assertTrue(pathMatcher.isPattern("/test?")); + assertTrue(pathMatcher.isPattern("/test/{name}")); + assertFalse(pathMatcher.isPattern("/test/name")); + assertFalse(pathMatcher.isPattern("/test/foo{bar")); + } + }