Skip to content

Commit

Permalink
Fix message interpolation when code is used as default message
Browse files Browse the repository at this point in the history
When `setUseCodeAsDefaultMessage(true)` was set on a message source,
attempting to interpolate the default message returned from the message
source would result in the code being unusable by upstream message
resolvers.

Fixes gh-28930
  • Loading branch information
scottfrederick committed Dec 16, 2021
1 parent 6555ad4 commit 92b096a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Expand Up @@ -30,6 +30,7 @@
* message using the underlying {@link MessageInterpolator}.
*
* @author Dmytro Nosan
* @author Scott Frederick
*/
class MessageSourceMessageInterpolator implements MessageInterpolator {

Expand Down Expand Up @@ -115,7 +116,12 @@ else if (buf.charAt(i) == SUFFIX) {
private String replaceParameter(String parameter, Locale locale, Set<String> 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);
}

}
Expand Up @@ -34,6 +34,7 @@
*
* @author Dmytro Nosan
* @author Andy Wilkinson
* @author Scott Frederick
*/
class MessageSourceMessageInterpolatorTests {

Expand All @@ -58,6 +59,14 @@ void interpolateWhenParametersAreUnknownShouldLeaveThemUnchanged() {
.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}");
Expand Down

0 comments on commit 92b096a

Please sign in to comment.