| I too am seeing this issue. I am using hibernate-validator-5.2.4.Final Here is a simple test which reproduces the problem:
package com.sample;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Optional;
public class ValidationSample
{
@NotNull
private String id = null;
@Valid
private Credentials credentials = null;
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public Optional<Credentials> getCredentials()
{
return Optional.ofNullable(credentials);
}
public void setCredentials(Credentials credentials)
{
this.credentials = credentials;
}
}
package com.sample;
import javax.validation.constraints.NotNull;
public class Credentials
{
@NotNull
private String username = null;
@NotNull
private String password = null;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
}
package com.sample;
import org.junit.Test;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import static org.junit.Assert.assertTrue;
public class ValidationSampleTest
{
private final Validator validator = createValidator();
private static Validator createValidator()
{
Map<String, String> validationProperties = new HashMap<>();
validationProperties.put("hibernate.validator.fail_fast", "false");
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.setValidationPropertyMap(validationProperties);
validator.afterPropertiesSet();
return validator;
}
@Test
public void testValidation()
{
Credentials credentials = new Credentials();
credentials.setUsername("user");
credentials.setPassword("password");
ValidationSample validationSample = new ValidationSample();
validationSample.setId("foo");
validationSample.setCredentials(credentials);
Set<ConstraintViolation<ValidationSample>> violations = validator.validate(validationSample);
assertTrue(violations.isEmpty());
}
}
The problem occurs when a constraint annotation is on a field, yet the getter method returns an optional. I found a workaround using the @UnwrapValidatedValue(false), which has to be put on the getter. Adding the annotation to the field does not resolve the issue. For example:
package com.sample;
import org.hibernate.validator.valuehandling.UnwrapValidatedValue;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Optional;
public class ValidationSample
{
@NotNull
private String id = null;
@Valid
private Credentials credentials = null;
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
@UnwrapValidatedValue(false)
public Optional<Credentials> getCredentials()
{
return Optional.ofNullable(credentials);
}
public void setCredentials(Credentials credentials)
{
this.credentials = credentials;
}
}
|