Search code examples
hibernate-validatorswagger-codegen

Should Hibernate-Validator ignore Boolean (object) getters when prefixed with "is" rather than "get"?


public class YourAnnotatedBean {

private Boolean activated;

@Valid
@NotNull
public Boolean isActivated() {
    return activated;
}

public void setOn(Boolean activated) {
    this.activated = activated;
}

}

Hibernate-validator seems to ignore @NotNull when it is on a Boolean getter called isActivated. If I rename it to getActivated, the constraint is enforced. I would just rename the getter, but this is coming from swagger-codegen.

My question is whether this is intentional or a bug? As far as I can tell, the JavaBeans spec doesn't mention Boolean wrapper objects. If this is not a bug, we'll need to update swagger-codegen to not use the "is" prefix for Booleans.


Solution

  • In the Java Bean convention, "is" is a valid prefix only for a primitive boolean.

    Here you have a Boolean so it should really be getActivated(). That's why HV ignores it.

    Any chance you could add the constraint to the property instead of the getter?

    We have this long term project of allowing alternative property selectors but, for now, it's just a project (see https://hibernate.atlassian.net/browse/HV-1363).