Skip to content

Commit 2066c04

Browse files
author
Dave Syer
committed
Make Map<String,String> a special case in RelaxedDataBinder
Fixes spring-projectsgh-195
1 parent 7b58718 commit 2066c04

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

spring-boot/src/main/java/org/springframework/boot/bind/RelaxedDataBinder.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,14 @@ private String initializePath(BeanWrapper wrapper, BeanPath path, int index) {
187187
if (descriptor == null || descriptor.isMap()) {
188188
if (descriptor != null) {
189189
wrapper.getPropertyValue(name + "[foo]");
190+
TypeDescriptor valueDescriptor = descriptor.getMapValueTypeDescriptor();
191+
if (valueDescriptor != null) {
192+
Class<?> valueType = valueDescriptor.getObjectType();
193+
if (valueType != null
194+
&& CharSequence.class.isAssignableFrom(valueType)) {
195+
path.collapseKeys(index);
196+
}
197+
}
190198
}
191199
path.mapIndex(index);
192200
extendMapIfNecessary(wrapper, path, index);
@@ -278,6 +286,22 @@ public BeanPath(String path) {
278286
this.nodes = splitPath(path);
279287
}
280288

289+
public void collapseKeys(int index) {
290+
List<PathNode> revised = new ArrayList<PathNode>();
291+
for (int i = 0; i < index; i++) {
292+
revised.add(this.nodes.get(i));
293+
}
294+
StringBuilder builder = new StringBuilder();
295+
for (int i = index; i < this.nodes.size(); i++) {
296+
if (i > index) {
297+
builder.append(".");
298+
}
299+
builder.append(this.nodes.get(i).name);
300+
}
301+
revised.add(new PropertyNode(builder.toString()));
302+
this.nodes = revised;
303+
}
304+
281305
public void mapIndex(int index) {
282306
PathNode node = this.nodes.get(index);
283307
if (node instanceof PropertyNode) {

spring-boot/src/test/java/org/springframework/boot/bind/RelaxedDataBinderTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,14 @@ public void testBindNestedMap() throws Exception {
250250
assertEquals("123", target.getNested().get("value"));
251251
}
252252

253+
@Test
254+
public void testBindNestedMapOfString() throws Exception {
255+
TargetWithNestedMapOfString target = new TargetWithNestedMapOfString();
256+
bind(target, "nested.foo: bar\n" + "nested.value.foo: 123");
257+
assertEquals("bar", target.getNested().get("foo"));
258+
assertEquals("123", target.getNested().get("value.foo"));
259+
}
260+
253261
@Test
254262
public void testBindNestedMapBracketReferenced() throws Exception {
255263
TargetWithNestedMap target = new TargetWithNestedMap();
@@ -469,6 +477,18 @@ public void setNested(Map<String, Object> nested) {
469477
}
470478
}
471479

480+
public static class TargetWithNestedMapOfString {
481+
private Map<String, String> nested;
482+
483+
public Map<String, String> getNested() {
484+
return this.nested;
485+
}
486+
487+
public void setNested(Map<String, String> nested) {
488+
this.nested = nested;
489+
}
490+
}
491+
472492
public static class TargetWithNestedMapOfListOfString {
473493
private Map<String, List<String>> nested;
474494

spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesTests.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import org.hamcrest.Matchers;
2828
import org.junit.After;
29-
import org.junit.Ignore;
3029
import org.junit.Rule;
3130
import org.junit.Test;
3231
import org.junit.rules.ExpectedException;
@@ -326,7 +325,6 @@ public void testMultipleExplicitTypes() throws Exception {
326325
}
327326

328327
@Test
329-
@Ignore("This is challenging, so maybe not supportable?")
330328
public void testBindingWithMapKeyWithPeriod() {
331329
this.context.register(ResourceBindingPropertiesWithMap.class);
332330
this.context.refresh();

0 commit comments

Comments
 (0)