Michael C. Maggio opened SPR-7934 and commented

The FormattingConversionService's handling of empty strings appears to be different depending on whether or not a field has a format annotation. For example, consider the following class.

public class MyForm
{
    private String unformatted;
    private String formatted;

    public String getUnformatted() { return unformatted; }
    public void setUnformatted(String v) { unformatted = v; }

    @MyFormat
    public String getFormatted() { return formatted; }
    public void setFormatted(String v) { formatted = v; }
}

If I POST a form with formatted="" and unformatted="", then I will receive an instance of MyForm with formatted=null and unformatted="".

The problem seems to be lines 231-233 of FormattingConversionService which executes the following immediately before invoking the parser:

if (text == null || text.length() == 0) {
    return null;
}

This might seem trivial, but it has caused us some confusion with the JSR303 bean validators when testing for blank fields. The unformatted fields will work fine with @NotBlank, but the formatted fields need BOTH @NotBlank and @NotNull to handle both empty strings and whitespace strings.

As a solution, I would recommend removing this check - at the very least the length check.


Affects: 3.0.5

Comment From: spring-projects-issues

Juergen Hoeller commented

Closing groups of outdated issues. Please reopen if still relevant.

Comment From: sbrannen

  • duplicate of #12969