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
@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.
Issue raised: https://jira.spring.io/browse/SPR-16297
@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
@field:Email
to tell Kotlin the annotation should be applied to fields rather than constructor parameters@Email val email: String
to class body, in {
and }
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
@field:Email
to tell Kotlin the annotation should be applied to fields rather than constructor parameters@Email val email: String
to class body, in{
and}