diff --git a/spring-core/src/main/java/org/springframework/core/convert/converter/ConverterFactory.java b/spring-core/src/main/java/org/springframework/core/convert/converter/ConverterFactory.java index 3aac06b900f..2d4f62e92a3 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/converter/ConverterFactory.java +++ b/spring-core/src/main/java/org/springframework/core/convert/converter/ConverterFactory.java @@ -38,6 +38,6 @@ public interface ConverterFactory { * @param targetType the target type to convert to * @return a converter from S to T */ - Converter getConverter(Class targetType); + Converter getConverter(Class targetType); } diff --git a/spring-core/src/test/kotlin/org/springframework/core/convert/converter/ConverterFactoryNullnessTests.kt b/spring-core/src/test/kotlin/org/springframework/core/convert/converter/ConverterFactoryNullnessTests.kt index 877e20817c9..221bdcc68e9 100644 --- a/spring-core/src/test/kotlin/org/springframework/core/convert/converter/ConverterFactoryNullnessTests.kt +++ b/spring-core/src/test/kotlin/org/springframework/core/convert/converter/ConverterFactoryNullnessTests.kt @@ -22,18 +22,27 @@ import kotlin.reflect.full.primaryConstructor /** * @author Brian Clozel + * @author Sebastien Deleuze */ class ConverterFactoryNullnessTests { @Test fun converterFactoryWithNullableTypes() { + val factory = StringToNullableIdConverterFactory + + val userIdConverter: Converter = factory.getConverter(UserId::class.java) + assertThat(userIdConverter.convert("42")).isEqualTo(UserId("42")) + } + + @Test + fun converterFactoryWithNonNullableTypes() { val factory = StringToIdConverterFactory - val userIdConverter = factory.getConverter(UserId::class.java) + val userIdConverter: Converter = factory.getConverter(UserId::class.java) assertThat(userIdConverter.convert("42")).isEqualTo(UserId("42")) } - object StringToIdConverterFactory : ConverterFactory { + object StringToNullableIdConverterFactory : ConverterFactory { override fun getConverter(targetType: Class): Converter { val constructor = checkNotNull(targetType.kotlin.primaryConstructor) return Converter { source -> @@ -42,12 +51,19 @@ class ConverterFactoryNullnessTests { } } + object StringToIdConverterFactory : ConverterFactory { + override fun getConverter(targetType: Class): Converter { + val constructor = checkNotNull(targetType.kotlin.primaryConstructor) + return Converter { source -> + constructor.call(source) + } + } + } + abstract class Id { abstract val value: String } data class UserId(override val value: String) : Id() - data class ProductId(override val value: String) : Id() - } \ No newline at end of file