hibernate-validator icon indicating copy to clipboard operation
hibernate-validator copied to clipboard

HV-1942 Update DefaultGroupSequenceProvider add default method provide the class of instance

Open iimik opened this issue 2 years ago • 7 comments

The provider can not get current class of instance when the instance is null When use a common DefaultGroupSequenceProvider for multi class. And the method of BeanMetaDataImpl#getValidDefaultGroupSequence checked must have the special group of beanClass.getName() and also can not return the default group of Default.class.

iimik avatar Mar 16 '23 08:03 iimik

Thanks for your pull request!

This pull request does not follow the contribution rules. Could you have a look?

❌ All commit messages should start with a JIRA issue key matching pattern HV-\d+     ↳ Offending commits: [0d16b27e7b04342dbc574ceac9182d1045fe679f]

› This message was automatically generated.

Hey @ilikly, thanks for submitting a pull request. Could you please provide a test case showing a problem you are trying to solve with these changes? Since the group sequence is resolved at runtime based on a type of a bean under validation, if an object passed to the DefaultGroupSequenceProvider is null, there's no point in continuing validation, and a provider can just return null.

But if you are trying to use Validator#validateValue(..) and that's where you get the null, then it is somewhat by design.

marko-bekhta avatar Mar 17 '23 09:03 marko-bekhta

Hey @ilikly, thanks for submitting a pull request. Could you please provide a test case showing a problem you are trying to solve with these changes? Since the group sequence is resolved at runtime based on a type of a bean under validation, if an object passed to the DefaultGroupSequenceProvider is null, there's no point in continuing validation, and a provider can just return null.

But if you are trying to use Validator#validateValue(..) and that's where you get the null, then it is somewhat by design.

But if the prodiver just return null will be thrown an exception of GroupDefinitionException at the method org.hibernate.validator.internal.metadata.aggregated.BeanMetaDataImpl#getValidDefaultGroupSequence image

iimik avatar Mar 17 '23 09:03 iimik

@ilikly please provide a sample project showing what you are trying to achieve and what doesn't work. It's hard to understand your requirement with the current details.

Thanks!

gsmet avatar Jun 16 '23 11:06 gsmet

@ilikly please provide a sample project showing what you are trying to achieve and what doesn't work. It's hard to understand your requirement with the current details.

Thanks!

public class Test {


    public static void main(String[] args) {
        final ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
        final Validator validator = validatorFactory.getValidator();
        A a = new A();
        validator.validate(a);
    }


    public static class GlobalDefaultGroupSequenceProvider implements DefaultGroupSequenceProvider<Object> {

        @Override
        public List<Class<?>> getValidationGroups(Object object) {
            List<Class<?>> groups = new ArrayList<>();

            if (object != null) {
                groups.add(object.getClass());
            }

            return groups;
        }
    }

    @Setter
    @Getter
    @GroupSequenceProvider(GlobalDefaultGroupSequenceProvider.class)
    public static class A {
        @NotNull
        private String name;
    }

    @Setter
    @Getter
    @GroupSequenceProvider(GlobalDefaultGroupSequenceProvider.class)
    public static class B {
        @NotNull
        private String name;
    }
}


The parameter in callback of method getValidationGroups is null, so the GlobalDefaultGroupSequenceProvider can not provide the Default group for A or B class.

iimik avatar Jun 16 '23 11:06 iimik

That being said, I'm not sure I understand when having a specific group sequence provider make sense if the bean class is actually null? I would expect it to not be validated and that having specific groups wouldn't make any difference?

So if you contribute a test case, make sure I can see why it makes sense to actually have this feature.

Thanks.

gsmet avatar Jun 20 '23 16:06 gsmet

I had add some test for DefaultGroupSequenceProvider.

iimik avatar Jun 23 '23 05:06 iimik