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

Make accept header check in validation case insensitive #38738

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
}
}
Loading