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

Closed
spring-issuemaster opened this issue Apr 8, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

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)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface ValidateRolesJava {
    String message() default "{com.example.demo.validators.ValidateRolesJava.message}";

    Class[] groups() default {};

    Class[] payload() default {};
}

@Constraint(validatedBy = arrayOf(RolesValidatorKotlin::class))
@Retention(AnnotationRetention.RUNTIME)
@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>> {
    @Override
    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 {
    @NotNull
    @ValidateRolesJava
    var roles: Set<String> = emptySet()
}

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

Each response should return 400

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

@PostMapping("/updateRolesKotlin")
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: https://github.com/Humberd/spring-annotation-kotlin

Referenced from: commits e4298e8, 1be5855

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Apr 9, 2018

Sébastien Deleuze commented

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

class KotlinBodyDTO {
    @field:NotNull
    @field:ValidateRolesKotlin
    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
You can’t perform that action at this time.