From 76729fdf8568f52358e97a8df32a4c7ffe16149f Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Sun, 13 Jul 2014 07:54:51 +0200 Subject: [PATCH 1/2] Propagate RelaxedConversionService Prior to this commit, the internal BeanWrapper used by the RelaxedDataBinder was not using any conversion service at all. This commit makes sure to propagate the one that has been configured internally. Also a dummy call to a [foo] key seems to have been left and has been removed. This allows to support Enum as map key. Fixes gh-1242 --- .../boot/bind/RelaxedDataBinder.java | 16 +++++++++++--- .../boot/bind/RelaxedDataBinderTests.java | 22 +++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedDataBinder.java b/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedDataBinder.java index e6b942db4ac..36465b2a0dd 100644 --- a/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedDataBinder.java +++ b/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedDataBinder.java @@ -28,6 +28,7 @@ import org.springframework.beans.BeanWrapperImpl; import org.springframework.beans.InvalidPropertyException; import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.PropertyValue; +import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.TypeDescriptor; import org.springframework.util.StringUtils; import org.springframework.validation.DataBinder; @@ -47,6 +48,8 @@ public class RelaxedDataBinder extends DataBinder { private boolean ignoreNestedProperties; + private ConversionService relaxedConversionService; + /** * Create a new {@link RelaxedDataBinder} instance. * @param target the target into which properties are bound @@ -76,12 +79,19 @@ public class RelaxedDataBinder extends DataBinder { this.ignoreNestedProperties = ignoreNestedProperties; } + @Override + public void setConversionService(ConversionService conversionService) { + super.setConversionService(conversionService); + this.relaxedConversionService = new RelaxedConversionService(getConversionService()); + } + @Override public void initBeanPropertyAccess() { super.initBeanPropertyAccess(); + this.relaxedConversionService = (this.relaxedConversionService != null + ? this.relaxedConversionService : new RelaxedConversionService(getConversionService())); // Hook in the RelaxedConversionService - getInternalBindingResult().initConversion( - new RelaxedConversionService(getConversionService())); + getInternalBindingResult().initConversion(relaxedConversionService); } @Override @@ -111,6 +121,7 @@ public class RelaxedDataBinder extends DataBinder { } BeanWrapper targetWrapper = new BeanWrapperImpl(target); + targetWrapper.setConversionService(this.relaxedConversionService); targetWrapper.setAutoGrowNestedPaths(true); List list = propertyValues.getPropertyValueList(); @@ -189,7 +200,6 @@ public class RelaxedDataBinder extends DataBinder { TypeDescriptor descriptor = wrapper.getPropertyTypeDescriptor(name); if (descriptor == null || descriptor.isMap()) { if (descriptor != null) { - wrapper.getPropertyValue(name + "[foo]"); TypeDescriptor valueDescriptor = descriptor.getMapValueTypeDescriptor(); if (valueDescriptor != null) { Class valueType = valueDescriptor.getObjectType(); diff --git a/spring-boot/src/test/java/org/springframework/boot/bind/RelaxedDataBinderTests.java b/spring-boot/src/test/java/org/springframework/boot/bind/RelaxedDataBinderTests.java index 9ac75a252e4..b918e6e9784 100644 --- a/spring-boot/src/test/java/org/springframework/boot/bind/RelaxedDataBinderTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/bind/RelaxedDataBinderTests.java @@ -284,6 +284,15 @@ public class RelaxedDataBinderTests { assertEquals("123", target.getNested().get("value.foo")); } + @Test + public void testBindNestedMapOfEnum() throws Exception { + this.conversionService = new DefaultConversionService(); + TargetWithNestedMapOfEnum target = new TargetWithNestedMapOfEnum(); + bind(target, "nested.this: bar\n" + "nested.ThAt: 123"); + assertEquals("bar", target.getNested().get(Bingo.THIS)); + assertEquals("123", target.getNested().get(Bingo.THAT)); + } + @Test public void testBindNestedMapBracketReferenced() throws Exception { TargetWithNestedMap target = new TargetWithNestedMap(); @@ -578,6 +587,19 @@ public class RelaxedDataBinderTests { } + public static class TargetWithNestedMapOfEnum { + + private Map nested; + + public Map getNested() { + return nested; + } + + public void setNested(Map nested) { + this.nested = nested; + } + } + public static class TargetWithNestedMapOfListOfString { private Map> nested; From 0c6a0bde5f20ef1909336cc929549f5df1eeb3c8 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 4 Aug 2014 15:47:38 -0700 Subject: [PATCH 2/2] Export SPRING_HOME for commands to consume if needed --- spring-boot-cli/src/main/executablecontent/bin/spring | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot-cli/src/main/executablecontent/bin/spring b/spring-boot-cli/src/main/executablecontent/bin/spring index c9aec1e479e..c13b79b31b8 100755 --- a/spring-boot-cli/src/main/executablecontent/bin/spring +++ b/spring-boot-cli/src/main/executablecontent/bin/spring @@ -69,7 +69,7 @@ if [ -z "${SPRING_HOME}" ]; then done SAVED="`pwd`" cd "`dirname \"$PRG\"`/../" >&- - SPRING_HOME="`pwd -P`" + export SPRING_HOME="`pwd -P`" cd "$SAVED" >&- fi