Skip to content

Commit

Permalink
Adapt field name in ValidationBindHandler to a valid ConfigurationPro…
Browse files Browse the repository at this point in the history
…pertyName

Fixes spring-projectsgh-19580
  • Loading branch information
mbhave committed Jan 10, 2020
1 parent ccf4e1a commit a017b89
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
*
* @author Phillip Webb
* @author Madhura Bhave
* @since 2.2.3
* @see DataObjectBinder
*/
abstract class DataObjectPropertyName {
public abstract class DataObjectPropertyName {

private DataObjectPropertyName() {
}
Expand All @@ -33,7 +34,7 @@ private DataObjectPropertyName() {
* @param name the source name
* @return the dashed from
*/
static String toDashedForm(String name) {
public static String toDashedForm(String name) {
StringBuilder result = new StringBuilder();
String replaced = name.replace('_', '-');
for (int i = 0; i < replaced.length(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.boot.context.properties.bind.BindContext;
import org.springframework.boot.context.properties.bind.BindHandler;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.DataObjectPropertyName;
import org.springframework.boot.context.properties.source.ConfigurationProperty;
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
import org.springframework.core.ResolvableType;
Expand Down Expand Up @@ -187,7 +188,7 @@ protected Object getActualFieldValue(String field) {
}

private ConfigurationPropertyName getName(String field) {
return this.name.append(field);
return this.name.append(DataObjectPropertyName.toDashedForm(field));
}

ValidationErrors getValidationErrors() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ void bindShouldValidateIfOtherHandlersInChainReplaceErrorWithResult() {
.satisfies((ex) -> assertThat(ex.getCause()).hasMessageContaining("years"));
}

@Test
void validationErrorsForCamelCaseFieldsShouldContainRejectedValue() {
this.sources.add(new MockConfigurationPropertySource("foo.inner.person-age", 2));
BindValidationException cause = bindAndExpectValidationError(() -> this.binder
.bind(ConfigurationPropertyName.of("foo"), Bindable.of(ExampleCamelCase.class), this.handler));
assertThat(cause.getMessage()).contains("rejected value [2]");
}

private BindValidationException bindAndExpectValidationError(Runnable action) {
try {
action.run();
Expand Down Expand Up @@ -305,6 +313,33 @@ void setAddress(String address) {

}

@Validated
static class ExampleCamelCase {

@Valid
private InnerProperties inner = new InnerProperties();

InnerProperties getInner() {
return this.inner;
}

static class InnerProperties {

@Min(5)
private int personAge;

int getPersonAge() {
return this.personAge;
}

void setPersonAge(int personAge) {
this.personAge = personAge;
}

}

}

@Validated
static class ExampleValidatedBeanWithGetterException {

Expand Down

0 comments on commit a017b89

Please sign in to comment.