Skip to content

Commit feeeab1

Browse files
committed
Reorder date formatting converter in registrar
Prior to this commit, the `DateFormatterRegistrar` would register the annotation-based formatter before the pattern-based formatter. This would create an issue when an application tries to convert a `String` to an annotated `@DateTimeFormat Date`: since the converters are considered in reversed order of registration in `GenericConversionServicei#ConvertersForPair`, the pattern-based variant would always be considered before the annotation-based variant, overriding the developer's opinion. This commit aligns the `DateFormatterRegistrar` with the `DateTimeFormatterRegistrar` and registers the annotation-based variant last. Closes gh-23896
1 parent b4cf471 commit feeeab1

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

spring-context/src/main/java/org/springframework/format/datetime/DateFormatterRegistrar.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,13 @@ public void setFormatter(DateFormatter dateFormatter) {
6161
@Override
6262
public void registerFormatters(FormatterRegistry registry) {
6363
addDateConverters(registry);
64-
registry.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory());
65-
6664
// In order to retain back compatibility we only register Date/Calendar
6765
// types when a user defined formatter is specified (see SPR-10105)
6866
if (this.dateFormatter != null) {
6967
registry.addFormatter(this.dateFormatter);
7068
registry.addFormatterForFieldType(Calendar.class, this.dateFormatter);
7169
}
70+
registry.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory());
7271
}
7372

7473
/**

spring-context/src/test/java/org/springframework/format/datetime/DateFormattingTests.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -46,7 +46,7 @@
4646
*/
4747
public class DateFormattingTests {
4848

49-
private final FormattingConversionService conversionService = new FormattingConversionService();
49+
private FormattingConversionService conversionService;
5050

5151
private DataBinder binder;
5252

@@ -58,6 +58,7 @@ public void setup() {
5858
}
5959

6060
private void setup(DateFormatterRegistrar registrar) {
61+
conversionService = new FormattingConversionService();
6162
DefaultConversionService.addDefaultConverters(conversionService);
6263
registrar.registerFormatters(conversionService);
6364

@@ -148,6 +149,20 @@ public void testBindDateAnnotatedPattern() {
148149
assertEquals("10/31/09 1:05", binder.getBindingResult().getFieldValue("dateAnnotatedPattern"));
149150
}
150151

152+
@Test
153+
public void testBindDateAnnotatedPatternWithGlobalFormat() {
154+
DateFormatterRegistrar registrar = new DateFormatterRegistrar();
155+
DateFormatter dateFormatter = new DateFormatter();
156+
dateFormatter.setIso(ISO.DATE_TIME);
157+
registrar.setFormatter(dateFormatter);
158+
setup(registrar);
159+
MutablePropertyValues propertyValues = new MutablePropertyValues();
160+
propertyValues.add("dateAnnotatedPattern", "10/31/09 1:05");
161+
binder.bind(propertyValues);
162+
assertEquals(0, binder.getBindingResult().getErrorCount());
163+
assertEquals("10/31/09 1:05", binder.getBindingResult().getFieldValue("dateAnnotatedPattern"));
164+
}
165+
151166
@Test
152167
public void testBindDateTimeOverflow() {
153168
MutablePropertyValues propertyValues = new MutablePropertyValues();

0 commit comments

Comments
 (0)