-
Notifications
You must be signed in to change notification settings - Fork 38.7k
Description
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