Skip to content

Fix Kotlin filter parameter bug in Router DSLs #26838

@ShindongLee

Description

@ShindongLee

fun filter(filterFunction: (ServerRequest, (ServerRequest) -> ServerResponse) -> ServerResponse) {
builder.filter { request, next ->
filterFunction(request) {
next.handle(request)
}
}

fun filter(filterFunction: suspend (ServerRequest, suspend (ServerRequest) -> ServerResponse) -> ServerResponse) {
builder.filter { serverRequest, handlerFunction ->
mono(Dispatchers.Unconfined) {
filterFunction(serverRequest) {
handlerFunction.handle(serverRequest).awaitSingle()
}
}
}
}

  1. Function filterFunction takes serverRequest and function handler as parameters. (I just name it second parameter of filterFunction as handler.)
  2. Function handler also takes serverRequest as parameter.
  3. These DSLs define handler.
  4. Function handlers that these DSLs defined are now using mistakenly filterFunction's serverRequest, not their own serverRequest parameters, which makes their own parameters meaningless.

This bug only exists in Kotlin DSL.

You can see java codes have no problem as follows.

R filter(ServerRequest request, HandlerFunction<T> next) throws Exception;

In java you can override filter something like

ServerRequest newRequest = someOperation(request)
return next.handle(newRequest)

but in Kotlin DSL, even if you write

filter { serverRequest, handlerFunction ->
    val newServerRequest = someOperation(serverRequest)
    handlerFunction(newServerRequest)
}

handlerFunction will use original serverRequest, not the newServerRequest

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: backportedAn issue that has been backported to maintenance branchestype: bugA general bug

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions