Skip to content

Commit 95e3f48

Browse files
committed
fixed SpringValidatorAdapter regression to return correct error codes for class-level constraints (SPR-8958)
1 parent 2a0714b commit 95e3f48

File tree

5 files changed

+40
-22
lines changed

5 files changed

+40
-22
lines changed

org.springframework.context/src/main/java/org/springframework/validation/AbstractBindingResult.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -120,13 +120,6 @@ public void addAllErrors(Errors errors) {
120120
this.errors.addAll(errors.getAllErrors());
121121
}
122122

123-
/**
124-
* Resolve the given error code into message codes.
125-
* Calls the MessageCodesResolver with appropriate parameters.
126-
* @param errorCode the error code to resolve into message codes
127-
* @return the resolved message codes
128-
* @see #setMessageCodesResolver
129-
*/
130123
public String[] resolveMessageCodes(String errorCode) {
131124
return getMessageCodesResolver().resolveMessageCodes(errorCode, getObjectName());
132125
}

org.springframework.context/src/main/java/org/springframework/validation/BindException.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -220,6 +220,10 @@ public void addError(ObjectError error) {
220220
this.bindingResult.addError(error);
221221
}
222222

223+
public String[] resolveMessageCodes(String errorCode) {
224+
return this.bindingResult.resolveMessageCodes(errorCode);
225+
}
226+
223227
public String[] resolveMessageCodes(String errorCode, String field) {
224228
return this.bindingResult.resolveMessageCodes(errorCode, field);
225229
}

org.springframework.context/src/main/java/org/springframework/validation/BindingResult.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -112,6 +112,14 @@ public interface BindingResult extends Errors {
112112
*/
113113
void addError(ObjectError error);
114114

115+
/**
116+
* Resolve the given error code into message codes.
117+
* <p>Calls the configured {@link MessageCodesResolver} with appropriate parameters.
118+
* @param errorCode the error code to resolve into message codes
119+
* @return the resolved message codes
120+
*/
121+
String[] resolveMessageCodes(String errorCode);
122+
115123
/**
116124
* Resolve the given error code into message codes for the given field.
117125
* <p>Calls the configured {@link MessageCodesResolver} with appropriate parameters.

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -119,24 +119,23 @@ protected void processConstraintViolations(Set<ConstraintViolation<Object>> viol
119119
// can do custom FieldError registration with invalid value from ConstraintViolation,
120120
// as necessary for Hibernate Validator compatibility (non-indexed set path in field)
121121
BindingResult bindingResult = (BindingResult) errors;
122-
String[] errorCodes = bindingResult.resolveMessageCodes(errorCode, field);
123122
String nestedField = bindingResult.getNestedPath() + field;
124-
ObjectError error;
125123
if ("".equals(nestedField)) {
126-
error = new ObjectError(
127-
errors.getObjectName(), errorCodes, errorArgs, violation.getMessage());
124+
String[] errorCodes = bindingResult.resolveMessageCodes(errorCode);
125+
bindingResult.addError(new ObjectError(
126+
errors.getObjectName(), errorCodes, errorArgs, violation.getMessage()));
128127
}
129128
else {
130129
Object invalidValue = violation.getInvalidValue();
131130
if (!"".equals(field) && invalidValue == violation.getLeafBean()) {
132131
// bean constraint with property path: retrieve the actual property value
133132
invalidValue = bindingResult.getRawFieldValue(field);
134133
}
135-
error = new FieldError(
134+
String[] errorCodes = bindingResult.resolveMessageCodes(errorCode, field);
135+
bindingResult.addError(new FieldError(
136136
errors.getObjectName(), nestedField, invalidValue, false,
137-
errorCodes, errorArgs, violation.getMessage());
137+
errorCodes, errorArgs, violation.getMessage()));
138138
}
139-
bindingResult.addError(error);
140139
}
141140
else {
142141
// got no BindingResult - can only do standard rejectValue call

org.springframework.context/src/test/java/org/springframework/validation/beanvalidation/ValidatorFactoryTests.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -110,11 +110,22 @@ public void testSpringValidation() throws Exception {
110110
assertEquals(2, result.getErrorCount());
111111
FieldError fieldError = result.getFieldError("name");
112112
assertEquals("name", fieldError.getField());
113-
System.out.println(Arrays.asList(fieldError.getCodes()));
113+
List<String> errorCodes = Arrays.asList(fieldError.getCodes());
114+
assertEquals(4, errorCodes.size());
115+
assertTrue(errorCodes.contains("NotNull.person.name"));
116+
assertTrue(errorCodes.contains("NotNull.name"));
117+
assertTrue(errorCodes.contains("NotNull.java.lang.String"));
118+
assertTrue(errorCodes.contains("NotNull"));
114119
System.out.println(fieldError.getDefaultMessage());
115120
fieldError = result.getFieldError("address.street");
116121
assertEquals("address.street", fieldError.getField());
117-
System.out.println(Arrays.asList(fieldError.getCodes()));
122+
errorCodes = Arrays.asList(fieldError.getCodes());
123+
assertEquals(5, errorCodes.size());
124+
assertTrue(errorCodes.contains("NotNull.person.address.street"));
125+
assertTrue(errorCodes.contains("NotNull.address.street"));
126+
assertTrue(errorCodes.contains("NotNull.street"));
127+
assertTrue(errorCodes.contains("NotNull.java.lang.String"));
128+
assertTrue(errorCodes.contains("NotNull"));
118129
System.out.println(fieldError.getDefaultMessage());
119130
}
120131

@@ -129,7 +140,10 @@ public void testSpringValidationWithClassLevel() throws Exception {
129140
validator.validate(person, result);
130141
assertEquals(1, result.getErrorCount());
131142
ObjectError globalError = result.getGlobalError();
132-
System.out.println(Arrays.asList(globalError.getCodes()));
143+
List<String> errorCodes = Arrays.asList(globalError.getCodes());
144+
assertEquals(2, errorCodes.size());
145+
assertTrue(errorCodes.contains("NameAddressValid.person"));
146+
assertTrue(errorCodes.contains("NameAddressValid"));
133147
System.out.println(globalError.getDefaultMessage());
134148
}
135149

0 commit comments

Comments
 (0)