-
Notifications
You must be signed in to change notification settings - Fork 570
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
Conversation
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 › This message was automatically generated. |
750077e
to
677982b
Compare
…e the class of instance
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? But if you are trying to use |
But if the prodiver just return |
@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 |
There was a problem hiding this 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!
engine/src/main/java/org/hibernate/validator/spi/group/DefaultGroupSequenceProvider.java
Outdated
Show resolved
Hide resolved
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. |
I had add some test for DefaultGroupSequenceProvider. |
Does a workaround exist for this please? |
Hey @valh1996 , It's been a while since I've looked at the group sequences... |
Hi @marko-bekhta To do this, I use @JsonView({View.Admin.class, View.Moderator.class})
@NotNull
private Date name; The problem is that validation takes place in all cases, so the So I also want to enable/disable validation rules based on user role using groups : @JsonView({View.Admin.class, View.Moderator.class})
@NotNull(groups = {View.Admin.class, View.Moderator.class})
private Date name; But to apply the right groups according to the user's role, I have to use an public class RoleBasedSequenceProvider implements DefaultGroupSequenceProvider<Object> {
@Override
public List<Class<?>> getValidationGroups(Object request) {
List<Class<?>> sequence = new ArrayList<>();
if (request == null) {
return sequence;
}
sequence.add(request.getClass());
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && authentication.getAuthorities() != null) {
Set<Class<?>> roleGroups = authentication.getAuthorities().stream()
.map(GrantedAuthority::getAuthority)
.map(ERole::valueOf)
.map(View.MAPPING::get)
.collect(Collectors.toSet());
sequence.addAll(roleGroups);
}
return sequence;
}
} Then i can add But as the PR indicates, it doesn't work to mark |
Hey, ok, this brings more clarity to what you are trying to achieve. See https://docs.jboss.org/hibernate/validator/9.0/api/org/hibernate/validator/spi/group/DefaultGroupSequenceProvider.html, the
So that means that the provider you are trying to implement:
should be used on an
and it means that you are working with a single DTO then just use that DTO instead of the public class RoleBasedSequenceProvider implements DefaultGroupSequenceProvider<MySingleDto> {
@Override
public List<Class<?>> getValidationGroups(MySingleDto request) {
List<Class<?>> sequence = new ArrayList<>();
sequence.add(MySingleDto.class);
// other stuff ....
return sequence;
}
} And that should do it. But... If you are trying to reuse the same sequence on different types and that's why you had |
Thanks |
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 ofbeanClass.getName()
and also can not return the default group ofDefault.class
.