Skip to content

Commit

Permalink
fix for #38730 - HTTP-header accept-language ignoring case
Browse files Browse the repository at this point in the history
(cherry picked from commit 405c1a2)
  • Loading branch information
Christian Thiel authored and gsmet committed Feb 13, 2024
1 parent ce76213 commit efca3e9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
import org.hibernate.validator.spi.messageinterpolation.LocaleResolver;
import org.hibernate.validator.spi.messageinterpolation.LocaleResolverContext;
import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.common.util.CaseInsensitiveMap;

abstract class AbstractLocaleResolver implements LocaleResolver {

private static final Logger log = Logger.getLogger(AbstractLocaleResolver.class);
private static final String ACCEPT_HEADER = "Accept-Language";

/**
* @return case-insensitive map
* @see CaseInsensitiveMap
*/
protected abstract Map<String, List<String>> getHeaders();

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package io.quarkus.hibernate.validator.runtime.locale;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jakarta.inject.Inject;
import jakarta.inject.Singleton;

import org.jboss.resteasy.reactive.common.util.CaseInsensitiveMap;

import io.quarkus.arc.Arc;
import io.quarkus.arc.ManagedContext;
import io.quarkus.vertx.http.runtime.CurrentVertxRequest;
Expand All @@ -31,7 +32,7 @@ protected Map<String, List<String>> getHeaders() {
}
RoutingContext current = currentVertxRequest.getCurrent();
if (current != null) {
Map<String, List<String>> result = new HashMap<>();
Map<String, List<String>> result = new CaseInsensitiveMap();
MultiMap headers = current.request().headers();
for (String name : headers.names()) {
result.put(name, headers.getAll(name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public class TextResource {
@GET
@Path("/validate/{id}")
public String validate(
@Digits(integer = 5, fraction = 0, message = "numeric value out of bounds") @PathParam("id") String id) {
@Digits(integer = 5, fraction = 0) @PathParam("id") String id) {
return id;
}

@GET
@Path("/validate/text/{id}")
@Produces(MediaType.TEXT_PLAIN)
public String validateText(
@Digits(integer = 5, fraction = 0, message = "numeric value out of bounds") @PathParam("id") String id) {
@Digits(integer = 5, fraction = 0) @PathParam("id") String id) {
return id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkus.locales=en,de
quarkus.default-locale=en
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.quarkus.it.hibernate.validator;

import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.containsString;

import org.junit.jupiter.api.Test;

Expand All @@ -25,4 +27,22 @@ public void fetchText() {
.statusCode(400)
.contentType(ContentType.TEXT);
}

@Test
public void shouldGetAcceptLanguageLocaleIfKeyIsUpperCase() {
given()
.header("Accept-Language", "de")
.when().get("/text/validate/boom")
.then().log().ifValidationFails()
.body(containsString("numerischer Wert außerhalb des gültigen Bereichs"));
}

@Test
public void shouldGetAcceptLanguageLocaleIfKeyIsLowerCase() {
given()
.header("accept-language", "de")
.when().get("/text/validate/boom")
.then().log().ifValidationFails()
.body(containsString("numerischer Wert außerhalb des gültigen Bereichs"));
}
}

0 comments on commit efca3e9

Please sign in to comment.