Object/FieldError exposes source object through unwrap/contains methods
Issue: SPR-16372
This commit is contained in:
@@ -77,7 +77,7 @@ public class DefaultBindingErrorProcessor implements BindingErrorProcessor {
|
||||
}
|
||||
FieldError error = new FieldError(bindingResult.getObjectName(), field, rejectedValue, true,
|
||||
codes, arguments, ex.getLocalizedMessage());
|
||||
error.initSource(ex);
|
||||
error.wrap(ex);
|
||||
bindingResult.addError(error);
|
||||
}
|
||||
|
||||
|
||||
@@ -74,7 +74,7 @@ public class ObjectError extends DefaultMessageSourceResolvable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the source behind this error: possibly an {@link Exception}
|
||||
* Preserve the source behind this error: possibly an {@link Exception}
|
||||
* (typically {@link org.springframework.beans.PropertyAccessException})
|
||||
* or a Bean Validation {@link javax.validation.ConstraintViolation}.
|
||||
* <p>Note that any such source object is being stored as transient:
|
||||
@@ -82,22 +82,51 @@ public class ObjectError extends DefaultMessageSourceResolvable {
|
||||
* @param source the source object
|
||||
* @since 5.0.4
|
||||
*/
|
||||
public void initSource(Object source) {
|
||||
Assert.state(this.source == null, "Source already initialized");
|
||||
public void wrap(Object source) {
|
||||
if (this.source != null) {
|
||||
throw new IllegalStateException("Already wrapping " + this.source);
|
||||
}
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the source behind this error: possibly an {@link Exception}
|
||||
* Unwrap the source behind this error: possibly an {@link Exception}
|
||||
* (typically {@link org.springframework.beans.PropertyAccessException})
|
||||
* or a Bean Validation {@link javax.validation.ConstraintViolation}.
|
||||
* @return the source object, or {@code null} if none available
|
||||
* (none specified or not available anymore after deserialization)
|
||||
* <p>The cause of the outermost exception will be introspected as well,
|
||||
* e.g. the underlying conversion exception or exception thrown from a setter
|
||||
* (instead of having to unwrap the {@code PropertyAccessException} in turn).
|
||||
* @return the source object of the given type
|
||||
* @throws IllegalArgumentException if no such source object is available
|
||||
* (i.e. none specified or not available anymore after deserialization)
|
||||
* @since 5.0.4
|
||||
*/
|
||||
@Nullable
|
||||
public Object getSource() {
|
||||
return this.source;
|
||||
public <T> T unwrap(Class<T> sourceType) {
|
||||
if (sourceType.isInstance(this.source)) {
|
||||
return sourceType.cast(this.source);
|
||||
}
|
||||
else if (this.source instanceof Throwable) {
|
||||
Throwable cause = ((Throwable) this.source).getCause();
|
||||
if (sourceType.isInstance(cause)) {
|
||||
return sourceType.cast(cause);
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("No source object of the given type available: " + sourceType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the source behind this error: possibly an {@link Exception}
|
||||
* (typically {@link org.springframework.beans.PropertyAccessException})
|
||||
* or a Bean Validation {@link javax.validation.ConstraintViolation}.
|
||||
* <p>The cause of the outermost exception will be introspected as well,
|
||||
* e.g. the underlying conversion exception or exception thrown from a setter
|
||||
* (instead of having to unwrap the {@code PropertyAccessException} in turn).
|
||||
* @return whether this error has been caused by a source object of the given type
|
||||
* @since 5.0.4
|
||||
*/
|
||||
public boolean contains(Class<?> sourceType) {
|
||||
return (sourceType.isInstance(this.source) ||
|
||||
(this.source instanceof Throwable && sourceType.isInstance(((Throwable) this.source).getCause())));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -144,7 +144,7 @@ public class SpringValidatorAdapter implements SmartValidator, javax.validation.
|
||||
String[] errorCodes = bindingResult.resolveMessageCodes(errorCode);
|
||||
ObjectError error = new ObjectError(
|
||||
errors.getObjectName(), errorCodes, errorArgs, violation.getMessage());
|
||||
error.initSource(violation);
|
||||
error.wrap(violation);
|
||||
bindingResult.addError(error);
|
||||
}
|
||||
else {
|
||||
@@ -152,7 +152,7 @@ public class SpringValidatorAdapter implements SmartValidator, javax.validation.
|
||||
String[] errorCodes = bindingResult.resolveMessageCodes(errorCode, field);
|
||||
FieldError error = new FieldError(errors.getObjectName(), nestedField,
|
||||
rejectedValue, false, errorCodes, errorArgs, violation.getMessage());
|
||||
error.initSource(violation);
|
||||
error.wrap(violation);
|
||||
bindingResult.addError(error);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user