Spring-boot: Bean Validation on rest controller does not work with 2.0.0.M7 and Kotlin

Created on 13 Dec 2017  路  6Comments  路  Source: spring-projects/spring-boot

I made a test case: https://github.com/MrBuddyCasino/spring-validator-test

Execute the following test case: de.codecentric.controller.AccountControllerShould

Expected result:
the request should fail with a BadRequest, the unit test should fail

Actual result:
the request succeeds, the unit test fails

invalid

Most helpful comment

@MrBuddyCasino
This isn't an issue, two ways could get it work as I know
The reason it doesn't work, is that you put them in the primary constructor

  1. Use @field:Email to tell Kotlin the annotation should be applied to fields rather than constructor parameters
  2. Put @Email val email: String to class body, in { and }

All 6 comments

@MrBuddyCasino sorry but nothing indicates that's an issue with Spring Boot. Can you please rewrite your sample in Java first? For instance, this may be related.

I made some tests and this version works:

class CreateUserDTO {

    @Email
    var email: String? = null

    @Size(min = 8)
    var password: String? = null
}

This version does not:

class CreateUserDTO(
        @Email
        val email: String,

        @Size(min = 8)
        val password: String) {
}

This version does not:

data class CreateUserDTO(
        @Email
        val email: String,

        @Size(min = 8)
        val password: String) {
}

So it seems to be a problem with immutable Kotlin properties. Since Spring Boot 2 specifically advertises Kotlin support, shouldn't it be in your test stack?

Maybe but it has nothing to do with Spring Boot and that information was quite key, I wished you'd have shared it initially. If you believe you've found a bug with validation support with Kotlin, please raise a Spring Framework issue.

I just found that out. I'm not aware of the Spring Boot / Spring Framework boundaries work. I'll raise a separate issue.

@MrBuddyCasino
This isn't an issue, two ways could get it work as I know
The reason it doesn't work, is that you put them in the primary constructor

  1. Use @field:Email to tell Kotlin the annotation should be applied to fields rather than constructor parameters
  2. Put @Email val email: String to class body, in { and }
Was this page helpful?
0 / 5 - 0 ratings