Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-17369

Method validation with @Validated not working

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Minor
    • Resolution: Works as Designed
    • Affects Version/s: 5.0.9
    • Fix Version/s: None
    • Component/s: Core
    • Labels:

      Description

      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.

        Attachments

          Activity

            People

            Assignee:
            sdeleuze Sébastien Deleuze
            Reporter:
            blaugold blaugold
            Last updater:
            Spring Issues Spring Issues
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Days since last comment:
              3 years, 24 weeks, 1 day ago