Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Kotlin + bean validation documentation [SPR-16701] #21242

spring-projects-issues opened this issue Apr 8, 2018 · 1 comment


Copy link

@spring-projects-issues spring-projects-issues commented Apr 8, 2018

Maciej Sawicki opened SPR-16701 and commented

GitHub example project with tests
Stackoverflow question

When I define a class implementing CustomValidator and it uses an annotation, for example ValidateRoles, which is defined in Kotlin, then the field validation with this annotation doesn't work. Whereas when I define the same annotation in Java the field is validated correctly.

Consider these 2 the same annotations. One is in Java and one is in Kotlin.

@Constraint(validatedBy = RolesValidatorJava.class)
public @interface ValidateRolesJava {
    String message() default "{com.example.demo.validators.ValidateRolesJava.message}";

    Class[] groups() default {};

    Class[] payload() default {};

@Constraint(validatedBy = arrayOf(RolesValidatorKotlin::class))
@Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY)
annotation class ValidateRolesKotlin(
    val message: String = "{com.example.demo.validators.ValidateRolesKotlin.message}",
    val groups: Array<KClass<*>> = arrayOf(),
    val payload: Array<KClass<*>> = arrayOf()

Consider 2 Validators one for each Annotation, each returning false upon validation

public class RolesValidatorJava implements ConstraintValidator<ValidateRolesJava, Collection<String>> {
    public boolean isValid(Collection<String> value, ConstraintValidatorContext context) {
        return false;

class RolesValidatorKotlin : ConstraintValidator<ValidateRolesKotlin, Collection<String>> {
    override fun isValid(value: Collection<String>?, context: ConstraintValidatorContext?): Boolean {
        return false

Now we have 2 the same request bodies, but with different annotations

class JavaBodyDTO {
    var roles: Set<String> = emptySet()

class KotlinBodyDTO {
    var roles: Set<String> = emptySet()

Each response should return 400

fun updateRolesJava(@RequestBody @Valid body: JavaBodyDTO): ResponseEntity<Void> {
    // this should never reach because validator invalidates body
    return ResponseEntity(HttpStatus.OK)

fun updateRolesKotlin(@RequestBody @Valid body: KotlinBodyDTO): ResponseEntity<Void> {
    // this should never reach because validator invalidates body, but the validation doesn't work
    return ResponseEntity(HttpStatus.OK)

Affects: 5.0.5

Reference URL:

Referenced from: commits e4298e8, 1be5855

Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Apr 9, 2018

Sébastien Deleuze commented

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

class KotlinBodyDTO {
    var roles: Set<String> = emptySet()

I have updated the reference documentation, which was mentioning this about primary constructor parameters, accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants