Improve property target name
Previously, non camel case properties were wrongly resolved, i.e. getFOO() leading to a 'f-o-o'. While unusual, underscores can also be added to a property name. In that case, the hyphen should not be added as the binder consider this to be a single "word". Typically setFoo_Bar on the "something" prefix is mapped using "something.foo_bar". All these cases are now handled properly, generating the target name that the binder expects. Fixes gh-2118
This commit is contained in:
@@ -19,6 +19,8 @@ package org.springframework.boot.configurationprocessor.metadata;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Configuration meta-data.
|
||||
@@ -30,6 +32,8 @@ import java.util.List;
|
||||
*/
|
||||
public class ConfigurationMetadata {
|
||||
|
||||
private static final Pattern CAMEL_CASE_PATTERN = Pattern.compile("([^A-Z-])([A-Z])");
|
||||
|
||||
private final List<ItemMetadata> items;
|
||||
|
||||
public ConfigurationMetadata() {
|
||||
@@ -73,15 +77,22 @@ public class ConfigurationMetadata {
|
||||
}
|
||||
|
||||
static String toDashedCase(String name) {
|
||||
StringBuilder dashed = new StringBuilder();
|
||||
for (int i = 0; i < name.length(); i++) {
|
||||
char c = name.charAt(i);
|
||||
if (Character.isUpperCase(c) && dashed.length() > 0) {
|
||||
dashed.append("-");
|
||||
Matcher matcher = CAMEL_CASE_PATTERN.matcher(name);
|
||||
StringBuffer result = new StringBuffer();
|
||||
while (matcher.find()) {
|
||||
String first = matcher.group(1);
|
||||
String second = matcher.group(2);
|
||||
String target;
|
||||
if (first.equals("_")) { // not a word for the binder
|
||||
target = first + second;
|
||||
} else {
|
||||
target = first + "-" + second;
|
||||
}
|
||||
dashed.append(Character.toLowerCase(c));
|
||||
matcher.appendReplacement(result,target);
|
||||
}
|
||||
return dashed.toString();
|
||||
matcher.appendTail(result);
|
||||
String value = result.toString();
|
||||
return value.toLowerCase();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.configurationprocessor.metadata;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link ConfigurationMetadata}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class ConfigurationMetadataTests {
|
||||
|
||||
@Test
|
||||
public void toDashedCaseCamelCase() {
|
||||
assertThat(ConfigurationMetadata.toDashedCase("simpleCamelCase"), is("simple-camel-case"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toDashedCaseWordsUnderScore() {
|
||||
assertThat(ConfigurationMetadata.toDashedCase("Word_With_underscore"), is("word_with_underscore"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toDashedCaseWordsSeveralUnderScores() {
|
||||
assertThat(ConfigurationMetadata.toDashedCase("Word___With__underscore"), is("word___with__underscore"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toDashedCaseLowerCaseUnderscore() {
|
||||
assertThat(ConfigurationMetadata.toDashedCase("lower_underscore"), is("lower_underscore"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toDashedCaseUpperUnderscore() {
|
||||
assertThat(ConfigurationMetadata.toDashedCase("UPPER_UNDERSCORE"), is("upper_underscore"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toDashedCaseMultipleUnderscores() {
|
||||
assertThat(ConfigurationMetadata.toDashedCase("super___crazy"), is("super___crazy"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toDashedCaseUppercase() {
|
||||
assertThat(ConfigurationMetadata.toDashedCase("UPPERCASE"), is("uppercase"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toDashedCaseLowercase() {
|
||||
assertThat(ConfigurationMetadata.toDashedCase("lowercase"), is("lowercase"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user