Skip to content

Commit

Permalink
Deprecate pathExtension routing predicates
Browse files Browse the repository at this point in the history
Closes gh-34103
  • Loading branch information
rstoyanchev committed Dec 19, 2024
1 parent 62d9600 commit 41a9db3
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 17 deletions.
7 changes: 2 additions & 5 deletions framework-docs/modules/ROOT/pages/web/webflux-functional.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -803,8 +803,7 @@ Java::
[source,java,indent=0,subs="verbatim,quotes"]
----
ClassPathResource index = new ClassPathResource("static/index.html");
List<String> extensions = List.of("js", "css", "ico", "png", "jpg", "gif");
RequestPredicate spaPredicate = path("/api/**").or(path("/error")).or(pathExtension(extensions::contains)).negate();
RequestPredicate spaPredicate = path("/api/**").or(path("/error")).negate();
RouterFunction<ServerResponse> redirectToIndex = route()
.resource(spaPredicate, index)
.build();
Expand All @@ -816,9 +815,7 @@ Kotlin::
----
val redirectToIndex = router {
val index = ClassPathResource("static/index.html")
val extensions = listOf("js", "css", "ico", "png", "jpg", "gif")
val spaPredicate = !(path("/api/**") or path("/error") or
pathExtension(extensions::contains))
val spaPredicate = !(path("/api/**") or path("/error"))
resource(spaPredicate, index)
}
----
Expand Down
6 changes: 2 additions & 4 deletions framework-docs/modules/ROOT/pages/web/webmvc-functional.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ Java::
----
ClassPathResource index = new ClassPathResource("static/index.html");
List<String> extensions = List.of("js", "css", "ico", "png", "jpg", "gif");
RequestPredicate spaPredicate = path("/api/**").or(path("/error")).or(pathExtension(extensions::contains)).negate();
RequestPredicate spaPredicate = path("/api/**").or(path("/error")).negate();
RouterFunction<ServerResponse> redirectToIndex = route()
.resource(spaPredicate, index)
.build();
Expand All @@ -794,9 +794,7 @@ Kotlin::
----
val redirectToIndex = router {
val index = ClassPathResource("static/index.html")
val extensions = listOf("js", "css", "ico", "png", "jpg", "gif")
val spaPredicate = !(path("/api/**") or path("/error") or
pathExtension(extensions::contains))
val spaPredicate = !(path("/api/**") or path("/error"))
resource(spaPredicate, index)
}
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,12 @@ public static RequestPredicate OPTIONS(String pattern) {
* 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);
Expand All @@ -282,7 +287,12 @@ public static RequestPredicate pathExtension(String extension) {
* @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);
}
Expand Down Expand Up @@ -354,7 +364,12 @@ public interface Visitor {
* 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);

