Affects: \6.0.4

The initial situation:

In a Spring Boot application, a custom formatter is annotated to a DTO object field:

public class RegistrationDto {

    @Trim
    private String username;

    //...

The marker interface @Trim basically does nothing:

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Trim {}

The formatter itself looks like this:

public class TrimFormatter implements Formatter<String> {

    @Override
    public String parse(String text, Locale locale) {
        return text.trim();
    }

    @Override
    public String print(String text, Locale locale) {
        return text;
    }
}

Building the AnnotationFormatterFactory looks like this:

public class TrimAnnotationFormatterFactory implements AnnotationFormatterFactory<Trim> {

    @Override
    public Set<Class<?>> getFieldTypes() {
        return Set.of(String.class);
    }

    @Override
    public Printer<?> getPrinter(Trim annotation, Class<?> fieldType) {
        return new TrimFormatter();
    }

    @Override
    public Parser<?> getParser(Trim annotation, Class<?> fieldType) {
        return new TrimFormatter();
    }
}

And finally the formatter is being registered:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addFormatterForFieldAnnotation(new TrimAnnotationFormatterFactory());
    }
}

The expected behavior:

If the username is passed an empty String or if the String consists of several empty characters, then the formatting process should return an empty String.

The issue:

If the DTO is built with an empty usernamename field, then the formatter is ignored altogether and the empty String is replaced with null.

Comment From: edyda99

I'm not sure if this is helpful but I tried to reproduce it locally using version 6.0.5 all was good, so maybe upgrading ur version will do you good. And I even tried to reproduce it inside \spring-context\src\test\java\org\springframework\format\number\NumberFormattingTests.java version 6.0.7-SNAPSHOT by adding your annotation to the convert using conversionService.addFormatterForFieldAnnotation(new TrimAnnotationFormatterFactory());, and it also went well.

Comment From: rstoyanchev

Thanks @edyda99 for the investigation.

@SlevinKT can you indeed give the latest 6.0.6 a try, and if you can still reproduce it, please provide a sample we can use to see the behavior.

Comment From: spring-projects-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: spring-projects-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

Comment From: ycaceresil

I have the same problem, the code in org.springframework.format.support.FormattingConversionService.ParserConverter::convert returns null when the text is null or empty:

            if (!StringUtils.hasText(text)) {
                return null;
            }

Comment From: bclozel

@ycaceresil you're not having the same issue, but rather an instance of https://github.com/spring-projects/spring-framework/issues/12969