Affects: Spring Boot 3.2.0-RC2, Spring Web 3.2.0-RC2, Kotlin 1.9.20
Issue
Using a Controller, with return type kotlin.Unit
, which uses directly HttpServletResponse
to write the response, adds unexpected bytes to the response. The following is a controller example where a file is read and written to the output directly:
@GetMapping(path = ["public/file.txt"])
fun getFile(
httpServletResponse: HttpServletResponse
) {
httpServletResponse.status = 200
httpServletResponse.outputStream.write(Files.readAllBytes(Path.of("src/main/resources/file.txt")))
}
The following shows the bytes expected vs current:
Current: [49, 50, 51, 52, 53, 54, 55, 56, 57, 123, 125]
Expected: [49, 50, 51, 52, 53, 54, 55, 56, 57]
123 and 125 bytes are {
and }
How to reproduce the issue
This is a Spring Boot project with a test that reproduces the issue.
Debugging
My guess would be: there is a new converter which is taking kotlin.Unit
and tries to serialize it, outputting {
and }
.
Comment From: quaff
It should be fixed at framework not boot side, I've created https://github.com/spring-projects/spring-framework/pull/31647 to handle this.
Comment From: quaff
@estigma88 You can add suspend
modifier to the method as workaround, don't forget adding dependency org.jetbrains.kotlinx:kotlinx-coroutines-reactor
Comment From: estigma88
@quaff Thanks for the suggestion, but does that work with virtual threads? We are using virtual threads heavily in the project
Comment From: quaff
@quaff Thanks for the suggestion, but does that work with virtual threads? We are using virtual threads heavily in the project
I'm not expert of kotlin, but I don't think it will conflict with virtual threads.
Comment From: sdeleuze
Thanks for catching this regression, this should now be fixed.
Comment From: estigma88
I confirm it is working with Spring Boot 3.2.0