Combined, empty RequestMapping matches both "" and "/"

Closes gh-29625
This commit is contained in:
rstoyanchev
2023-01-09 16:56:29 +00:00
parent 46875c91ce
commit 312db36849
8 changed files with 40 additions and 35 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 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.
@@ -48,6 +48,9 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
private static final Set<String> EMPTY_PATH = Collections.singleton("");
private static final SortedSet<PathPattern> ROOT_PATH_PATTERNS =
new TreeSet<>(List.of(new PathPatternParser().parse(""), new PathPatternParser().parse("/")));
private final SortedSet<PathPattern> patterns;
@@ -109,19 +112,18 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
}
/**
* Returns a new instance with URL patterns from the current instance ("this") and
* the "other" instance as follows:
* Combine the patterns of the current and of the other instances as follows:
* <ul>
* <li>If there are patterns in both instances, combine the patterns in "this" with
* the patterns in "other" using {@link PathPattern#combine(PathPattern)}.
* <li>If only one instance has patterns, use them.
* <li>If neither instance has patterns, use an empty String (i.e. "").
* <li>If only one instance has patterns, use those.
* <li>If both have patterns, combine patterns from "this" instance with
* patterns from the other instance via {@link PathPattern#combine(PathPattern)}.
* <li>If neither has patterns, use {@code ""} and {@code "/"} as root path patterns.
* </ul>
*/
@Override
public PatternsRequestCondition combine(PatternsRequestCondition other) {
if (isEmptyPathMapping() && other.isEmptyPathMapping()) {
return this;
return new PatternsRequestCondition(ROOT_PATH_PATTERNS);
}
else if (other.isEmptyPathMapping()) {
return this;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -52,8 +52,8 @@ public class PatternsRequestConditionTests {
PatternsRequestCondition c2 = new PatternsRequestCondition();
PatternsRequestCondition c3 = c1.combine(c2);
assertThat(c3).isSameAs(c1);
assertThat(c1.getPatterns()).isSameAs(c2.getPatterns()).containsExactly(this.parser.parse(""));
assertThat(c3.toString()).isEqualTo("[/ || ]");
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -73,7 +73,7 @@ public class RequestMappingInfoTests {
assertThat(info.getCustomCondition()).isSameAs(anotherInfo.getCustomCondition());
RequestMappingInfo result = info.combine(anotherInfo);
assertThat(info.getPatternsCondition()).isSameAs(result.getPatternsCondition());
assertThat(result.getPatternsCondition().toString()).isEqualTo("[/ || ]");
assertThat(info.getMethodsCondition()).isSameAs(result.getMethodsCondition());
assertThat(info.getParamsCondition()).isSameAs(result.getParamsCondition());
assertThat(info.getHeadersCondition()).isSameAs(result.getHeadersCondition());