Use RelaxedNames to search for prefix in RelaxedBinder

RelaxedDataBinder now supports "env var" style variables that include the
path prefix, e.g. FOO_BAR_BAZ=boom will bind to a bean with property "baz"
and a binder with prefix "foo.bar".

Fixes gh-98
pull/85/merge
Dave Syer 11 years ago
parent eff587d5b0
commit 8d186945e7

@ -121,11 +121,13 @@ public class RelaxedDataBinder extends DataBinder {
MutablePropertyValues rtn = new MutablePropertyValues();
for (PropertyValue pv : propertyValues.getPropertyValues()) {
String name = pv.getName();
if (name.startsWith(this.namePrefix)) {
name = name.substring(this.namePrefix.length());
for (String candidate : new RelaxedNames(this.namePrefix)) {
if (name.startsWith(candidate)) {
name = name.substring(candidate.length());
rtn.add(name, pv.getValue());
}
}
}
return rtn;
}

@ -41,7 +41,6 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.NotWritablePropertyException;
import org.springframework.boot.bind.RelaxedDataBinder;
import org.springframework.context.support.StaticMessageSource;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
@ -75,6 +74,34 @@ public class RelaxedDataBinderTests {
assertEquals("bar", target.getFoo());
}
@Test
public void testBindStringWithPrefix() throws Exception {
VanillaTarget target = new VanillaTarget();
bind(target, "test.foo: bar", "test");
assertEquals("bar", target.getFoo());
}
@Test
public void testBindFromEnvironmentStyleWithPrefix() throws Exception {
VanillaTarget target = new VanillaTarget();
bind(target, "TEST_FOO: bar", "test");
assertEquals("bar", target.getFoo());
}
@Test
public void testBindFromEnvironmentStyleWithNestedPrefix() throws Exception {
VanillaTarget target = new VanillaTarget();
bind(target, "TEST_IT_FOO: bar", "test.it");
assertEquals("bar", target.getFoo());
}
@Test
public void testBindCapitals() throws Exception {
VanillaTarget target = new VanillaTarget();
bind(target, "FOO: bar");
assertEquals("bar", target.getFoo());
}
@Test
public void testBindUnderscoreInActualPropertyName() throws Exception {
VanillaTarget target = new VanillaTarget();
@ -215,8 +242,6 @@ public class RelaxedDataBinderTests {
}
@Test
// @Ignore("Should be possible but currently not supported")
// FIXME: bind to map containing beans
public void testBindNestedMapOfBean() throws Exception {
TargetWithNestedMapOfBean target = new TargetWithNestedMapOfBean();
bind(target, "nested.foo.foo: bar\n" + "nested.bar.foo: bucket");
@ -225,8 +250,6 @@ public class RelaxedDataBinderTests {
}
@Test
// @Ignore("Should be possible but currently not supported")
// FIXME: bind to map containing beans
public void testBindNestedMapOfListOfBean() throws Exception {
TargetWithNestedMapOfListOfBean target = new TargetWithNestedMapOfListOfBean();
bind(target, "nested.foo[0].foo: bar\n" + "nested.bar[0].foo: bucket\n"

Loading…
Cancel
Save