I have created some DTO classes through Swaggerhub, generating from a REST API. There are Validation annotations as necessary in the generated DTO classes, and these do work for other data types than Boolean. (So the rest of the required annotations, and configs are in place)
The problem is, that the generated class has a property accessor properly named like isBlack()
, and this method is annotated as @NotNull
.
@NotNull
public Boolean isBlack() {
return black;
}
The class having null
in the property passes through the REST controller method's @Valid
endpoint without error message.
After experimenting how to find a workaround to the problem I added another accessor method to this field like
@NotNull
public Boolean getBlack() {
return black;
}
With this addition, the null value was properly captured, and returned with validation error message. I am pretty sure, this is a bug in Spring code, which does not look for methods starting with 'is' instead of the regular 'get' for property accessors.
Comment From: snicoll
I've moved this issue to the Spring Framework issue tracker as Spring Boot is not involved here.
I am pretty sure, this is a bug in Spring code
I don't think it is. The JavaBean specification states (section 8.3.2) that
In addition, for boolean properties, we allow a getter method to match the pattern: public boolean is
();
The black
property uses the wrapper type. As such, it should be getBlack
, not isBlack
. If you switch the property type from Boolean
to boolean
, isBlack
should be detected.