Make Map<String,String> a special case in RelaxedDataBinder
Fixes gh-195
This commit is contained in:
parent
7b58718453
commit
2066c04a3e
|
|
@ -187,6 +187,14 @@ public class RelaxedDataBinder extends DataBinder {
|
||||||
if (descriptor == null || descriptor.isMap()) {
|
if (descriptor == null || descriptor.isMap()) {
|
||||||
if (descriptor != null) {
|
if (descriptor != null) {
|
||||||
wrapper.getPropertyValue(name + "[foo]");
|
wrapper.getPropertyValue(name + "[foo]");
|
||||||
|
TypeDescriptor valueDescriptor = descriptor.getMapValueTypeDescriptor();
|
||||||
|
if (valueDescriptor != null) {
|
||||||
|
Class<?> valueType = valueDescriptor.getObjectType();
|
||||||
|
if (valueType != null
|
||||||
|
&& CharSequence.class.isAssignableFrom(valueType)) {
|
||||||
|
path.collapseKeys(index);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
path.mapIndex(index);
|
path.mapIndex(index);
|
||||||
extendMapIfNecessary(wrapper, path, index);
|
extendMapIfNecessary(wrapper, path, index);
|
||||||
|
|
@ -278,6 +286,22 @@ public class RelaxedDataBinder extends DataBinder {
|
||||||
this.nodes = splitPath(path);
|
this.nodes = splitPath(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void collapseKeys(int index) {
|
||||||
|
List<PathNode> revised = new ArrayList<PathNode>();
|
||||||
|
for (int i = 0; i < index; i++) {
|
||||||
|
revised.add(this.nodes.get(i));
|
||||||
|
}
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
for (int i = index; i < this.nodes.size(); i++) {
|
||||||
|
if (i > index) {
|
||||||
|
builder.append(".");
|
||||||
|
}
|
||||||
|
builder.append(this.nodes.get(i).name);
|
||||||
|
}
|
||||||
|
revised.add(new PropertyNode(builder.toString()));
|
||||||
|
this.nodes = revised;
|
||||||
|
}
|
||||||
|
|
||||||
public void mapIndex(int index) {
|
public void mapIndex(int index) {
|
||||||
PathNode node = this.nodes.get(index);
|
PathNode node = this.nodes.get(index);
|
||||||
if (node instanceof PropertyNode) {
|
if (node instanceof PropertyNode) {
|
||||||
|
|
|
||||||
|
|
@ -250,6 +250,14 @@ public class RelaxedDataBinderTests {
|
||||||
assertEquals("123", target.getNested().get("value"));
|
assertEquals("123", target.getNested().get("value"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBindNestedMapOfString() throws Exception {
|
||||||
|
TargetWithNestedMapOfString target = new TargetWithNestedMapOfString();
|
||||||
|
bind(target, "nested.foo: bar\n" + "nested.value.foo: 123");
|
||||||
|
assertEquals("bar", target.getNested().get("foo"));
|
||||||
|
assertEquals("123", target.getNested().get("value.foo"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBindNestedMapBracketReferenced() throws Exception {
|
public void testBindNestedMapBracketReferenced() throws Exception {
|
||||||
TargetWithNestedMap target = new TargetWithNestedMap();
|
TargetWithNestedMap target = new TargetWithNestedMap();
|
||||||
|
|
@ -469,6 +477,18 @@ public class RelaxedDataBinderTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class TargetWithNestedMapOfString {
|
||||||
|
private Map<String, String> nested;
|
||||||
|
|
||||||
|
public Map<String, String> getNested() {
|
||||||
|
return this.nested;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNested(Map<String, String> nested) {
|
||||||
|
this.nested = nested;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static class TargetWithNestedMapOfListOfString {
|
public static class TargetWithNestedMapOfListOfString {
|
||||||
private Map<String, List<String>> nested;
|
private Map<String, List<String>> nested;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,6 @@ import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
import org.hamcrest.Matchers;
|
import org.hamcrest.Matchers;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.rules.ExpectedException;
|
import org.junit.rules.ExpectedException;
|
||||||
|
|
@ -326,7 +325,6 @@ public class EnableConfigurationPropertiesTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore("This is challenging, so maybe not supportable?")
|
|
||||||
public void testBindingWithMapKeyWithPeriod() {
|
public void testBindingWithMapKeyWithPeriod() {
|
||||||
this.context.register(ResourceBindingPropertiesWithMap.class);
|
this.context.register(ResourceBindingPropertiesWithMap.class);
|
||||||
this.context.refresh();
|
this.context.refresh();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue