spring-framework icon indicating copy to clipboard operation
spring-framework copied to clipboard

Exception for validation failure in WebFlux does not match the documentation

Open xinxiong-tw opened this issue 1 year ago • 8 comments

Affects: <Spring Framework 6.1.8 and maybe other versions>


I write a simple demo below, I write a Controller with a handler method receiving a post request and consume a valid request body

@RestController
class Controller {
    @PostMapping("/test")
    fun test(@RequestBody @Valid foo: Foo) {
        println(foo)
    }

    @ExceptionHandler(Exception::class)
    fun handleException(e: Exception) {
        println(e)
    }
}

data class Foo(
    @field:NotEmpty
    val name: String
)

Then I send a post request with this body to the application.

{
  "name": ""
}

Check out the console and you can see a exception printed and it's type is WebExchangeBindException But according to the spring document validation, it should throw a MethodArgumentNotValidException. image

I also test this behavior in mvc and it's the expected exception.

xinxiong-tw avatar Jun 18 '24 06:06 xinxiong-tw

Check out the console

It looks like you'd like us to run the example to actually see the error. Please don't paste code in text like that as the only choice we have is to copy it in a small project created from start.spring.io in the hope we're not missing another piece you're not showing. I haven't looked at the actual code, but please edit your description to replace that with a reference to an actual sample. You can attach a zip to this issue or push the code to a GitHub repository.

snicoll avatar Jun 18 '24 07:06 snicoll

spring-kotlin-webflux-playground.zip @snicoll Sorry, here is the code in zip.

xinxiong-tw avatar Jun 18 '24 14:06 xinxiong-tw

Thanks. I've reproduced what you've described, we'll look into it.

snicoll avatar Jun 18 '24 15:06 snicoll

Hi @snicoll I would like to begin contributing to spring framework. Do you mind assigning this to me?

imvtsl avatar Jun 19 '24 23:06 imvtsl

@imvtsl thanks for the offer but I am not sure yet what we want to do for this. While the issue title makes it sound like a documentation change, it might not be. You can see this by the "waiting-for-triage" label that means the team hasn't really determined the nature of the issue yet.

snicoll avatar Jun 20 '24 04:06 snicoll

@snicoll @imvtsl

While the issue title makes it sound like a documentation change

Maybe I didn't make it clean, IMO I would like this exception to be type of MethodArgumentNotValidException. Because in webmvc it's this exception type and align with the doc.

xinxiong-tw avatar Jun 20 '24 09:06 xinxiong-tw

I understood what you reported but I am not sure what we should do. @rstoyanchev should know.

snicoll avatar Jun 22 '24 09:06 snicoll

WebExchangeBindException is the expected exception for WebFlux. It is a ResponseStatusException as other exceptions in WebFlux and BindingResult is implemented as an interface only. MethodArgumentNotValidException on the other hand extends BindException and inherits the BindingResult implementation. In short, I confirm that it is a documentation issue.

rstoyanchev avatar Jun 24 '24 11:06 rstoyanchev