Merge branch '2.7.x'
Closes gh-32564
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package org.springframework.boot.context.properties.bind;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Parameter;
|
||||
@@ -25,6 +26,7 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import kotlin.reflect.KFunction;
|
||||
import kotlin.reflect.KParameter;
|
||||
@@ -32,6 +34,7 @@ import kotlin.reflect.jvm.ReflectJvmMapping;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
|
||||
import org.springframework.core.CollectionFactory;
|
||||
import org.springframework.core.DefaultParameterNameDiscoverer;
|
||||
import org.springframework.core.KotlinDetector;
|
||||
import org.springframework.core.MethodParameter;
|
||||
@@ -101,7 +104,7 @@ class ValueObjectBinder implements DataObjectBinder {
|
||||
if (annotation instanceof DefaultValue defaultValueAnnotation) {
|
||||
String[] defaultValue = defaultValueAnnotation.value();
|
||||
if (defaultValue.length == 0) {
|
||||
return getNewInstanceIfPossible(context, type);
|
||||
return getNewDefaultValueInstanceIfPossible(context, type);
|
||||
}
|
||||
return convertDefaultValue(context.getConverter(), defaultValue, type, annotations);
|
||||
}
|
||||
@@ -124,7 +127,7 @@ class ValueObjectBinder implements DataObjectBinder {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T getNewInstanceIfPossible(Binder.Context context, ResolvableType type) {
|
||||
private <T> T getNewDefaultValueInstanceIfPossible(Binder.Context context, ResolvableType type) {
|
||||
Class<T> resolved = (Class<T>) type.resolve();
|
||||
Assert.state(resolved == null || isEmptyDefaultValueAllowed(resolved),
|
||||
() -> "Parameter of type " + type + " must have a non-empty default value.");
|
||||
@@ -132,14 +135,27 @@ class ValueObjectBinder implements DataObjectBinder {
|
||||
if (instance != null) {
|
||||
return instance;
|
||||
}
|
||||
return (resolved != null) ? BeanUtils.instantiateClass(resolved) : null;
|
||||
if (resolved != null) {
|
||||
if (Optional.class == resolved) {
|
||||
return (T) Optional.empty();
|
||||
}
|
||||
if (Collection.class.isAssignableFrom(resolved)) {
|
||||
return (T) CollectionFactory.createCollection(resolved, 0);
|
||||
}
|
||||
if (Map.class.isAssignableFrom(resolved)) {
|
||||
return (T) CollectionFactory.createMap(resolved, 0);
|
||||
}
|
||||
if (resolved.isArray()) {
|
||||
return (T) Array.newInstance(resolved.getComponentType(), 0);
|
||||
}
|
||||
return BeanUtils.instantiateClass(resolved);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean isEmptyDefaultValueAllowed(Class<?> type) {
|
||||
if (type.isPrimitive() || type.isEnum() || isAggregate(type) || type.getName().startsWith("java.lang")) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return (Optional.class == type || isAggregate(type))
|
||||
|| !(type.isPrimitive() || type.isEnum() || type.getName().startsWith("java.lang"));
|
||||
}
|
||||
|
||||
private boolean isAggregate(Class<?> type) {
|
||||
|
||||
@@ -29,6 +29,7 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -852,6 +853,17 @@ class ConfigurationPropertiesTests {
|
||||
assertThat(bean.getBar()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadWhenBindingToConstructorParametersWithEmptyDefaultValueShouldBind() {
|
||||
load(ConstructorParameterEmptyDefaultValueConfiguration.class);
|
||||
ConstructorParameterEmptyDefaultValueProperties bean = this.context
|
||||
.getBean(ConstructorParameterEmptyDefaultValueProperties.class);
|
||||
assertThat(bean.getSet()).isEmpty();
|
||||
assertThat(bean.getMap()).isEmpty();
|
||||
assertThat(bean.getArray()).isEmpty();
|
||||
assertThat(bean.getOptional()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadWhenBindingToConstructorParametersWithDefaultDataUnitShouldBind() {
|
||||
load(ConstructorParameterWithUnitConfiguration.class);
|
||||
@@ -2152,6 +2164,45 @@ class ConfigurationPropertiesTests {
|
||||
|
||||
}
|
||||
|
||||
@ConfigurationProperties(prefix = "test")
|
||||
static class ConstructorParameterEmptyDefaultValueProperties {
|
||||
|
||||
private final Set<String> set;
|
||||
|
||||
private final Map<String, String> map;
|
||||
|
||||
private final int[] array;
|
||||
|
||||
private final Optional<String> optional;
|
||||
|
||||
ConstructorParameterEmptyDefaultValueProperties(@DefaultValue Set<String> set,
|
||||
@DefaultValue Map<String, String> map, @DefaultValue int[] array,
|
||||
@DefaultValue Optional<String> optional) {
|
||||
this.set = set;
|
||||
this.map = map;
|
||||
this.array = array;
|
||||
this.optional = optional;
|
||||
}
|
||||
|
||||
Set<String> getSet() {
|
||||
return this.set;
|
||||
}
|
||||
|
||||
Map<String, String> getMap() {
|
||||
return this.map;
|
||||
}
|
||||
|
||||
int[] getArray() {
|
||||
return this.array;
|
||||
}
|
||||
|
||||
Optional<String> getOptional() {
|
||||
return this.optional;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ConstructorBinding
|
||||
@ConfigurationProperties(prefix = "test")
|
||||
static class ConstructorParameterWithUnitProperties {
|
||||
|
||||
@@ -2229,6 +2280,11 @@ class ConfigurationPropertiesTests {
|
||||
|
||||
}
|
||||
|
||||
@EnableConfigurationProperties(ConstructorParameterEmptyDefaultValueProperties.class)
|
||||
static class ConstructorParameterEmptyDefaultValueConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@EnableConfigurationProperties(ConstructorParameterWithUnitProperties.class)
|
||||
static class ConstructorParameterWithUnitConfiguration {
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -287,29 +288,31 @@ class ValueObjectBinderTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void bindWhenCollectionParameterWithEmptyDefaultValueShouldThrowException() {
|
||||
assertThatExceptionOfType(BindException.class)
|
||||
.isThrownBy(() -> this.binder.bindOrCreate("foo",
|
||||
Bindable.of(NestedConstructorBeanWithEmptyDefaultValueForCollectionTypes.class)))
|
||||
.withStackTraceContaining(
|
||||
"Parameter of type java.util.List<java.lang.String> must have a non-empty default value.");
|
||||
void bindWhenCollectionParameterWithEmptyDefaultValueShouldReturnEmptyInstance() {
|
||||
NestedConstructorBeanWithEmptyDefaultValueForCollectionTypes bound = this.binder.bindOrCreate("foo",
|
||||
Bindable.of(NestedConstructorBeanWithEmptyDefaultValueForCollectionTypes.class));
|
||||
assertThat(bound.getListValue()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void bindWhenMapParametersWithEmptyDefaultValueShouldThrowException() {
|
||||
assertThatExceptionOfType(BindException.class)
|
||||
.isThrownBy(() -> this.binder.bindOrCreate("foo",
|
||||
Bindable.of(NestedConstructorBeanWithEmptyDefaultValueForMapTypes.class)))
|
||||
.withStackTraceContaining(
|
||||
"Parameter of type java.util.Map<java.lang.String, java.lang.String> must have a non-empty default value.");
|
||||
void bindWhenMapParametersWithEmptyDefaultValueShouldReturnEmptyInstance() {
|
||||
NestedConstructorBeanWithEmptyDefaultValueForMapTypes bound = this.binder.bindOrCreate("foo",
|
||||
Bindable.of(NestedConstructorBeanWithEmptyDefaultValueForMapTypes.class));
|
||||
assertThat(bound.getMapValue()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void bindWhenArrayParameterWithEmptyDefaultValueShouldThrowException() {
|
||||
assertThatExceptionOfType(BindException.class)
|
||||
.isThrownBy(() -> this.binder.bindOrCreate("foo",
|
||||
Bindable.of(NestedConstructorBeanWithEmptyDefaultValueForArrayTypes.class)))
|
||||
.withStackTraceContaining("Parameter of type java.lang.String[] must have a non-empty default value.");
|
||||
void bindWhenArrayParameterWithEmptyDefaultValueShouldReturnEmptyInstance() {
|
||||
NestedConstructorBeanWithEmptyDefaultValueForArrayTypes bound = this.binder.bindOrCreate("foo",
|
||||
Bindable.of(NestedConstructorBeanWithEmptyDefaultValueForArrayTypes.class));
|
||||
assertThat(bound.getArrayValue()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void bindWhenOptionalParameterWithEmptyDefaultValueShouldReturnEmptyInstance() {
|
||||
NestedConstructorBeanWithEmptyDefaultValueForOptionalTypes bound = this.binder.bindOrCreate("foo",
|
||||
Bindable.of(NestedConstructorBeanWithEmptyDefaultValueForOptionalTypes.class));
|
||||
assertThat(bound.getOptionalValue()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -747,8 +750,7 @@ class ValueObjectBinderTests {
|
||||
|
||||
private final String[] arrayValue;
|
||||
|
||||
NestedConstructorBeanWithEmptyDefaultValueForArrayTypes(@DefaultValue String[] arrayValue,
|
||||
@DefaultValue Integer intValue) {
|
||||
NestedConstructorBeanWithEmptyDefaultValueForArrayTypes(@DefaultValue String[] arrayValue) {
|
||||
this.arrayValue = arrayValue;
|
||||
}
|
||||
|
||||
@@ -758,6 +760,20 @@ class ValueObjectBinderTests {
|
||||
|
||||
}
|
||||
|
||||
static class NestedConstructorBeanWithEmptyDefaultValueForOptionalTypes {
|
||||
|
||||
private final Optional<String> optionalValue;
|
||||
|
||||
NestedConstructorBeanWithEmptyDefaultValueForOptionalTypes(@DefaultValue Optional<String> optionalValue) {
|
||||
this.optionalValue = optionalValue;
|
||||
}
|
||||
|
||||
Optional<String> getOptionalValue() {
|
||||
return this.optionalValue;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class NestedConstructorBeanWithEmptyDefaultValueForEnumTypes {
|
||||
|
||||
private Foo foo;
|
||||
|
||||
Reference in New Issue
Block a user