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

Ensure Locale context is available for WebFlux method validation #33810

Closed
stefan42 opened this issue Oct 29, 2024 · 2 comments
Closed

Ensure Locale context is available for WebFlux method validation #33810

stefan42 opened this issue Oct 29, 2024 · 2 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@stefan42
Copy link

Hi,

I’ve stumbled over some strange behavior regarding internationalization of validation error messages in a spring-boot application using webflux. But I think the error is related to spring-framework and webflux.

When I’m validating fields in the request body, the language of occurring validation error messages can be changed via the Accept-Language HTTP header. This is done automatically because the AcceptHeaderLocaleContextResolver is used during the validation process.

Although when I’m validating a URL query parameter or a path variable (e.g. via the @Size validation annotation), the AcceptHeaderLocaleContextResolver is not used during the validation and the validation error message is always generated in the language of the JVM locale.

Is this the correct behavior?

I’v attached a small demo project.

demo.zip

In the DemoApplicationTests class there are the testBodyValidationErrorForLocaleEN and testBodyValidationErrorForLocaleDE methods which are passing without errors. The testPathVariableValidationErrorForLocaleEN
and testPathVariableValidationErrorForLocaleDE methods are not passing (or at least one of them regarding your JVM locale)

Tested Version:
Spring-boot: 3.3.5
(Which is using Spring-framework: 6.1.14)

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 29, 2024
@jhoeller jhoeller added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Oct 29, 2024
@quaff
Copy link
Contributor

quaff commented Oct 30, 2024

I confirm the inconsistent behavior, webmvc is not affected.

@rstoyanchev
Copy link
Contributor

Internationalized messages in Jakarta Validation depend on a MessageInterpolator, and the one set up in LocalValidatorFactoryBean depends on a ThreadLocal set up by LocaleContextHolder. On Spring MVC this works throughout request handling, but on WebFlux we need to establish the ThreadLocal context where needed. We did that for arguments in #21764, but it's not done yet for method validation.

@rstoyanchev rstoyanchev self-assigned this Feb 4, 2025
@rstoyanchev rstoyanchev added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Feb 4, 2025
@rstoyanchev rstoyanchev added this to the 6.2.3 milestone Feb 4, 2025
@rstoyanchev rstoyanchev changed the title WebFlux: AcceptHeaderLocaleContextResolver is not used during method parameter validation Ensure Locale context is available for WebFlux method validation Feb 4, 2025
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: bug A general bug
Projects
None yet
Development

No branches or pull requests

5 participants