Skip to content

Method validation with @Validated not working [SPR-17369] #21902

@spring-projects-issues

Description

@spring-projects-issues

blaugold opened SPR-17369 and commented

I would like to use validation groups in my service layer to validate dtos, but am not able to trigger validation exceptions when using @Validated on a method parameter. @Valid works, but to use groups I need @Validated to work. I have isolated the issue in a reproduction repo which includes these minimal classes:

class Obj(

        @field:NotNull
        var default: String? = "",

        @field:NotNull(groups = [GroupA::class])
        var groupA: String? = ""

) {

    interface GroupA

    companion object {
        val Valid = Obj()
        val Invalid = Obj(null, null)
        val DefaultInvalid = Obj(null, "")
        val GroupAInvalid = Obj("", null)
    }

}

@Service
@Validated
class Service {

    fun plainMethod(obj: Obj) {}

    fun validMethod(@Valid obj: Obj) {}

    fun validatedMethod(@Validated obj: Obj) {}

    fun groupValidatedMethod(@Validated(Obj.GroupA::class) obj: Obj) {}

}

Service.validMethod throws as expected if obj is invalid (for default group constraints), but the methods where @Validated is used do not seem to validated obj at all.

I was not able to find any examples where @Validated is used in a service, only in controllers, but from the docs I got the impression this should work.


Affects: 5.0.9

Reference URL: https://github.com/blaugold/spring-method-validation-issue

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: declinedA suggestion or change that we don't feel we should currently apply

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions