Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MethodArgumentNotValidException::errorsToStringList should include only the resolved message when MessageSource is used #30198

Closed
9450035 opened this issue Mar 26, 2023 · 2 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@9450035
Copy link

9450035 commented Mar 26, 2023

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"
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 26, 2023
@rstoyanchev rstoyanchev added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Mar 28, 2023
@rstoyanchev
Copy link
Contributor

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.

@rstoyanchev rstoyanchev self-assigned this Mar 28, 2023
@rstoyanchev rstoyanchev added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Mar 28, 2023
@rstoyanchev rstoyanchev added this to the 6.1.0-M1 milestone Mar 28, 2023
@rstoyanchev rstoyanchev changed the title MethodArgumentNotValidException::errorsToStringList problem MethodArgumentNotValidException::errorsToStringList should include only the resolved message Mar 28, 2023
@rstoyanchev rstoyanchev changed the title MethodArgumentNotValidException::errorsToStringList should include only the resolved message MethodArgumentNotValidException::errorsToStringList should include only the resolved message when MessageSource is used Jun 13, 2023
@rstoyanchev
Copy link
Contributor

rstoyanchev commented Jun 13, 2023

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

  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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants