Deprecate pathExtension routing predicates

Closes gh-34103
This commit is contained in:
rstoyanchev
2024-12-18 15:43:28 +00:00
parent 62d9600cdd
commit 41a9db376d
16 changed files with 108 additions and 17 deletions

View File

@@ -269,7 +269,12 @@ public abstract class RequestPredicates {
* Return a {@code RequestPredicate} that matches if the request's path has the given extension.
* @param extension the path extension to match against, ignoring case
* @return a predicate that matches if the request's path has the given file extension
* @deprecated without replacement to discourage use of path extensions for request
* mapping and for content negotiation (with similar deprecations and removals already
* applied to annotated controllers). For further context, please read issue
* <a href="https://github.com/spring-projects/spring-framework/issues/24179">#24179</a>
*/
@Deprecated(since = "7.0", forRemoval = true)
public static RequestPredicate pathExtension(String extension) {
Assert.notNull(extension, "'extension' must not be null");
return new PathExtensionPredicate(extension);
@@ -281,7 +286,12 @@ public abstract class RequestPredicates {
* @param extensionPredicate the predicate to test against the request path extension
* @return a predicate that matches if the given predicate matches against the request's path
* file extension
* @deprecated without replacement to discourage use of path extensions for request
* mapping and for content negotiation (with similar deprecations and removals already
* applied to annotated controllers). For further context, please read issue
* <a href="https://github.com/spring-projects/spring-framework/issues/24179">#24179</a>
*/
@Deprecated(since = "7.0", forRemoval = true)
public static RequestPredicate pathExtension(Predicate<String> extensionPredicate) {
return new PathExtensionPredicate(extensionPredicate);
}
@@ -352,7 +362,12 @@ public abstract class RequestPredicates {
* Receive notification of a path extension predicate.
* @param extension the path extension that makes up the predicate
* @see RequestPredicates#pathExtension(String)
* @deprecated without replacement to discourage use of path extensions for request
* mapping and for content negotiation (with similar deprecations and removals already
* applied to annotated controllers). For further context, please read issue
* <a href="https://github.com/spring-projects/spring-framework/issues/24179">#24179</a>
*/
@Deprecated(since = "7.0", forRemoval = true)
void pathExtension(String extension);
/**
@@ -814,6 +829,7 @@ public abstract class RequestPredicates {
}
@Deprecated(since = "7.0", forRemoval = true)
private static class PathExtensionPredicate implements RequestPredicate {
private final Predicate<String> extensionPredicate;

View File

@@ -101,6 +101,8 @@ class ToStringVisitor implements RouterFunctions.Visitor, RequestPredicates.Visi
this.builder.append(pattern);
}
@SuppressWarnings("removal")
@Deprecated(since = "7.0", forRemoval = true)
@Override
public void pathExtension(String extension) {
this.builder.append(String.format("*.%s", extension));

View File

@@ -549,6 +549,11 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD
* Route to the given handler function if the given pathExtension predicate applies.
* @see RouterFunctions.route
*/
@Suppress("removal", "DEPRECATION")
@Deprecated("without replacement to discourage use of path extensions for request mapping and for" +
"content negotiation (with similar deprecations and removals already applied to" +
"annotated controllers). For further context, please read issue " +
"https://github.com/spring-projects/spring-framework/issues/24179", replaceWith = ReplaceWith("None"))
fun pathExtension(extension: String, f: (ServerRequest) -> ServerResponse) {
builder.add(RouterFunctions.route(RequestPredicates.pathExtension(extension), HandlerFunction(f)))
}
@@ -558,12 +563,22 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD
* @param extension the path extension to match against, ignoring case
* @return a predicate that matches if the request's path has the given file extension
*/
@Suppress("removal", "DEPRECATION")
@Deprecated("without replacement to discourage use of path extensions for request mapping and for" +
"content negotiation (with similar deprecations and removals already applied to" +
"annotated controllers). For further context, please read issue " +
"https://github.com/spring-projects/spring-framework/issues/24179", replaceWith = ReplaceWith("None"))
fun pathExtension(extension: String): RequestPredicate = RequestPredicates.pathExtension(extension)
/**
* Route to the given handler function if the given pathExtension predicate applies.
* @see RouterFunctions.route
*/
@Suppress("removal", "DEPRECATION")
@Deprecated("without replacement to discourage use of path extensions for request mapping and for" +
"content negotiation (with similar deprecations and removals already applied to" +
"annotated controllers). For further context, please read issue " +
"https://github.com/spring-projects/spring-framework/issues/24179", replaceWith = ReplaceWith("None"))
fun pathExtension(predicate: (String?) -> Boolean, f: (ServerRequest) -> ServerResponse) {
builder.add(RouterFunctions.route(RequestPredicates.pathExtension(predicate), HandlerFunction(f)))
}
@@ -573,6 +588,11 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD
* predicate.
* @see RequestPredicates.pathExtension
*/
@Suppress("removal", "DEPRECATION")
@Deprecated("without replacement to discourage use of path extensions for request mapping and for" +
"content negotiation (with similar deprecations and removals already applied to" +
"annotated controllers). For further context, please read issue " +
"https://github.com/spring-projects/spring-framework/issues/24179", replaceWith = ReplaceWith("None"))
fun pathExtension(predicate: (String?) -> Boolean): RequestPredicate =
RequestPredicates.pathExtension(predicate)

View File

@@ -223,6 +223,7 @@ class RequestPredicatesTests {
assertThat(predicate.test(request)).isFalse();
}
@SuppressWarnings("removal")
@Test
void pathExtension() {
RequestPredicate predicate = RequestPredicates.pathExtension("txt");
@@ -237,6 +238,7 @@ class RequestPredicatesTests {
assertThat(predicate.test(initRequest("GET", "/file"))).isFalse();
}
@SuppressWarnings("removal")
@Test
void pathExtensionPredicate() {
List<String> extensions = List.of("foo", "bar");

View File

@@ -29,7 +29,6 @@ import static org.springframework.web.servlet.function.RequestPredicates.method;
import static org.springframework.web.servlet.function.RequestPredicates.methods;
import static org.springframework.web.servlet.function.RequestPredicates.param;
import static org.springframework.web.servlet.function.RequestPredicates.path;
import static org.springframework.web.servlet.function.RequestPredicates.pathExtension;
import static org.springframework.web.servlet.function.RouterFunctions.route;
/**
@@ -61,6 +60,7 @@ class ToStringVisitorTests {
assertThat(result).isEqualTo(expected);
}
@SuppressWarnings("removal")
@Test
void predicates() {
testPredicate(methods(HttpMethod.GET), "GET");
@@ -68,8 +68,6 @@ class ToStringVisitorTests {
testPredicate(path("/foo"), "/foo");
testPredicate(pathExtension("foo"), "*.foo");
testPredicate(contentType(MediaType.APPLICATION_JSON), "Content-Type: application/json");
ToStringVisitor visitor = new ToStringVisitor();

View File

@@ -192,6 +192,7 @@ class RouterFunctionDslTests {
null
}
}
@Suppress("DEPRECATION")
GET(pathExtension { it == "properties" }) {
ok().body("foo=bar")
}