Bean Validation Group inheritance isn't working with Group Sequence Provider

六眼飞鱼酱① 提交于 2019-12-23 05:23:26

问题


I'm currently working on implementing an application that stores contact information which contains both foreign and domestic addresses, but the addresses can also be disabled.

I have attempted to define three validation groups: a super group for the always validated info and two child classes for the different address states.

public interface ContactValidation {}
public interface DomesticValidation extends ContactValidation {}
public interface ForeignValidation extends ContactValidation {}

I then defined an @GroupSequenceProvider on the contact class.

public class ContactGroupSequenceProvider implements DefaultGroupSequenceProvider<Contact> {

@Override
public List<Class<?>> getValidationGroups(Contact contact) {
    List<Class<?>> defaultGroupSequence = new ArrayList<Class<?>>();

    if(contact.getIsForeign()){
        defaultGroupSequence.add(Contact.ForeignValidation.class);
    } else {
        defaultGroupSequence.add(Contact.DomesticValidation.class);
    } //TODO: Add Address Disabled Case

    defaultGroupSequence.add(Contact.class); //Get a GroupDefinitionException without this line

    return defaultGroupSequence;
}

When I run the program with either address validation, the fields marked with groups=Contact.ContactValidation.class are not being validated even though it is a super class of the address validations.

I need this application to validate together so the used doesn't have to fix an error, submit and get the new errors.

I also would like to avoid solutions with the always validated fields being like this.

@NotBlank(groups={DomesticValidation.class,ForeignValidation.class ,ContactValidation.class})   
private String fullContactName;

Any advice appreciated!


回答1:


If you return one of the sub-classes, I'd also expect the constraints tagged with ContactValidation to be validated. This seems to be a bug in Hibernate Validator, so could you open a ticket in our issue tracker?

As a work-around you might try to explicitly add ContactValidation to the list you return.



来源:https://stackoverflow.com/questions/24168964/bean-validation-group-inheritance-isnt-working-with-group-sequence-provider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!