Fix message interpolation when code is used as default message
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
This commit is contained in:
parent
6555ad404e
commit
92b096abbf
|
@ -30,6 +30,7 @@ import org.springframework.context.i18n.LocaleContextHolder;
|
||||||
* message using the underlying {@link MessageInterpolator}.
|
* message using the underlying {@link MessageInterpolator}.
|
||||||
*
|
*
|
||||||
* @author Dmytro Nosan
|
* @author Dmytro Nosan
|
||||||
|
* @author Scott Frederick
|
||||||
*/
|
*/
|
||||||
class MessageSourceMessageInterpolator implements MessageInterpolator {
|
class MessageSourceMessageInterpolator implements MessageInterpolator {
|
||||||
|
|
||||||
|
@ -115,7 +116,12 @@ class MessageSourceMessageInterpolator implements MessageInterpolator {
|
||||||
private String replaceParameter(String parameter, Locale locale, Set<String> visitedParameters) {
|
private String replaceParameter(String parameter, Locale locale, Set<String> visitedParameters) {
|
||||||
parameter = replaceParameters(parameter, locale, visitedParameters);
|
parameter = replaceParameters(parameter, locale, visitedParameters);
|
||||||
String value = this.messageSource.getMessage(parameter, null, null, locale);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,7 @@ import static org.mockito.Mockito.mock;
|
||||||
*
|
*
|
||||||
* @author Dmytro Nosan
|
* @author Dmytro Nosan
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
|
* @author Scott Frederick
|
||||||
*/
|
*/
|
||||||
class MessageSourceMessageInterpolatorTests {
|
class MessageSourceMessageInterpolatorTests {
|
||||||
|
|
||||||
|
@ -58,6 +59,14 @@ class MessageSourceMessageInterpolatorTests {
|
||||||
.isEqualTo("{foo}{child}+{child}{bar}");
|
.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
|
@Test
|
||||||
void interpolateWhenParametersAreNestedShouldFullyReplaceAllParameters() {
|
void interpolateWhenParametersAreNestedShouldFullyReplaceAllParameters() {
|
||||||
this.messageSource.addMessage("top", Locale.getDefault(), "{child}+{child}");
|
this.messageSource.addMessage("top", Locale.getDefault(), "{child}+{child}");
|
||||||
|
|
Loading…
Reference in New Issue