Skip to content

Hibernate validation messages broken in spring boot 2.6.1 when setUseCodeAsDefaultMessage set to true #28930

Closed

Description

@runningonfumes

Configuring the following messageSource breaks hibernate validation messages

@Bean
public MessageSource messageSource() {
	var resourceBundleMessageSource = new ResourceBundleMessageSource();
	resourceBundleMessageSource.setUseCodeAsDefaultMessage(true);
	return resourceBundleMessageSource;
}

attached is a simple test case where the defaultMessage is the key, not the expected translation.

If that test is run against spring boot 2.5.7 it is fine, against 2.6.1 it is broken
demo.zip
.

Activity

  1. snicoll commented on Dec 8, 2021

    @snicoll
    Member

    Thanks for the report. Spring Boot 2.6 switched to Hibernate Validator 7 and downgrading it to the version used by Spring Boot 2.5.x didn't fix the problem so we'll have to investigate.

  2. wilkinsona commented on Dec 8, 2021

    @wilkinsona
    Member

    Thanks for the sample, @runningonfumes. The change in behaviour is an unanticipated side-effect of #17530. You can restore the previous behaviour by defining your own validator:

    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public static LocalValidatorFactoryBean customValidator() {
        LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
        MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory();
        factoryBean.setMessageInterpolator(interpolatorFactory.getObject());
        return factoryBean;
    }

    To make this easier, we could perhaps introduce a configuration property that allows you to opt out of using the MessageSource for validation message interpolation. Flagging for team attention so that we can consider our options.

  3. added
    type: regressionA regression from a previous release
    and removed
    for: team-attentionAn issue we'd like other members of the team to review
    status: waiting-for-triageAn issue that has not yet been triaged. Contributions will not be accepted.
    on Dec 15, 2021
  4. added this to the 2.6.x milestone on Dec 15, 2021
  5. added a commit that references this issue on Dec 16, 2021
    92b096a
  6. modified the milestones: 2.6.x, 2.6.2 on Dec 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

type: regressionA regression from a previous release

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions