so first of all i could not think of a better title for this question so i'm open for changes.
I am trying to validate a bean using the bean validation mechanism (JSR-380) with spring boot.
所以我有一个这样的控制器:
@Controller
@RequestMapping("/users")
class UserController {
@PostMapping
fun createUser(@Valid user: User, bindingResult: BindingResult): ModelAndView {
return ModelAndView("someview", "user", user)
}
}
with this being the User class written in kotlin:
data class User(
@field:NotEmpty
var roles: MutableSet<@NotNull Role> = HashSet()
)
这就是测试:
@Test
internal fun shouldNotCreateNewTestWithInvalidParams() {
mockMvc.perform(post("/users")
.param("roles", "invalid role"))
.andExpect(model().attributeHasFieldErrors("user", "roles[]"))
}
Invalid Roles are mapped to null.
As you can see i want roles
to contain at least one item with none of the items being null.
However when testing the above code no binding errors are reported if roles
contains null values. It does report an error if the set is empty though.
I was thinking that this might be an issue with how kotlin code compiles as the same code works just fine when the User class is written in java. Like this:
@Data // just lombok...
public class User {
@NotEmpty
private Set<@NotNull Role> roles = new HashSet<>();
}
Same Controller, same test.
在判断字节码之后,我注意到kotlin版本不包括嵌套的@NotNull
注释(见下文).
Java:
private Ljava/util/Set; roles
@Ljavax/validation/constraints/NotEmpty;()
@Ljavax/validation/constraints/NotNull;() : FIELD, 0;
@Ljavax/validation/constraints/NotEmpty;() : FIELD, null
Kotlin :
private Ljava/util/Set; roles
@Ljavax/validation/constraints/NotEmpty;()
@Lorg/jetbrains/annotations/NotNull;() // added because roles is not nullable in kotlin. this does not affect validation
Now the question is why?
Here's a 100 in case you want to try some stuff.