Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

iimik
Copy link

@iimik iimik commented Mar 16, 2023

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.

@hibernate-github-bot
Copy link

hibernate-github-bot bot commented Mar 16, 2023

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: [0d16b27]

› This message was automatically generated.

@iimik iimik force-pushed the main branch 2 times, most recently from 750077e to 677982b Compare March 16, 2023 08:19
@marko-bekhta
Copy link
Member

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.

@iimik
Copy link
Author

iimik commented Mar 17, 2023

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

@gsmet
Copy link
Member

gsmet commented Jun 16, 2023

@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!

@iimik
Copy link
Author

iimik commented Jun 16, 2023

@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.

Copy link
Member

@gsmet gsmet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm OK to incorporate this change but we need a test added.

Thanks!

@gsmet
Copy link
Member

gsmet commented Jun 20, 2023

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.

@iimik
Copy link
Author

iimik commented Jun 23, 2023

I had add some test for DefaultGroupSequenceProvider.

@iimik iimik requested a review from gsmet January 19, 2024 03:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants