Add @Name support for Kotlin value object binding

Fixes gh-24379
This commit is contained in:
Scott Frederick 2020-12-07 18:51:42 -06:00
parent dc9cdb71f5
commit d61724aada
2 changed files with 19 additions and 1 deletions

View File

@ -48,6 +48,7 @@ import org.springframework.util.Assert;
* @author Madhura Bhave * @author Madhura Bhave
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Phillip Webb * @author Phillip Webb
* @author Scott Frederick
*/ */
class ValueObjectBinder implements DataObjectBinder { class ValueObjectBinder implements DataObjectBinder {
@ -202,7 +203,7 @@ class ValueObjectBinder implements DataObjectBinder {
List<KParameter> parameters = kotlinConstructor.getParameters(); List<KParameter> parameters = kotlinConstructor.getParameters();
List<ConstructorParameter> result = new ArrayList<>(parameters.size()); List<ConstructorParameter> result = new ArrayList<>(parameters.size());
for (KParameter parameter : parameters) { for (KParameter parameter : parameters) {
String name = parameter.getName(); String name = getParameterName(parameter);
ResolvableType parameterType = ResolvableType ResolvableType parameterType = ResolvableType
.forType(ReflectJvmMapping.getJavaType(parameter.getType()), type); .forType(ReflectJvmMapping.getJavaType(parameter.getType()), type);
Annotation[] annotations = parameter.getAnnotations().toArray(new Annotation[0]); Annotation[] annotations = parameter.getAnnotations().toArray(new Annotation[0]);
@ -211,6 +212,11 @@ class ValueObjectBinder implements DataObjectBinder {
return Collections.unmodifiableList(result); return Collections.unmodifiableList(result);
} }
private String getParameterName(KParameter parameter) {
return parameter.getAnnotations().stream().filter(Name.class::isInstance).findFirst().map(Name.class::cast)
.map(Name::value).orElse(parameter.getName());
}
@Override @Override
List<ConstructorParameter> getConstructorParameters() { List<ConstructorParameter> getConstructorParameters() {
return this.constructorParameters; return this.constructorParameters;

View File

@ -10,6 +10,7 @@ import org.springframework.core.ResolvableType
* Tests for `ConstructorParametersBinder`. * Tests for `ConstructorParametersBinder`.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Scott Frederick
*/ */
class KotlinConstructorParametersBinderTests { class KotlinConstructorParametersBinderTests {
@ -187,6 +188,15 @@ class KotlinConstructorParametersBinderTests {
assertThat(bean.value.get("bar")).isEqualTo("baz"); assertThat(bean.value.get("bar")).isEqualTo("baz");
} }
@Test
fun `Bind to named constructor parameter`() {
val source = MockConfigurationPropertySource()
source.put("foo.string-value", "test")
val binder = Binder(source)
val bean = binder.bind("foo", Bindable.of(ExampleNamedParameterBean::class.java)).get()
assertThat(bean.stringDataValue).isEqualTo("test")
}
class ExampleValueBean(val intValue: Int?, val longValue: Long?, class ExampleValueBean(val intValue: Int?, val longValue: Long?,
val booleanValue: Boolean?, val stringValue: String?, val booleanValue: Boolean?, val stringValue: String?,
val enumValue: ExampleEnum?) val enumValue: ExampleEnum?)
@ -228,6 +238,8 @@ class KotlinConstructorParametersBinderTests {
val stringValue: String = "my data", val stringValue: String = "my data",
val enumValue: ExampleEnum = ExampleEnum.BAR_BAZ) val enumValue: ExampleEnum = ExampleEnum.BAR_BAZ)
data class ExampleNamedParameterBean(@Name("stringValue") val stringDataValue: String)
data class GenericValue<T>( data class GenericValue<T>(
val value: T val value: T
) )