Skip to content

Commit 53251a9

Browse files
committed
Add suffix dot on prefix only if necessary
Closes spring-projectsgh-3787
1 parent 1466169 commit 53251a9

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
*
4646
* @author Dave Syer
4747
* @author Phillip Webb
48+
* @author Stephane Nicoll
4849
* @see RelaxedNames
4950
*/
5051
public class RelaxedDataBinder extends DataBinder {
@@ -73,7 +74,14 @@ public RelaxedDataBinder(Object target) {
7374
public RelaxedDataBinder(Object target, String namePrefix) {
7475
super(wrapTarget(target), (StringUtils.hasLength(namePrefix) ? namePrefix
7576
: DEFAULT_OBJECT_NAME));
76-
this.namePrefix = (StringUtils.hasLength(namePrefix) ? namePrefix + "." : null);
77+
this.namePrefix = cleanNamePrefix(namePrefix);
78+
}
79+
80+
private static String cleanNamePrefix(String namePrefix) {
81+
if (!StringUtils.hasLength(namePrefix)) {
82+
return null;
83+
}
84+
return (namePrefix.endsWith(".") ? namePrefix : namePrefix + ".");
7785
}
7886

7987
/**

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

+7
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ public void testBindStringWithPrefix() throws Exception {
9595
assertEquals("bar", target.getFoo());
9696
}
9797

98+
@Test
99+
public void testBindStringWithPrefixDotSuffix() throws Exception {
100+
VanillaTarget target = new VanillaTarget();
101+
bind(target, "some.test.foo: bar", "some.test.");
102+
assertEquals("bar", target.getFoo());
103+
}
104+
98105
@Test
99106
public void testBindFromEnvironmentStyleWithPrefix() throws Exception {
100107
VanillaTarget target = new VanillaTarget();

0 commit comments

Comments
 (0)