Affects: 5.3.10
Background:
I assume the AntPathMatcher matches (among othersú a path against a pattern if all below apply:
- pattern
and path
are equal
- they both contain only alphanumeric characters (no Ant-specific characters such as *
, ?
or {
with }
and no slashes /
and \
)
Reproducible sample:
The following code reproduces a strange behavior:
new AntPathMatcher().match("consent", "consent"); // true
new AntPathMatcher().match("consentreg", "consentreg"); // true
new AntPathMatcher("\\t").match("consent", "consent"); // true
new AntPathMatcher("\\t").match("consentreg", "consentreg"); // false
Do I assume the behavior wrongly? If so, would you describe the behavior and how to fix my use case?
Workaround:
As of now, I must check whether the pattern
is not Ant pattern and check the equality first:
String string = "consentreg";
String pattern = "consentreg";
AntPathMatcher matcher = new AntPathMatcher("\\t");
boolean result = (!matcher.isPattern(pattern) && pattern.equals(string)) || getAntPathMatcher().match(pattern, string);
// true
Comment From: rstoyanchev
The use of such a separator is a bit unusual. Do you actually mean for it to be a backslash followed by the letter t
or just a tab character? If it is the former, the use of multiple chars is somewhat of a surprise as each char ends up being used in StrinkTokenizer
as a character to split the path, so in effect it treats both the backslash and the t
as separators. If it is the latter, then it should be new AntPathMatcher("\t")
which works fine.
Comment From: Nikolas-Charalambidis
@rstoyanchev Originally, it was supposed to be the tab character "\t
, however, I mistakenly included one slash ("\\t"
). Although it was my error, I am still a bit surprised by the behavior that each character ends up being used independently and not as a whole.
Comment From: rstoyanchev
Okay, thanks for clarifying. Even if we did treat it as one, it wouldn't have been the intended result, since you were expecting a tab and not a slash followed by t
to be used as separator.
While it is true that a String implies potentially multiple characters, in practice the implementation is biased towards a single character separator I think, and I'm not keen on changing that. There could very well be more things that would need to change to make that fully working and it's not doing that without a clear use case. Therefore I'm closing this for now.
Comment From: aoyvx
@rstoyanchev HI.If AntPathmatcher
can only work normally under single character,I think mandatory verification should be added or a warning log should be given.
AntPathMatcher matcher = new AntPathMatcher("/+");
assertThat(matcher.match("test+/test", "test/+test")).isTrue();
assertThat(matcher.match("test//+test", "test//+test")).isFalse();