commit
8e82aad6ff
|
@ -17,6 +17,7 @@
|
||||||
package org.springframework.boot.context.properties.bind;
|
package org.springframework.boot.context.properties.bind;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.EnumMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
@ -65,8 +66,7 @@ class MapBinder extends AggregateBinder<Map<Object, Object>> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Map<Object, Object> map = CollectionFactory
|
Map<Object, Object> map = createMap(target);
|
||||||
.createMap((target.getValue() != null) ? Map.class : target.getType().resolve(Object.class), 0);
|
|
||||||
for (ConfigurationPropertySource source : getContext().getSources()) {
|
for (ConfigurationPropertySource source : getContext().getSources()) {
|
||||||
if (!ConfigurationPropertyName.EMPTY.equals(name)) {
|
if (!ConfigurationPropertyName.EMPTY.equals(name)) {
|
||||||
source = source.filter(name::isAncestorOf);
|
source = source.filter(name::isAncestorOf);
|
||||||
|
@ -76,6 +76,15 @@ class MapBinder extends AggregateBinder<Map<Object, Object>> {
|
||||||
return map.isEmpty() ? null : map;
|
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) {
|
private boolean hasDescendants(ConfigurationPropertyName name) {
|
||||||
for (ConfigurationPropertySource source : getContext().getSources()) {
|
for (ConfigurationPropertySource source : getContext().getSources()) {
|
||||||
if (source.containsDescendantOf(name) == ConfigurationPropertyState.PRESENT) {
|
if (source.containsDescendantOf(name) == ConfigurationPropertyState.PRESENT) {
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.EnumMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
@ -156,6 +157,10 @@ class ValueObjectBinder implements DataObjectBinder {
|
||||||
if (Collection.class.isAssignableFrom(resolved)) {
|
if (Collection.class.isAssignableFrom(resolved)) {
|
||||||
return (T) CollectionFactory.createCollection(resolved, 0);
|
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)) {
|
if (Map.class.isAssignableFrom(resolved)) {
|
||||||
return (T) CollectionFactory.createMap(resolved, 0);
|
return (T) CollectionFactory.createMap(resolved, 0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@ import java.net.InetAddress;
|
||||||
import java.util.AbstractMap;
|
import java.util.AbstractMap;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.EnumMap;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -79,6 +80,9 @@ class MapBinderTests {
|
||||||
private static final Bindable<Map<String, String[]>> STRING_ARRAY_MAP = Bindable.mapOf(String.class,
|
private static final Bindable<Map<String, String[]>> STRING_ARRAY_MAP = Bindable.mapOf(String.class,
|
||||||
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 List<ConfigurationPropertySource> sources = new ArrayList<>();
|
||||||
|
|
||||||
private final Binder binder = new Binder(this.sources);
|
private final Binder binder = new Binder(this.sources);
|
||||||
|
@ -637,6 +641,17 @@ class MapBinderTests {
|
||||||
assertThat(result.getCustomMap().getSource()).isEqualTo("value");
|
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) {
|
private <K, V> Bindable<Map<K, V>> getMapBindable(Class<K> keyGeneric, ResolvableType valueType) {
|
||||||
ResolvableType keyType = ResolvableType.forClass(keyGeneric);
|
ResolvableType keyType = ResolvableType.forClass(keyGeneric);
|
||||||
return Bindable.of(ResolvableType.forClassWithGenerics(Map.class, keyType, valueType));
|
return Bindable.of(ResolvableType.forClassWithGenerics(Map.class, keyType, valueType));
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.nio.file.Paths;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.EnumMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
@ -306,6 +307,13 @@ class ValueObjectBinderTests {
|
||||||
assertThat(bound.getMapValue()).isEmpty();
|
assertThat(bound.getMapValue()).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void bindWhenEnumMapParametersWithEmptyDefaultValueShouldReturnEmptyInstance() {
|
||||||
|
NestedConstructorBeanWithEmptyDefaultValueForEnumMapTypes bound = this.binder.bindOrCreate("foo",
|
||||||
|
Bindable.of(NestedConstructorBeanWithEmptyDefaultValueForEnumMapTypes.class));
|
||||||
|
assertThat(bound.getMapValue()).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void bindWhenArrayParameterWithEmptyDefaultValueShouldReturnEmptyInstance() {
|
void bindWhenArrayParameterWithEmptyDefaultValueShouldReturnEmptyInstance() {
|
||||||
NestedConstructorBeanWithEmptyDefaultValueForArrayTypes bound = this.binder.bindOrCreate("foo",
|
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 {
|
static class NestedConstructorBeanWithEmptyDefaultValueForArrayTypes {
|
||||||
|
|
||||||
private final String[] arrayValue;
|
private final String[] arrayValue;
|
||||||
|
|
Loading…
Reference in New Issue