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 07363390b1e8..8287262b10e1 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,8 @@ */ class MessageSourceMessageInterpolator implements MessageInterpolator { + private static final String DEFAULT_MESSAGE = MessageSourceMessageInterpolator.class.getName(); + private static final char PREFIX = '{'; private static final char SUFFIX = '}'; @@ -115,13 +117,11 @@ else if (buf.charAt(i) == SUFFIX) { 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 && !isUsingCodeAsDefaultMessage(value, parameter)) - ? replaceParameters(value, locale, visitedParameters) : null; - } - - private boolean isUsingCodeAsDefaultMessage(String value, String parameter) { - return value.equals(parameter); + String value = this.messageSource.getMessage(parameter, null, DEFAULT_MESSAGE, locale); + if (value == null || value.equals(DEFAULT_MESSAGE)) { + return null; + } + return replaceParameters(value, locale, visitedParameters); } } 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 66653ef9ad74..2ce034560435 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -62,8 +62,8 @@ void interpolateWhenParametersAreUnknownShouldLeaveThemUnchanged() { 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}"); + this.messageSource.addMessage("foo", Locale.getDefault(), "foo"); + assertThat(this.interpolator.interpolate("{foo}{top}{bar}", this.context)).isEqualTo("foo{child}+{child}{bar}"); } @Test