jakubdyszkiewicz opened SPR-17231 and commented

Hello,

I've got a problem with providing custom messages in different languages for bean validation using WebFlux. It does not use messages for locale I send in header.

I think this is a bug because LocalValidatorFactoryBean uses LocaleContextMessageInterpolator. Hibernate Validator calls the public String interpolate(String message, Context context) method, which relays on the LocaleContextHolder. It uses a ThreadLocal which can't be used in WebFlux.

I created sample repository, I hope you find it helpful https://github.com/jakubdyszkiewicz/webflux-validation

There is the issue I created by mistake in spring-boot project https://github.com/spring-projects/spring-boot/issues/14253

Dmytro Nosan provided workaround, but I think it should work out of the box.

Thanks


Referenced from: commits https://github.com/spring-projects/spring-framework-issues/commit/d059b1a450492f003700c24e88fdeb9ea3236737

Comment From: spring-projects-issues

Juergen Hoeller commented

We could potentially (re-)expose a short-lived thread-local for affected callbacks in WebFlux. This would be the easiest way to make existing locale resolution work... Otherwise we'd have to support an explicit Locale argument in every such place.

Comment From: spring-projects-issues

Rossen Stoyanchev commented

jakubdyszkiewicz thanks for the sample project, and sorry for the delay!

Juergen Hoeller, we could set LocaleContextHolder in a try-finally block within the validate methods of AbstractMessageReaderArgumentResolver and ModelAttributeMethodArgumentResolver, the two places in WebFlux where such validation is applied. We could even if check with LocaleContext#getLocale() to see if there is a need. That said, it would be more in character for WebFlux to pass the locale. One way or another I see no way around the need to be aware of having to pass the Locale.

Couldn't we pass the LocaleContext as one of the validation hints? Then when processing constraint validations, SpringValidator or rather the LocalValidatorFactoryBean, would have a chance to look up through the MessageInterpolator with the LocaleContext provided as a hint. Or some such approach where the error messages are post-processed and mapped to their Locale-specific values.

Comment From: slonka

Any updates on this?

Comment From: mdsina

Someone have custom implementation for this?

Comment From: hicnar

Any news here?

Comment From: rstoyanchev

This is now superseded by the attached pull request #28739.