javaspringkotlinbean-validation

Spring does not create ConstraintValidator


Spring does not create an instance of class implementing ConstraintValidator. Even when I annotate RolesValidator with @Configuartion Spring create its instance, but validation doesn't work.

Here is my code:

@Constraint(validatedBy = [RolesValidator::class])
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.FIELD)
@Retention(AnnotationRetention.RUNTIME)
annotation class ValidateRoles(
    val message: String = "{com.app.authservice.validators.ValidateRoles.message}",
    val groups: Array<KClass<*>> = arrayOf(),
    val payload: Array<KClass<out Payload>> = arrayOf()
)

//------Validator------

class RolesValidator : ConstraintValidator<ValidateRoles, Collection<String>> {
    lateinit var allowedValues: List<String>

    override fun initialize(constraintAnnotation: ValidateRoles?) {
        allowedValues = RoleType.values().map { it.name }
    }

    override fun isValid(value: Collection<String>?, context: ConstraintValidatorContext?): Boolean {
        return allowedValues.containsAll(value!!)
    }
}

//------Usage------

class AccountUpdateRolesDTO {
    @NotNull
    @ValidateRoles
    var roles: Set<String> = emptySet()
}

//------Controller------

@PreAuthorize("hasAnyAuthority($_ADMIN)")
@Transactional
@PutMapping("/{accountId}/roles")
fun updateRoles(
    @RequestBody @Valid body: AccountUpdateRolesDTO,
    @PathVariable accountId: Long,
    jwt: JWTData
): ResponseEntity<Void> {
    return ResponseEntity(HttpStatus.OK)
}

@edit https://github.com/spring-projects/spring-framework/issues/21242 (formerly SPR-16701)


Solution

  • If you leverage annotation use-site targets, Kotlin tests pass as well:

    class KotlinBodyDTO {
        @field:NotNull
        @field:ValidateRolesKotlin
        var roles: Set<String> = emptySet()
    }