Avoid NPE in ObjectToObjectConverter
Bypass ObjectToObject conversion when source and object types are identical and protect against a null source object. Prior to this commit the TypeDescriptor was used to determine if conversion was necessary. This caused issues when comparing a descriptor with annotations to one without. The updated code now compares using TypeDescriptor.getType(). The ObjectToObject converter will now no longer attempt to convert null source objects. Issue: SPR-9933
This commit is contained in:
@@ -18,6 +18,8 @@ package org.springframework.core.convert.support;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.SystemColor;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
@@ -744,6 +746,22 @@ public class GenericConversionServiceTests {
|
||||
assertEquals("1", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertNullAnnotatedStringToString() throws Exception {
|
||||
DefaultConversionService.addDefaultConverters(conversionService);
|
||||
String source = null;
|
||||
TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("annotatedString"));
|
||||
TypeDescriptor targetType = TypeDescriptor.valueOf(String.class);
|
||||
conversionService.convert(source, sourceType, targetType);
|
||||
}
|
||||
|
||||
@ExampleAnnotation
|
||||
public String annotatedString;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public static @interface ExampleAnnotation {
|
||||
}
|
||||
|
||||
private static class MyConditionalConverter implements Converter<String, Color>,
|
||||
ConditionalConverter {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user