Skip to content

Commit 83901a2

Browse files
nosanwilkinsona
authored andcommitted
Fix parameter replacement when message matches its code
This commit addresses an issue where placeholders like {foo} remain as {foo} if the message is the same as its code. See gh-45212 Signed-off-by: Dmytro Nosan <[email protected]>
1 parent 7f02013 commit 83901a2

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

Diff for: spring-boot-project/spring-boot/src/main/java/org/springframework/boot/validation/MessageSourceMessageInterpolator.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,8 @@
3434
*/
3535
class MessageSourceMessageInterpolator implements MessageInterpolator {
3636

37+
private static final String DEFAULT_MESSAGE = MessageSourceMessageInterpolator.class.getName();
38+
3739
private static final char PREFIX = '{';
3840

3941
private static final char SUFFIX = '}';
@@ -115,13 +117,11 @@ else if (buf.charAt(i) == SUFFIX) {
115117

116118
private String replaceParameter(String parameter, Locale locale, Set<String> visitedParameters) {
117119
parameter = replaceParameters(parameter, locale, visitedParameters);
118-
String value = this.messageSource.getMessage(parameter, null, null, locale);
119-
return (value != null && !isUsingCodeAsDefaultMessage(value, parameter))
120-
? replaceParameters(value, locale, visitedParameters) : null;
121-
}
122-
123-
private boolean isUsingCodeAsDefaultMessage(String value, String parameter) {
124-
return value.equals(parameter);
120+
String value = this.messageSource.getMessage(parameter, null, DEFAULT_MESSAGE, locale);
121+
if (value == null || value.equals(DEFAULT_MESSAGE)) {
122+
return null;
123+
}
124+
return replaceParameters(value, locale, visitedParameters);
125125
}
126126

127127
}

Diff for: spring-boot-project/spring-boot/src/test/java/org/springframework/boot/validation/MessageSourceMessageInterpolatorTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -62,8 +62,8 @@ void interpolateWhenParametersAreUnknownShouldLeaveThemUnchanged() {
6262
void interpolateWhenParametersAreUnknownUsingCodeAsDefaultShouldLeaveThemUnchanged() {
6363
this.messageSource.setUseCodeAsDefaultMessage(true);
6464
this.messageSource.addMessage("top", Locale.getDefault(), "{child}+{child}");
65-
assertThat(this.interpolator.interpolate("{foo}{top}{bar}", this.context))
66-
.isEqualTo("{foo}{child}+{child}{bar}");
65+
this.messageSource.addMessage("foo", Locale.getDefault(), "foo");
66+
assertThat(this.interpolator.interpolate("{foo}{top}{bar}", this.context)).isEqualTo("foo{child}+{child}{bar}");
6767
}
6868

6969
@Test

0 commit comments

Comments
 (0)