Skip to content

Commit

Permalink
Support empty @DefaultValue annotations on aggregates and optional
Browse files Browse the repository at this point in the history
Update `ValueObjectBinder` to allow an empty `@DefaultValue` to be
used on map, collection, arrays and optional types.

Closes gh-32559
  • Loading branch information
philwebb committed Oct 1, 2022
1 parent 8a93abf commit efc431b
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 27 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -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;
Expand All @@ -25,13 +26,15 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import kotlin.reflect.KFunction;
import kotlin.reflect.KParameter;
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;
Expand Down Expand Up @@ -101,7 +104,7 @@ private <T> T getDefaultValue(Binder.Context context, ConstructorParameter param
if (annotation instanceof DefaultValue) {
String[] defaultValue = ((DefaultValue) annotation).value();
if (defaultValue.length == 0) {
return getNewInstanceIfPossible(context, type);
return getNewDefaultValueInstanceIfPossible(context, type);
}
return convertDefaultValue(context.getConverter(), defaultValue, type, annotations);
}
Expand All @@ -124,22 +127,35 @@ private <T> T convertDefaultValue(BindConverter converter, String[] defaultValue
}

@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.");
T instance = create(Bindable.of(type), context);
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) {
Expand Down
Expand Up @@ -29,6 +29,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;

Expand Down Expand Up @@ -853,6 +854,17 @@ void loadWhenBindingToConstructorParametersWithDefaultValuesShouldBind() {
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);
Expand Down Expand Up @@ -2145,6 +2157,45 @@ int getBar() {

}

@ConstructorBinding
@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 {
Expand Down Expand Up @@ -2225,6 +2276,11 @@ static class ConstructorParameterConfiguration {

}

@EnableConfigurationProperties(ConstructorParameterEmptyDefaultValueProperties.class)
static class ConstructorParameterEmptyDefaultValueConfiguration {

}

@EnableConfigurationProperties(ConstructorParameterWithUnitProperties.class)
static class ConstructorParameterWithUnitConfiguration {

Expand Down
Expand Up @@ -31,6 +31,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
Expand Down Expand Up @@ -293,29 +294,31 @@ void bindWhenJavaLangParameterWithEmptyDefaultValueShouldThrowException() {
}

@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
Expand Down Expand Up @@ -753,8 +756,7 @@ static class NestedConstructorBeanWithEmptyDefaultValueForArrayTypes {

private final String[] arrayValue;

NestedConstructorBeanWithEmptyDefaultValueForArrayTypes(@DefaultValue String[] arrayValue,
@DefaultValue Integer intValue) {
NestedConstructorBeanWithEmptyDefaultValueForArrayTypes(@DefaultValue String[] arrayValue) {
this.arrayValue = arrayValue;
}

Expand All @@ -764,6 +766,20 @@ String[] getArrayValue() {

}

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;
Expand Down

0 comments on commit efc431b

Please sign in to comment.