Skip to content

Commit 5ccccf1

Browse files
committed
Merge branch '3.3.x' into 3.4.x
Closes gh-45213
2 parents 5ee5678 + b218e9d commit 5ccccf1

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

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
}

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

+14-1
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.
@@ -66,6 +66,19 @@ void interpolateWhenParametersAreUnknownUsingCodeAsDefaultShouldLeaveThemUnchang
6666
.isEqualTo("{foo}{child}+{child}{bar}");
6767
}
6868

69+
@Test
70+
void interpolateShouldReplaceParameterThatReferencesAMessageThatMatchesItsCode() {
71+
this.messageSource.addMessage("foo", Locale.getDefault(), "foo");
72+
assertThat(this.interpolator.interpolate("{foo}", this.context)).isEqualTo("foo");
73+
}
74+
75+
@Test
76+
void interpolateUsingCodeAsDefaultShouldReplaceParameterThatReferencesAMessageThatMatchesItsCode() {
77+
this.messageSource.setUseCodeAsDefaultMessage(true);
78+
this.messageSource.addMessage("foo", Locale.getDefault(), "foo");
79+
assertThat(this.interpolator.interpolate("{foo}", this.context)).isEqualTo("foo");
80+
}
81+
6982
@Test
7083
void interpolateWhenParametersAreNestedShouldFullyReplaceAllParameters() {
7184
this.messageSource.addMessage("top", Locale.getDefault(), "{child}+{child}");

0 commit comments

Comments
 (0)