Skip to content

Built-in method validation validates @RequestBody parameter twice #31711

Closed
@giger85

Description

@giger85

Version

  • spring framework: 6.1.1 (spring boot 3.2.0)

Situation

I tested below spring MVC rest controller and model. (use kotlin)

@RestController
@RequestMapping(value = ["/api"])
class FooController {
    @PostExchange("/foo")
    fun createFoo(
        @RequestBody @Valid myData: MyData,
        @Min(1) @RequestParam("hint") hint: Int
    ): Map<String, Any?> {
        return mapOf("myData" to myData)
    }
}

data class MyData(
    val name: String,
    @get:Min(1)
    val age: Int
)

And call api and debug.

> curl -X POST \
-H 'Content-Type: application/json' \
'http://localhost:8080/api/foo?hint=2' \
-d '{"name": "foo-1", "age": 3}'

Then, the validation of age field occurs twice.
Is this behavior valid?

Related code

In invokeForRequest method, executed getMethodArgumentValues method first and then methodValidator.applyArgumentValidation method.

If requestBody is validate object and built-in method validation is activated, then request body object is validated twice.

Object[] args = getMethodArgumentValues(request, mavContainer, providedArgs);

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions