Affects: 6.0.4

I try to use error handling by setting problemdetails.enabled=true and internationalising error messages. But I can't achieve my expected result Because in org.springframework.web.bind.MethodArgumentNotValidException::errorsToStringList field and single quotations were added to messages.

private static List<String> errorsToStringList(
List<? extends ObjectError> errors, Function<ObjectError, String> formatter) {

        List<String> result = new ArrayList<>(errors.size());
        for (ObjectError error : errors) {
            String value = formatter.apply(error);
            if (StringUtils.hasText(value)) {
                result.add(error instanceof FieldError fieldError ?
                        fieldError.getField() + ": '" + value + "'" : "'" + value + "'");
            }
        }
        return result;
    }

Is there any way to internationalize the field name or remove the field name and brackets from messages?

messages.properties

problemDetail.org.springframework.web.bind.support.WebExchangeBindException= Format is incorrect because {1}
Pattern.phoneNumber= phone number is not correct
Pattern.email= email is not correct

Here is sample output:

{
  "type": "about:blank",
  "title": "problemDetail.title.org.springframework.web.bind.support.WebExchangeBindException",
  "status": 400,
  "detail": "Format is incorrect because [phoneNumber: 'phone number is not correct',email: 'email is not correct']",
  "instance": "/profile"
}

But I expect:

{
  "type": "about:blank",
  "title": "problemDetail.title.org.springframework.web.bind.support.WebExchangeBindException",
  "status": 400,
  "detail": "Format is incorrect because phone number is not correct and email is not correct",
  "instance": "/profile"
}

Comment From: rstoyanchev

Indeed, we should use the resolved message as is without the field name since the field name is itself not resolved, while the message codes allow being specific about the field name.

Comment From: rstoyanchev

@9450035 thanks for raising this issue. I've made the requested changes in 96c494c6ad90719346a5b09db91a82e26f0fcb28.

  1. For the errorsToStringList variant without a MessageSource, each FieldError is still formatted with the field name prepended since the default validation error message from Bean Validation doesn't have it, but I've removed single quotes and list brackets, and used ", and " to join the errors.
  2. For the errorsToStringList variant with a MessageSource, we simply resolve FieldError through the MessageSource and add nothing else.

Note that 2) can result in validation error messages without field names, if the messages for the individual error codes are not customized, e.g. the Pattern.phoneNumber and Pattern.email from above, but since the application has full control over that, we assume those codes are present, or that otherwise they will be added.

I've scheduled a documentation update to go with this in #30653.