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

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.spi.group;

import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import jakarta.validation.constraints.NotNull;
import org.hibernate.validator.group.GroupSequenceProvider;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
* DefaultGroupSequenceProviderTest.
*
* @author ilikly
*/
public class DefaultGroupSequenceProviderTest {

@Test
public void withoutClassParam() {
Assert.assertThrows( () -> {
final ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
final Validator validator = validatorFactory.getValidator();
validator.validate( new A1() );
} );
}

@Test
public void withClassParam() {
final ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
final Validator validator = validatorFactory.getValidator();
validator.validate( new A2() );
}


public static class DefaultGroupSequenceProvider1 implements DefaultGroupSequenceProvider<Object> {

@Override
public List<Class<?>> getValidationGroups( Object object ) {
List<Class<?>> groups = new ArrayList<>();
if ( Objects.nonNull( object ) ) {
groups.add( object.getClass() );
}
return groups;
}
}


public static class DefaultGroupSequenceProvider2 implements DefaultGroupSequenceProvider<Object> {

@Override
public List<Class<?>> getValidationGroups( Class<?> clazz, Object object ) {
List<Class<?>> groups = new ArrayList<>();
if ( Objects.nonNull( clazz ) ) {
groups.add( clazz );
}
return groups;
}

@Override
public List<Class<?>> getValidationGroups( Object object ) {
throw new IllegalArgumentException( "" );
}
}


@GroupSequenceProvider(DefaultGroupSequenceProvider1.class)
public static class A1 {
@NotNull
private String name;

public String getName() {
return name;
}

public void setName( String name ) {
this.name = name;
}
}

@GroupSequenceProvider(DefaultGroupSequenceProvider1.class)
public static class B1 {
@NotNull
private String name;

public String getName() {
return name;
}

public void setName( String name ) {
this.name = name;
}
}

@GroupSequenceProvider(DefaultGroupSequenceProvider2.class)
public static class A2 {
@NotNull
private String name;

public String getName() {
return name;
}

public void setName( String name ) {
this.name = name;
}
}

@GroupSequenceProvider(DefaultGroupSequenceProvider2.class)
public static class B2 {
@NotNull
private String name;

public String getName() {
return name;
}

public void setName( String name ) {
this.name = name;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public Optional<ExecutableMetaData> getMetaDataFor(Executable executable) {
@Override
public List<Class<?>> getDefaultGroupSequence(T beanState) {
if ( hasDefaultGroupSequenceProvider() ) {
List<Class<?>> providerDefaultGroupSequence = defaultGroupSequenceProvider.getValidationGroups( beanState );
List<Class<?>> providerDefaultGroupSequence = defaultGroupSequenceProvider.getValidationGroups( beanClass, beanState );
return getValidDefaultGroupSequence( beanClass, providerDefaultGroupSequence );
}

Expand All @@ -335,7 +335,7 @@ public List<Class<?>> getDefaultGroupSequence(T beanState) {
@Override
public Iterator<Sequence> getDefaultValidationSequence(T beanState) {
if ( hasDefaultGroupSequenceProvider() ) {
List<Class<?>> providerDefaultGroupSequence = defaultGroupSequenceProvider.getValidationGroups( beanState );
List<Class<?>> providerDefaultGroupSequence = defaultGroupSequenceProvider.getValidationGroups( beanClass, beanState );
return validationOrderGenerator.getDefaultValidationOrder(
beanClass,
getValidDefaultGroupSequence( beanClass, providerDefaultGroupSequence )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@
*/
public interface DefaultGroupSequenceProvider<T> {

/**
* This method returns the default group sequence for the given instance.
* <p>
* The object parameter allows to dynamically compose the default group sequence in function of the validated value state.
* </p>
*
* @param clazz the instance class being validated.
* @param object the instance being validated. This value can be {@code null} in case this method was called as part of
* {@linkplain jakarta.validation.Validator#validateValue(Class, String, Object, Class[]) Validator#validateValue}.
*
* @return a list of classes specifying the default group sequence. The same constraints to the redefined group list
* apply as for lists defined via {@code GroupSequence}. In particular the list has to contain the type T.
*/
default List<Class<?>> getValidationGroups(Class<?> clazz, T object) {
return getValidationGroups( object );
}

/**
* This method returns the default group sequence for the given instance.
* <p>
Expand Down