diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/validation/MessageSourceMessageInterpolator.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/validation/MessageSourceMessageInterpolator.java index e63ce39c98..a1ab317ca7 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/validation/MessageSourceMessageInterpolator.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/validation/MessageSourceMessageInterpolator.java @@ -30,6 +30,7 @@ import org.springframework.context.i18n.LocaleContextHolder; * message using the underlying {@link MessageInterpolator}. * * @author Dmytro Nosan + * @author Scott Frederick */ class MessageSourceMessageInterpolator implements MessageInterpolator { @@ -115,7 +116,12 @@ class MessageSourceMessageInterpolator implements MessageInterpolator { private String replaceParameter(String parameter, Locale locale, Set visitedParameters) { parameter = replaceParameters(parameter, locale, visitedParameters); String value = this.messageSource.getMessage(parameter, null, null, locale); - return (value != null) ? replaceParameters(value, locale, visitedParameters) : null; + return (value != null && !isUsingCodeAsDefaultMessage(value, parameter)) + ? replaceParameters(value, locale, visitedParameters) : null; + } + + private boolean isUsingCodeAsDefaultMessage(String value, String parameter) { + return value.equals(parameter); } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/validation/MessageSourceMessageInterpolatorTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/validation/MessageSourceMessageInterpolatorTests.java index 387db16348..5d149c072b 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/validation/MessageSourceMessageInterpolatorTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/validation/MessageSourceMessageInterpolatorTests.java @@ -34,6 +34,7 @@ import static org.mockito.Mockito.mock; * * @author Dmytro Nosan * @author Andy Wilkinson + * @author Scott Frederick */ class MessageSourceMessageInterpolatorTests { @@ -58,6 +59,14 @@ class MessageSourceMessageInterpolatorTests { .isEqualTo("{foo}{child}+{child}{bar}"); } + @Test + void interpolateWhenParametersAreUnknownUsingCodeAsDefaultShouldLeaveThemUnchanged() { + this.messageSource.setUseCodeAsDefaultMessage(true); + this.messageSource.addMessage("top", Locale.getDefault(), "{child}+{child}"); + assertThat(this.interpolator.interpolate("{foo}{top}{bar}", this.context)) + .isEqualTo("{foo}{child}+{child}{bar}"); + } + @Test void interpolateWhenParametersAreNestedShouldFullyReplaceAllParameters() { this.messageSource.addMessage("top", Locale.getDefault(), "{child}+{child}");