Skip to content

Commit 452281c

Browse files
committed
Fix property detection in SpringApplicationBuilder
Update SpringApplicationBuilder so that properties of the form `abc=d:e:f` are correctly parsed. Prior to this commit the `:` delimiter would always be chosen over `=`, even if `=` occurred first. Fixes gh-6121
1 parent f27bdcb commit 452281c

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -394,20 +394,28 @@ public SpringApplicationBuilder properties(String... defaultProperties) {
394394
return properties(getMapFromKeyValuePairs(defaultProperties));
395395
}
396396

397-
private Map<String, Object> getMapFromKeyValuePairs(String[] args) {
397+
private Map<String, Object> getMapFromKeyValuePairs(String[] properties) {
398398
Map<String, Object> map = new HashMap<String, Object>();
399-
for (String pair : args) {
400-
int index = pair.indexOf(":");
401-
if (index <= 0) {
402-
index = pair.indexOf("=");
403-
}
404-
String key = pair.substring(0, index > 0 ? index : pair.length());
405-
String value = index > 0 ? pair.substring(index + 1) : "";
399+
for (String property : properties) {
400+
int index = lowestIndexOf(property, ":", "=");
401+
String key = property.substring(0, index > 0 ? index : property.length());
402+
String value = index > 0 ? property.substring(index + 1) : "";
406403
map.put(key, value);
407404
}
408405
return map;
409406
}
410407

408+
private int lowestIndexOf(String property, String... candidates) {
409+
int index = -1;
410+
for (String candidate : candidates) {
411+
int candidateIndex = property.indexOf(candidate);
412+
if (candidateIndex > 0) {
413+
index = (index == -1 ? candidateIndex : Math.min(index, candidateIndex));
414+
}
415+
}
416+
return index;
417+
}
418+
411419
/**
412420
* Default properties for the environment in the form {@code key=value} or
413421
* {@code key:value}.

spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3232
import org.springframework.context.annotation.Configuration;
3333
import org.springframework.context.support.StaticApplicationContext;
34+
import org.springframework.core.env.ConfigurableEnvironment;
3435
import org.springframework.core.env.StandardEnvironment;
3536
import org.springframework.core.io.DefaultResourceLoader;
3637
import org.springframework.core.io.ResourceLoader;
@@ -91,6 +92,20 @@ public void propertiesAsProperties() throws Exception {
9192
assertThat(this.context.getEnvironment().getProperty("bar"), is(equalTo("foo")));
9293
}
9394

95+
@Test
96+
public void propertiesWithRepeatSeparator() throws Exception {
97+
SpringApplicationBuilder application = new SpringApplicationBuilder()
98+
.sources(ExampleConfig.class).contextClass(StaticApplicationContext.class)
99+
.properties("one=c:\\logging.file", "two=a:b", "three:c:\\logging.file",
100+
"four:a:b");
101+
this.context = application.run();
102+
ConfigurableEnvironment environment = this.context.getEnvironment();
103+
assertThat(environment.getProperty("one"), is(equalTo("c:\\logging.file")));
104+
assertThat(environment.getProperty("two"), is(equalTo("a:b")));
105+
assertThat(environment.getProperty("three"), is(equalTo("c:\\logging.file")));
106+
assertThat(environment.getProperty("four"), is(equalTo("a:b")));
107+
}
108+
94109
@Test
95110
public void specificApplicationContextClass() throws Exception {
96111
SpringApplicationBuilder application = new SpringApplicationBuilder()

0 commit comments

Comments
 (0)