Merge branch '3.2.x' into 3.3.x

Closes gh-41563
This commit is contained in:
Andy Wilkinson 2024-07-19 16:29:15 +01:00
commit 8e82aad6ff
4 changed files with 55 additions and 4 deletions

View File

@ -17,6 +17,7 @@
package org.springframework.boot.context.properties.bind;
import java.util.Collection;
import java.util.EnumMap;
import java.util.Map;
import java.util.Properties;
import java.util.function.Supplier;
@ -65,8 +66,7 @@ class MapBinder extends AggregateBinder<Map<Object, Object>> {
}
}
}
Map<Object, Object> map = CollectionFactory
.createMap((target.getValue() != null) ? Map.class : target.getType().resolve(Object.class), 0);
Map<Object, Object> map = createMap(target);
for (ConfigurationPropertySource source : getContext().getSources()) {
if (!ConfigurationPropertyName.EMPTY.equals(name)) {
source = source.filter(name::isAncestorOf);
@ -76,6 +76,15 @@ class MapBinder extends AggregateBinder<Map<Object, Object>> {
return map.isEmpty() ? null : map;
}
private Map<Object, Object> createMap(Bindable<?> target) {
Class<?> mapType = (target.getValue() != null) ? Map.class : target.getType().resolve(Object.class);
if (EnumMap.class.isAssignableFrom(mapType)) {
Class<?> keyType = target.getType().asMap().getGeneric(0).resolve();
return CollectionFactory.createMap(mapType, keyType, 0);
}
return CollectionFactory.createMap(mapType, 0);
}
private boolean hasDescendants(ConfigurationPropertyName name) {
for (ConfigurationPropertySource source : getContext().getSources()) {
if (source.containsDescendantOf(name) == ConfigurationPropertyState.PRESENT) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
@ -25,6 +25,7 @@ import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@ -156,6 +157,10 @@ class ValueObjectBinder implements DataObjectBinder {
if (Collection.class.isAssignableFrom(resolved)) {
return (T) CollectionFactory.createCollection(resolved, 0);
}
if (EnumMap.class.isAssignableFrom(resolved)) {
Class<?> keyType = type.asMap().getGeneric(0).resolve();
return (T) CollectionFactory.createMap(resolved, keyType, 0);
}
if (Map.class.isAssignableFrom(resolved)) {
return (T) CollectionFactory.createMap(resolved, 0);
}

View File

@ -20,6 +20,7 @@ import java.net.InetAddress;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
@ -79,6 +80,9 @@ class MapBinderTests {
private static final Bindable<Map<String, String[]>> STRING_ARRAY_MAP = Bindable.mapOf(String.class,
String[].class);
private static final Bindable<EnumMap<ExampleEnum, String>> EXAMPLE_ENUM_STRING_ENUM_MAP = Bindable
.of(ResolvableType.forClassWithGenerics(EnumMap.class, ExampleEnum.class, String.class));
private final List<ConfigurationPropertySource> sources = new ArrayList<>();
private final Binder binder = new Binder(this.sources);
@ -637,6 +641,17 @@ class MapBinderTests {
assertThat(result.getCustomMap().getSource()).isEqualTo("value");
}
@Test
void bindToEnumMapShouldBind() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("props.foo-bar", "value");
this.sources.add(source);
Binder binder = new Binder(this.sources, null, null, null);
EnumMap<ExampleEnum, String> result = binder.bind("props", EXAMPLE_ENUM_STRING_ENUM_MAP).get();
assertThat(result).hasSize(1).containsEntry(ExampleEnum.FOO_BAR, "value");
}
private <K, V> Bindable<Map<K, V>> getMapBindable(Class<K> keyGeneric, ResolvableType valueType) {
ResolvableType keyType = ResolvableType.forClass(keyGeneric);
return Bindable.of(ResolvableType.forClassWithGenerics(Map.class, keyType, valueType));

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
@ -21,6 +21,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@ -306,6 +307,13 @@ class ValueObjectBinderTests {
assertThat(bound.getMapValue()).isEmpty();
}
@Test
void bindWhenEnumMapParametersWithEmptyDefaultValueShouldReturnEmptyInstance() {
NestedConstructorBeanWithEmptyDefaultValueForEnumMapTypes bound = this.binder.bindOrCreate("foo",
Bindable.of(NestedConstructorBeanWithEmptyDefaultValueForEnumMapTypes.class));
assertThat(bound.getMapValue()).isEmpty();
}
@Test
void bindWhenArrayParameterWithEmptyDefaultValueShouldReturnEmptyInstance() {
NestedConstructorBeanWithEmptyDefaultValueForArrayTypes bound = this.binder.bindOrCreate("foo",
@ -781,6 +789,20 @@ class ValueObjectBinderTests {
}
static class NestedConstructorBeanWithEmptyDefaultValueForEnumMapTypes {
private final EnumMap<ExampleEnum, String> mapValue;
NestedConstructorBeanWithEmptyDefaultValueForEnumMapTypes(@DefaultValue EnumMap<ExampleEnum, String> mapValue) {
this.mapValue = mapValue;
}
EnumMap<ExampleEnum, String> getMapValue() {
return this.mapValue;
}
}
static class NestedConstructorBeanWithEmptyDefaultValueForArrayTypes {
private final String[] arrayValue;