Skip to content

Commit 84be348

Browse files
committed
log or rethrow original ConversionFailedException as appropriate
1 parent 207b231 commit 84be348

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

org.springframework.beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ public <T> T convertIfNecessary(String propertyName, Object oldValue, Object new
131131
// Custom editor for this type?
132132
PropertyEditor editor = this.propertyEditorRegistry.findCustomEditor(requiredType, propertyName);
133133

134+
ConversionFailedException firstAttemptEx = null;
135+
134136
// No custom editor but custom ConversionService specified?
135137
ConversionService conversionService = this.propertyEditorRegistry.getConversionService();
136138
if (editor == null && conversionService != null && convertedValue != null && typeDescriptor != null) {
@@ -139,8 +141,10 @@ public <T> T convertIfNecessary(String propertyName, Object oldValue, Object new
139141
if (conversionService.canConvert(sourceTypeDesc, targetTypeDesc)) {
140142
try {
141143
return (T) conversionService.convert(convertedValue, sourceTypeDesc, targetTypeDesc);
142-
} catch (ConversionFailedException e) {
144+
}
145+
catch (ConversionFailedException ex) {
143146
// fallback to default conversion logic below
147+
firstAttemptEx = ex;
144148
}
145149
}
146150
}
@@ -216,6 +220,9 @@ else if (convertedValue instanceof String && !requiredType.isInstance(convertedV
216220
}
217221

218222
if (!ClassUtils.isAssignableValue(requiredType, convertedValue)) {
223+
if (firstAttemptEx != null) {
224+
throw firstAttemptEx;
225+
}
219226
// Definitely doesn't match: throw IllegalArgumentException/IllegalStateException
220227
StringBuilder msg = new StringBuilder();
221228
msg.append("Cannot convert value of type [").append(ClassUtils.getDescriptiveType(newValue));
@@ -236,6 +243,11 @@ else if (convertedValue instanceof String && !requiredType.isInstance(convertedV
236243
}
237244
}
238245

246+
if (firstAttemptEx != null) {
247+
logger.debug("Original ConversionService attempt failed - ignored since " +
248+
"PropertyEditor based conversion eventually succeeded", firstAttemptEx);
249+
}
250+
239251
return (T) convertedValue;
240252
}
241253

@@ -524,7 +536,7 @@ protected Collection convertToTypedCollection(
524536
Object element = it.next();
525537
String indexedPropertyName = buildIndexedPropertyName(propertyName, i);
526538
Object convertedElement = convertIfNecessary(indexedPropertyName, null, element,
527-
(elementType != null ? elementType.getType() : null) , typeDescriptor.getElementTypeDescriptor());
539+
(elementType != null ? elementType.getType() : null) , elementType);
528540
try {
529541
convertedCopy.add(convertedElement);
530542
}
@@ -608,9 +620,9 @@ protected Map convertToTypedMap(
608620
Object value = entry.getValue();
609621
String keyedPropertyName = buildKeyedPropertyName(propertyName, key);
610622
Object convertedKey = convertIfNecessary(keyedPropertyName, null, key,
611-
(keyType != null ? keyType.getType() : null), typeDescriptor.getMapKeyTypeDescriptor());
623+
(keyType != null ? keyType.getType() : null), keyType);
612624
Object convertedValue = convertIfNecessary(keyedPropertyName, null, value,
613-
(valueType!= null ? valueType.getType() : null), typeDescriptor.getMapValueTypeDescriptor());
625+
(valueType!= null ? valueType.getType() : null), valueType);
614626
try {
615627
convertedCopy.put(convertedKey, convertedValue);
616628
}

0 commit comments

Comments
 (0)