spring-framework icon indicating copy to clipboard operation
spring-framework copied to clipboard

Redundant registration of Date/Calendar/Long converters in DefaultFormattingConversionService

Open itaming opened this issue 5 months ago • 1 comments

While debugging WebConversionService initialization, I noticed that DateFormatterRegistrar.addDateConverters(...) appears to be invoked twice during the setup of the default formatting conversion service.

The call path is:

DefaultFormattingConversionService.addDefaultFormatters(FormatterRegistry formatterRegistry)
 ├─ new DateTimeFormatterRegistrar().registerFormatters(formatterRegistry)
 │   └─ DateTimeConverters.registerConverters(registry)
 │       └─ DateFormatterRegistrar.addDateConverters(registry)
 └─ new DateFormatterRegistrar().registerFormatters(formatterRegistry)
     └─ addDateConverters(registry)

As a result, the following converters are registered twice:

  • DateToLongConverter
  • DateToCalendarConverter
  • CalendarToDateConverter
  • CalendarToLongConverter
  • LongToDateConverter
  • LongToCalendarConverter

When inspecting the internal converter registry, I can see duplicate converter instances for the same source/target type pairs.

I understand that this does not appear to cause any functional issues, since conversion behavior remains unchanged. However, the duplicate registrations seem redundant and may introduce unnecessary converter instances in the registry.

I may be missing some historical context here, but I wanted to check whether this duplication is expected or if it could be simplified.

itaming avatar Jun 19 '26 11:06 itaming

I had a quick look at the registration path.

It looks like the duplicate registrations come from DateTimeFormatterRegistrar calling DateTimeConverters.registerConverters(...), which currently delegates to DateFormatterRegistrar.addDateConverters(...), and then DefaultFormattingConversionService.addDefaultFormatters(...) registers DateFormatterRegistrar as well.

I agree this does not appear to change conversion behavior, but it does create duplicate converter instances for the same source/target pairs.

If the team considers this worth simplifying, I can explore a small change that keeps standalone DateTimeFormatterRegistrar behavior intact while avoiding the duplicate registration in DefaultFormattingConversionService.

codingkiddo avatar Jun 20 '26 13:06 codingkiddo