Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webflux fails to apply the rule for controller methods returning void to kotlin suspend functions returning Unit #27629

Closed
rogeriomgatto opened this issue Nov 1, 2021 · 2 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) theme: kotlin An issue related to Kotlin support type: bug A general bug
Milestone

Comments

@rogeriomgatto
Copy link

rogeriomgatto commented Nov 1, 2021

Affects: 5.3.12

Webflux fails to apply the rule for controller methods returning void to suspend functions returning Unit. Code like the example below causes error messages in log: ERROR o.s.w.s.a.HttpWebHandlerAdapter - [882bce24-1] Error [java.lang.UnsupportedOperationException] for HTTP POST "/some/api/path", but ServerHttpResponse already committed (200 OK)

@PostMapping
suspend fun doSomething(response: ServerHttpResponse): Unit {
    // ...
    response.writeWith(/* ... */).awaitSingleOrNull()
}

Cause

InvocableHandlerMethod::invoke wraps coroutine results with either mono or flux using CoroutinesUtils.invokeSuspendingFunction, but getReturnType simply returns Void.class. This in turn causes reactiveAdapterRegistry.getAdapter to return null and isAsyncVoidReturnType to return false

Workaround

Remove suspend modifier and replicate CoroutinesUtils.invokeSuspendingFunction wrapping manually:

@PostMapping
fun doSomething(response: ServerHttpResponse) = mono(Dispatchers.Unconfined) {
    // ...
    response.writeWith(/* ... */).awaitSingleOrNull()
}
    .filter { false }
    .onErrorMap(InvocationTargetException::class.java, InvocationTargetException::getTargetException)
    .cast(Void.TYPE)
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 1, 2021
@rstoyanchev rstoyanchev added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Nov 8, 2021
@koenpunt
Copy link
Contributor

I'm experiencing the same behavior. The workaround works well.

@sdeleuze sdeleuze added the theme: kotlin An issue related to Kotlin support label Sep 14, 2022
@sdeleuze sdeleuze self-assigned this Sep 23, 2022
@sdeleuze sdeleuze added this to the 5.3.24 milestone Sep 23, 2022
@sdeleuze sdeleuze removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 23, 2022
@sdeleuze
Copy link
Contributor

Looks like something we should fix even in 5.3.x.

sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Nov 13, 2022
@sdeleuze sdeleuze added the type: bug A general bug label Nov 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) theme: kotlin An issue related to Kotlin support type: bug A general bug
Projects
None yet
Development

No branches or pull requests

5 participants