Support for Bean Validation 2.0 container elements (with BV 2.0 test setup)

Includes latest dependency updates (Hibernate Validator 6.0.1, Caffeine 2.5.4, Netty 4.1.14, Tomcat 8.5.19, Johnzon 1.1.2, JsonPath 2.4, Jython 2.7.1)

Issue: SPR-15839
Issue: SPR-15808
This commit is contained in:
Juergen Hoeller
2017-08-08 17:26:30 +02:00
parent 48f95e9b96
commit de09f8ca1f
10 changed files with 1193 additions and 36 deletions

View File

@@ -25,6 +25,8 @@ import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.validation.ConstraintViolation;
import javax.validation.ElementKind;
import javax.validation.Path;
import javax.validation.ValidationException;
import javax.validation.executable.ExecutableValidator;
import javax.validation.metadata.BeanDescriptor;
@@ -57,7 +59,7 @@ import org.springframework.validation.SmartValidator;
*/
public class SpringValidatorAdapter implements SmartValidator, javax.validation.Validator {
private static final Set<String> internalAnnotationAttributes = new HashSet<>(3);
private static final Set<String> internalAnnotationAttributes = new HashSet<>(4);
static {
internalAnnotationAttributes.add("message");
@@ -176,7 +178,31 @@ public class SpringValidatorAdapter implements SmartValidator, javax.validation.
* @see org.springframework.validation.FieldError#getField()
*/
protected String determineField(ConstraintViolation<Object> violation) {
return violation.getPropertyPath().toString();
Path path = violation.getPropertyPath();
StringBuilder sb = new StringBuilder();
boolean first = true;
for (Path.Node node : path) {
if (node.isInIterable()) {
sb.append('[');
Object index = node.getIndex();
if (index == null) {
index = node.getKey();
}
if (index != null) {
sb.append(index);
}
sb.append(']');
}
String name = node.getName();
if (name != null && node.getKind() == ElementKind.PROPERTY) {
if (!first) {
sb.append('.');
}
first = false;
sb.append(name);
}
}
return sb.toString();
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,8 +31,6 @@ import org.springframework.tests.sample.beans.TestBean;
import static org.junit.Assert.*;
/**
* Tested against Hibernate Validator 5.x.
*
* @author Juergen Hoeller
*/
public class BeanValidationPostProcessorTests {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,8 +35,6 @@ import org.springframework.validation.annotation.Validated;
import static org.junit.Assert.*;
/**
* Tests against Hibernate Validator 5.x.
*
* @author Juergen Hoeller
*/
@SuppressWarnings("rawtypes")

View File

@@ -48,7 +48,6 @@ import static org.junit.Assert.*;
/**
* @author Kazuki Shimizu
* @author Juergen Hoeller
* @since 4.3
*/
public class SpringValidatorAdapterTests {
@@ -84,6 +83,7 @@ public class SpringValidatorAdapterTests {
validatorAdapter.validate(testBean, errors);
assertThat(errors.getFieldErrorCount("password"), is(1));
assertThat(errors.getFieldValue("password"), is("pass"));
assertThat(messageSource.getMessage(errors.getFieldError("password"), Locale.ENGLISH),
is("Size of Password is must be between 8 and 128"));
}
@@ -98,6 +98,7 @@ public class SpringValidatorAdapterTests {
validatorAdapter.validate(testBean, errors);
assertThat(errors.getFieldErrorCount("password"), is(1));
assertThat(errors.getFieldValue("password"), is("password"));
assertThat(messageSource.getMessage(errors.getFieldError("password"), Locale.ENGLISH),
is("Password must be same value with Password(Confirm)"));
}
@@ -112,6 +113,7 @@ public class SpringValidatorAdapterTests {
validatorAdapter.validate(testBean, errors);
assertThat(errors.getFieldErrorCount("email"), is(1));
assertThat(errors.getFieldValue("email"), is("test@example.com"));
assertThat(errors.getFieldErrorCount("confirmEmail"), is(1));
assertThat(messageSource.getMessage(errors.getFieldError("email"), Locale.ENGLISH),
is("email must be same value with confirmEmail"));
@@ -131,6 +133,7 @@ public class SpringValidatorAdapterTests {
validatorAdapter.validate(testBean, errors);
assertThat(errors.getFieldErrorCount("email"), is(1));
assertThat(errors.getFieldValue("email"), is("test@example.com"));
assertThat(errors.getFieldErrorCount("confirmEmail"), is(1));
assertThat(messageSource.getMessage(errors.getFieldError("email"), Locale.ENGLISH),
is("email must be same value with confirmEmail"));
@@ -139,6 +142,7 @@ public class SpringValidatorAdapterTests {
}
@Same(field = "password", comparingField = "confirmPassword")
@Same(field = "email", comparingField = "confirmEmail")
static class TestBean {

View File

@@ -55,8 +55,6 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
* Tests against Hibernate Validator 5.x.
*
* @author Juergen Hoeller
*/
public class ValidatorFactoryTests {