spring-projects / spring-projects/spring-framework

@Pattern error message "{0}" is not resolved. [SPR-17217]

Open
#21,750 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: web type: bug
Dominant language
Java
Stars
60.2k
Forks
38.8k
Avg merge
5d 2h
Merged PRs (30d)
27

Description

yoshikawaa opened SPR-17217 and commented

Application

Spring Boot Web MVC Application
Spring Boot 2.0.4.RELEASE
Sample : https://github.com/yoshikawaa/spring-boot-pattern-demo

Problem

There are cases where @Pattern error message {0} is not resolved.

  • ValidationMessages.properties
javax.validation.constraints.Pattern.message = {0} must match "{regexp}".
  • application.properties
spring.messages.basename = ValidationMessages
  • Form
public class DemoForm {
    @Pattern(regexp = "\\d{3}")    // -> message [ valid must match "\d{3}".  ]
    private String valid;
    @Pattern(regexp = "\\d{1,3}") // -> message [ {0} must match "\d{1,3}".  ]
    private String invalid;
    // omit getter and setter
}

My validation is that if a range of digits is used in a regular expression, the mechanism for resolving {0} does not work properly.

Controller
    @PostMapping
    public String post(@Valid DemoForm form, BindingResult result) {
        result.getFieldErrors().forEach(e -> logger.info("field:{},error:{}", e.getField(), e.getDefaultMessage()));
        // log [ field:valid,error:{0} must match "\d{3}". ]
        // log [ field:invalid,error:{0} must match "\d{1,3}". ]
        return "demo";
    }

When logging BindingResult with Controller, you can see that LocalValidatorFactoryBean (Hibernate Validator) resolves message variable {regexp} beforehand.
After that, when Spring MVC resolve the message, it is considered that the regular expression in the message is obstructing resolution of {0}.


Affects: 5.0.8

Reference URL: https://github.com/spring-projects/spring-boot/issues/14163

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with ValidationMessages.properties, application.properties, DemoForm, and the Controller example, then trace how LocalValidatorFactoryBean and Spring MVC resolve the BindingResult field error. Compare the messages with and without the ranged regular expression, and consider the issue done when the {0} variable resolves consistently without breaking {regexp} substitution.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.