Skip to content

Commit 8f972a5

Browse files
committed
Merge branch '5.1.x'
2 parents 3c3d828 + 80c6f2b commit 8f972a5

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

spring-context-support/src/test/java/org/springframework/validation/beanvalidation2/SpringValidatorAdapterTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public class SpringValidatorAdapterTests {
7474

7575
@Before
7676
public void setupSpringValidatorAdapter() {
77-
messageSource.addMessage("Size", Locale.ENGLISH, "Size of {0} is must be between {2} and {1}");
77+
messageSource.addMessage("Size", Locale.ENGLISH, "Size of {0} must be between {2} and {1}");
7878
messageSource.addMessage("Same", Locale.ENGLISH, "{2} must be same value as {1}");
7979
messageSource.addMessage("password", Locale.ENGLISH, "Password");
8080
messageSource.addMessage("confirmPassword", Locale.ENGLISH, "Password(Confirm)");
@@ -100,7 +100,7 @@ public void testNoStringArgumentValue() {
100100
assertThat(errors.getFieldValue("password")).isEqualTo("pass");
101101
FieldError error = errors.getFieldError("password");
102102
assertThat(error).isNotNull();
103-
assertThat(messageSource.getMessage(error, Locale.ENGLISH)).isEqualTo("Size of Password is must be between 8 and 128");
103+
assertThat(messageSource.getMessage(error, Locale.ENGLISH)).isEqualTo("Size of Password must be between 8 and 128");
104104
assertThat(error.contains(ConstraintViolation.class)).isTrue();
105105
assertThat(error.unwrap(ConstraintViolation.class).getPropertyPath().toString()).isEqualTo("password");
106106
}

spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ protected void processConstraintViolations(Set<ConstraintViolation<Object>> viol
169169
errors.getObjectName(), errorCodes, errorArgs, violation.getMessage()) {
170170
@Override
171171
public boolean shouldRenderDefaultMessage() {
172-
return false;
172+
return requiresMessageFormat(violation);
173173
}
174174
};
175175
error.wrap(violation);
@@ -182,7 +182,7 @@ public boolean shouldRenderDefaultMessage() {
182182
rejectedValue, false, errorCodes, errorArgs, violation.getMessage()) {
183183
@Override
184184
public boolean shouldRenderDefaultMessage() {
185-
return false;
185+
return requiresMessageFormat(violation);
186186
}
187187
};
188188
error.wrap(violation);
@@ -300,12 +300,36 @@ protected Object[] getArgumentsForConstraint(String objectName, String field, Co
300300
* @param field the field that caused the binding error
301301
* @return a corresponding {@code MessageSourceResolvable} for the specified field
302302
* @since 4.3
303+
* @see #getArgumentsForConstraint
303304
*/
304305
protected MessageSourceResolvable getResolvableField(String objectName, String field) {
305306
String[] codes = new String[] {objectName + Errors.NESTED_PATH_SEPARATOR + field, field};
306307
return new DefaultMessageSourceResolvable(codes, field);
307308
}
308309

310+
/**
311+
* Indicate whether this violation's interpolated message has remaining
312+
* placeholders and therefore requires {@link java.text.MessageFormat}
313+
* to be applied to it. Called for a Bean Validation defined message
314+
* (coming out {@code ValidationMessages.properties}) when rendered
315+
* as the default message in Spring's MessageSource.
316+
* <p>The default implementation considers a Spring-style "{0}" placeholder
317+
* for the field name as an indication for {@link java.text.MessageFormat}.
318+
* Any other placeholder or escape syntax occurrences are typically a
319+
* mismatch, coming out of regex pattern values or the like. Note that
320+
* standard Bean Validation does not support "{0}" style placeholders at all;
321+
* this is a feature typically used in Spring MessageSource resource bundles.
322+
* @param violation the Bean Validation constraint violation, including
323+
* BV-defined interpolation of named attribute references in its message
324+
* @return {@code true} if {@code java.text.MessageFormat} is to be applied,
325+
* or {@code false} if the violation's message should be used as-is
326+
* @since 5.1.8
327+
* @see #getArgumentsForConstraint
328+
*/
329+
protected boolean requiresMessageFormat(ConstraintViolation<?> violation) {
330+
return violation.getMessage().contains("{0}");
331+
}
332+
309333
/**
310334
* Extract the rejected value behind the given constraint violation,
311335
* for exposure through the Spring errors representation.

spring-context/src/test/java/org/springframework/validation/beanvalidation/SpringValidatorAdapterTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public class SpringValidatorAdapterTests {
7171

7272
@Before
7373
public void setupSpringValidatorAdapter() {
74-
messageSource.addMessage("Size", Locale.ENGLISH, "Size of {0} is must be between {2} and {1}");
74+
messageSource.addMessage("Size", Locale.ENGLISH, "Size of {0} must be between {2} and {1}");
7575
messageSource.addMessage("Same", Locale.ENGLISH, "{2} must be same value as {1}");
7676
messageSource.addMessage("password", Locale.ENGLISH, "Password");
7777
messageSource.addMessage("confirmPassword", Locale.ENGLISH, "Password(Confirm)");
@@ -97,7 +97,7 @@ public void testNoStringArgumentValue() {
9797
assertThat(errors.getFieldValue("password")).isEqualTo("pass");
9898
FieldError error = errors.getFieldError("password");
9999
assertThat(error).isNotNull();
100-
assertThat(messageSource.getMessage(error, Locale.ENGLISH)).isEqualTo("Size of Password is must be between 8 and 128");
100+
assertThat(messageSource.getMessage(error, Locale.ENGLISH)).isEqualTo("Size of Password must be between 8 and 128");
101101
assertThat(error.contains(ConstraintViolation.class)).isTrue();
102102
assertThat(error.unwrap(ConstraintViolation.class).getPropertyPath().toString()).isEqualTo("password");
103103
}

0 commit comments

Comments
 (0)