/**
Expand Down Expand Up @@ -816,6 +831,7 @@ public String toString() {
}


@Deprecated(since = "7.0", forRemoval = true)
private static class PathExtensionPredicate implements RequestPredicate {

private final Predicate<String> extensionPredicate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public void path(String pattern) {
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,11 @@ class CoRouterFunctionDsl internal constructor (private val init: (CoRouterFunct
* Route to the given handler function if the given pathExtension predicate applies.
* @see RouterFunctions.route
*/
@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"))
@Suppress("removal", "DEPRECATION")
fun pathExtension(extension: String, f: suspend (ServerRequest) -> ServerResponse) {
builder.add(RouterFunctions.route(RequestPredicates.pathExtension(extension), asHandlerFunction(f)))
}
Expand All @@ -516,21 +521,35 @@ class CoRouterFunctionDsl internal constructor (private val init: (CoRouterFunct
* @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: suspend (ServerRequest) -> ServerResponse) {
builder.add(RouterFunctions.route(RequestPredicates.pathExtension(predicate), asHandlerFunction(f)))
}

/**
* Return a [RequestPredicate] that matches if the request's path matches the given
* 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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,11 @@ class RouterFunctionDsl internal constructor (private val init: RouterFunctionDs
* 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) -> Mono<out ServerResponse>) {
builder.add(RouterFunctions.route(RequestPredicates.pathExtension(extension)) { f(it).cast(ServerResponse::class.java) })
}
Expand All @@ -563,21 +568,35 @@ class RouterFunctionDsl internal constructor (private val init: RouterFunctionDs
* @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) -> Mono<out ServerResponse>) {
builder.add(RouterFunctions.route(RequestPredicates.pathExtension(predicate)) { f(it).cast(ServerResponse::class.java) })
}

/**
* Return a [RequestPredicate] that matches if the request's path matches the given
* 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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ void multipleAccepts() {
assertThat(predicate.test(request)).isFalse();
}

@SuppressWarnings("removal")
@Test
void pathExtension() {
RequestPredicate predicate = RequestPredicates.pathExtension("txt");
Expand All @@ -319,6 +320,7 @@ void pathExtension() {
assertThat(predicate.test(request)).isFalse();
}

@SuppressWarnings("removal")
@Test
void pathExtensionPredicate() {
List<String> extensions = List.of("foo", "bar");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import static org.springframework.web.reactive.function.server.RequestPredicates.method;
import static org.springframework.web.reactive.function.server.RequestPredicates.methods;
import static org.springframework.web.reactive.function.server.RequestPredicates.path;
import static org.springframework.web.reactive.function.server.RequestPredicates.pathExtension;
import static org.springframework.web.reactive.function.server.RequestPredicates.queryParam;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

Expand Down Expand Up @@ -69,8 +68,6 @@ void predicates() {

testPredicate(path("/foo"), "/foo");

testPredicate(pathExtension("foo"), "*.foo");

testPredicate(contentType(MediaType.APPLICATION_JSON), "Content-Type: application/json");

ToStringVisitor visitor = new ToStringVisitor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ class CoRouterFunctionDslTests {
null
}
}
@Suppress("DEPRECATION")
GET(pathExtension { it == "properties" }) {
ok().bodyValueAndAwait("foo=bar")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ class RouterFunctionDslTests {
Mono.empty()
}
}
@Suppress("DEPRECATION")
GET(pathExtension { it == "properties" }) {
ok().bodyValue("foo=bar")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,12 @@ public static RequestPredicate OPTIONS(String pattern) {
* 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);
Expand All @@ -281,7 +286,12 @@ public static RequestPredicate pathExtension(String extension) {
* @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);
}
Expand Down Expand Up @@ -352,7 +362,12 @@ public interface Visitor {
* 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);

/**
Expand Down Expand Up @@ -814,6 +829,7 @@ public String toString() {
}


@Deprecated(since = "7.0", forRemoval = true)
private static class PathExtensionPredicate implements RequestPredicate {

private final Predicate<String> extensionPredicate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public void path(String pattern) {
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
Expand All @@ -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)))
}
Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ void multipleAccepts() {
assertThat(predicate.test(request)).isFalse();
}

@SuppressWarnings("removal")
@Test
void pathExtension() {
RequestPredicate predicate = RequestPredicates.pathExtension("txt");
Expand All @@ -237,6 +238,7 @@ void pathExtension() {
assertThat(predicate.test(initRequest("GET", "/file"))).isFalse();
}

@SuppressWarnings("removal")
@Test
void pathExtensionPredicate() {
List<String> extensions = List.of("foo", "bar");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
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;

/**
Expand Down Expand Up @@ -61,15 +60,14 @@ void nested() {
assertThat(result).isEqualTo(expected);
}

@SuppressWarnings("removal")
@Test
void predicates() {
testPredicate(methods(HttpMethod.GET), "GET");
testPredicate(methods(HttpMethod.GET, HttpMethod.POST), "[GET, POST]");

testPredicate(path("/foo"), "/foo");

testPredicate(pathExtension("foo"), "*.foo");

testPredicate(contentType(MediaType.APPLICATION_JSON), "Content-Type: application/json");

ToStringVisitor visitor = new ToStringVisitor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ class RouterFunctionDslTests {
null
}
}
@Suppress("DEPRECATION")
GET(pathExtension { it == "properties" }) {
ok().body("foo=bar")
}
Expand Down

0 comments on commit 41a9db3

Please sign in to comment.