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

Quarkus hibernate validator only supports a single locale #5882

Closed
sean-scott-lr opened this issue Nov 29, 2019 · 1 comment
Closed

Quarkus hibernate validator only supports a single locale #5882

sean-scott-lr opened this issue Nov 29, 2019 · 1 comment
Labels
area/hibernate-validator Hibernate Validator kind/bug Something isn't working
Milestone

Comments

@sean-scott-lr
Copy link

Describe the bug
I tried to write a validation exception mapper that returns validation errors in the language requested and I receive this error message:

HV000250: Uninitialized locale: es. Please register your locale as a locale to initialize when initializing your ValidatorFactory.

Expected behavior
I expected the validation error to be in spanish

Actual behavior
An error occured

To Reproduce
Steps to reproduce the behavior:
Create a class with validation annotations
Create a validation exception mapper like this:


@Provider
@ApplicationScoped
public class ValidationErrorMapper extends BaseExceptionMapper
    implements ExceptionMapper<ConstraintViolationException> {

  @javax.ws.rs.core.Context
  private HttpHeaders requestHeaders;

  private MessageInterpolator interpolator;


  @Inject
  public ValidationErrorMapper(ValidatorFactory validatorFactory){
    this.interpolator = validatorFactory.getMessageInterpolator();
  }

  public Response toResponse( ConstraintViolationException e ){
    return Response.status(Status.BAD_REQUEST)
        .type(MediaType.APPLICATION_JSON)
        .entity(
            ResponseV1.error(Status.BAD_REQUEST.getStatusCode(),
                getMessage(e.getConstraintViolations().iterator().next(),requestHeaders.getAcceptableLanguages().get(0))))
        .build();
  }

  protected String getMessage(ConstraintViolation v, Locale locale) {

    if( locale == null ){
      locale = Locale.getDefault();
    }
    return interpolator.interpolate(v.getMessageTemplate(),createContext(v), locale);
  }

  protected Context createContext(ConstraintViolation violation){
    return new ContextImpl(violation);
  }

Send a request to the endpoint that fails validation rules and specify the accept language
Accept-Language es

Configuration

# Add your application.properties here, if applicable.

Screenshots
(If applicable, add screenshots to help explain your problem.)

Environment (please complete the following information):

  • Quarkus version 1.0.0.Final

Additional context
(Add any other context about the problem here.)

package io.quarkus.hibernate.validator.runtime;

...

@Recorder
public class HibernateValidatorRecorder {

...


                Set<Locale> localesToInitialize = Collections.singleton(Locale.getDefault());

...

                configuration
                        .initializeBeanMetaData(classesToBeValidated)
                        .initializeLocales(localesToInitialize)
                        .beanMetaDataClassNormalizer(new ArcProxyBeanMetaDataClassNormalizer());
@gsmet
Copy link
Member

gsmet commented Aug 13, 2020

This has been fixed for a long time now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/hibernate-validator Hibernate Validator kind/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants