Skip to content

Commit 26284ca

Browse files
committed
Hibernate Validator 5 compatible support for element constraints
Issue: SPR-15916 Issue: SPR-15839
1 parent b6cae21 commit 26284ca

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

spring-context-support/src/test/java/org/springframework/validation/beanvalidation2/SpringValidatorAdapterTests.java

+49
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222
import java.lang.annotation.Retention;
2323
import java.lang.annotation.Target;
2424
import java.util.Arrays;
25+
import java.util.HashMap;
2526
import java.util.List;
2627
import java.util.Locale;
28+
import java.util.Map;
2729
import javax.validation.Constraint;
2830
import javax.validation.ConstraintValidator;
2931
import javax.validation.ConstraintValidatorContext;
3032
import javax.validation.Payload;
33+
import javax.validation.Valid;
3134
import javax.validation.Validation;
3235
import javax.validation.Validator;
3336
import javax.validation.constraints.NotNull;
@@ -157,6 +160,35 @@ public void testListElementConstraint() {
157160
assertNull(errors.getFieldValue("property[4]"));
158161
}
159162

163+
@Test // SPR-15839
164+
public void testMapValueConstraint() {
165+
Map<String, String> property = new HashMap<>();
166+
property.put("no value can be", null);
167+
168+
BeanWithMapEntryConstraint bean = new BeanWithMapEntryConstraint();
169+
bean.setProperty(property);
170+
171+
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(bean, "bean");
172+
validatorAdapter.validate(bean, errors);
173+
174+
assertThat(errors.getFieldErrorCount("property[no value can be]"), is(1));
175+
assertNull(errors.getFieldValue("property[no value can be]"));
176+
}
177+
178+
@Test // SPR-15839
179+
public void testMapEntryConstraint() {
180+
Map<String, String> property = new HashMap<>();
181+
property.put(null, null);
182+
183+
BeanWithMapEntryConstraint bean = new BeanWithMapEntryConstraint();
184+
bean.setProperty(property);
185+
186+
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(bean, "bean");
187+
validatorAdapter.validate(bean, errors);
188+
189+
assertTrue(errors.hasFieldErrors("property[]"));
190+
assertNull(errors.getFieldValue("property[]"));
191+
}
160192

161193

162194
@Same(field = "password", comparingField = "confirmPassword")
@@ -278,14 +310,31 @@ public boolean isValid(Object value, ConstraintValidatorContext context) {
278310

279311
public class BeanWithListElementConstraint {
280312

313+
@Valid
281314
private List<@NotNull String> property;
282315

283316
public List<String> getProperty() {
284317
return property;
285318
}
319+
286320
public void setProperty(List<String> property) {
287321
this.property = property;
288322
}
289323
}
290324

325+
326+
public class BeanWithMapEntryConstraint {
327+
328+
@Valid
329+
private Map<@NotNull String, @NotNull String> property;
330+
331+
public Map<String, String> getProperty() {
332+
return property;
333+
}
334+
335+
public void setProperty(Map<String, String> property) {
336+
this.property = property;
337+
}
338+
}
339+
291340
}

spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ protected String determineField(ConstraintViolation<Object> violation) {
194194
sb.append(']');
195195
}
196196
String name = node.getName();
197-
if (name != null && node.getKind() == ElementKind.PROPERTY) {
197+
if (name != null && node.getKind() == ElementKind.PROPERTY && !name.startsWith("<")) {
198198
if (!first) {
199199
sb.append('.');
200200
}

0 commit comments

Comments
 (0)