Skip to content

Commit 6056b96

Browse files
committed
Fix relaxed binding of camel-case properties
Extended RelaxedDataBinder to include a case insensitive version of SEPARATED_TO_CAMELCASE. This allow properties of the form TEST_THE_VALUE to to bound to a @ConfigurationProperties class with a prefix of "test" and a field of `theValue`. Fixes gh-2304
1 parent 3cd40e2 commit 6056b96

File tree

4 files changed

+45
-23
lines changed

4 files changed

+45
-23
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -201,7 +201,6 @@ protected String normalizePath(BeanWrapper wrapper, String path) {
201201
}
202202

203203
private String initializePath(BeanWrapper wrapper, BeanPath path, int index) {
204-
205204
String prefix = path.prefix(index);
206205
String key = path.name(index);
207206
if (path.isProperty(index)) {

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

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2013 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -158,22 +158,34 @@ public String apply(String value) {
158158
SEPARATED_TO_CAMELCASE {
159159
@Override
160160
public String apply(String value) {
161-
StringBuilder builder = new StringBuilder();
162-
for (String field : value.split("[_\\-.]")) {
163-
builder.append(builder.length() == 0 ? field : StringUtils
164-
.capitalize(field));
165-
}
166-
for (String suffix : new String[] { "_", "-", "." }) {
167-
if (value.endsWith(suffix)) {
168-
builder.append(suffix);
169-
}
170-
}
171-
return builder.toString();
161+
return separatedToCamelCase(value, false);
162+
}
163+
},
164+
165+
CASE_INSENSITIVE_SEPARATED_TO_CAMELCASE {
166+
@Override
167+
public String apply(String value) {
168+
return separatedToCamelCase(value, true);
172169
}
173170
};
174171

175172
public abstract String apply(String value);
176173

174+
private static String separatedToCamelCase(String value, boolean caseInsensitive) {
175+
StringBuilder builder = new StringBuilder();
176+
for (String field : value.split("[_\\-.]")) {
177+
field = (caseInsensitive ? field.toLowerCase() : field);
178+
builder.append(builder.length() == 0 ? field : StringUtils
179+
.capitalize(field));
180+
}
181+
for (String suffix : new String[] { "_", "-", "." }) {
182+
if (value.endsWith(suffix)) {
183+
builder.append(suffix);
184+
}
185+
}
186+
return builder.toString();
187+
188+
}
177189
}
178190

179191
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2013 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@ public void iterator() throws Exception {
3737
assertThat(iterator.next(), equalTo("my-RELAXED-property"));
3838
assertThat(iterator.next(), equalTo("my_RELAXED_property"));
3939
assertThat(iterator.next(), equalTo("myRELAXEDProperty"));
40+
assertThat(iterator.next(), equalTo("myRelaxedProperty"));
4041
assertThat(iterator.next(), equalTo("my-relaxed-property"));
4142
assertThat(iterator.next(), equalTo("my_relaxed_property"));
4243
assertThat(iterator.next(), equalTo("myrelaxedproperty"));

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -136,10 +136,20 @@ public void testInitializersSeeBoundProperties() {
136136
@Test
137137
public void testPropertyWithEnum() throws Exception {
138138
this.context = new AnnotationConfigApplicationContext();
139-
EnvironmentTestUtils.addEnvironment(this.context, "test.value:foo");
139+
EnvironmentTestUtils.addEnvironment(this.context, "test.the-value:foo");
140140
this.context.register(PropertyWithEnum.class);
141141
this.context.refresh();
142-
assertThat(this.context.getBean(PropertyWithEnum.class).getValue(),
142+
assertThat(this.context.getBean(PropertyWithEnum.class).getTheValue(),
143+
equalTo(FooEnum.FOO));
144+
}
145+
146+
@Test
147+
public void testRelaxedPropertyWithEnum() throws Exception {
148+
this.context = new AnnotationConfigApplicationContext();
149+
EnvironmentTestUtils.addEnvironment(this.context, "TEST_THE_VALUE:FoO");
150+
this.context.register(PropertyWithEnum.class);
151+
this.context.refresh();
152+
assertThat(this.context.getBean(PropertyWithEnum.class).getTheValue(),
143153
equalTo(FooEnum.FOO));
144154
}
145155

@@ -349,14 +359,14 @@ public void setChars(char[] chars) {
349359
@ConfigurationProperties(prefix = "test")
350360
public static class PropertyWithEnum {
351361

352-
private FooEnum value;
362+
private FooEnum theValue;
353363

354-
public void setValue(FooEnum value) {
355-
this.value = value;
364+
public void setTheValue(FooEnum value) {
365+
this.theValue = value;
356366
}
357367

358-
public FooEnum getValue() {
359-
return this.value;
368+
public FooEnum getTheValue() {
369+
return this.theValue;
360370
}
361371

362372
}

0 commit comments

Comments
 (0)