From 18a6af31f4417fa8b32459031a06cc3544276904 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Fri, 25 Mar 2022 08:43:35 +0200 Subject: [PATCH] Properly handle char and Character parameter types in JAX-RS methods Fixes: #24541 --- .../processor/ServerEndpointIndexer.java | 6 +++++ .../converters/CharParamConverter.java | 26 +++++++++++++++++++ .../converters/CharacterParamConverter.java | 26 +++++++++++++++++++ .../simple/SimpleQuarkusRestResource.java | 4 ++- .../simple/SimpleQuarkusRestTestCase.java | 4 ++- 5 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/parameters/converters/CharParamConverter.java create mode 100644 independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/parameters/converters/CharacterParamConverter.java diff --git a/independent-projects/resteasy-reactive/server/processor/src/main/java/org/jboss/resteasy/reactive/server/processor/ServerEndpointIndexer.java b/independent-projects/resteasy-reactive/server/processor/src/main/java/org/jboss/resteasy/reactive/server/processor/ServerEndpointIndexer.java index 991e53d5b6413..975ae12748c7f 100644 --- a/independent-projects/resteasy-reactive/server/processor/src/main/java/org/jboss/resteasy/reactive/server/processor/ServerEndpointIndexer.java +++ b/independent-projects/resteasy-reactive/server/processor/src/main/java/org/jboss/resteasy/reactive/server/processor/ServerEndpointIndexer.java @@ -54,6 +54,8 @@ import org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames; import org.jboss.resteasy.reactive.common.processor.transformation.AnnotationStore; import org.jboss.resteasy.reactive.server.core.parameters.ParameterExtractor; +import org.jboss.resteasy.reactive.server.core.parameters.converters.CharParamConverter; +import org.jboss.resteasy.reactive.server.core.parameters.converters.CharacterParamConverter; import org.jboss.resteasy.reactive.server.core.parameters.converters.ListConverter; import org.jboss.resteasy.reactive.server.core.parameters.converters.LoadedParameterConverter; import org.jboss.resteasy.reactive.server.core.parameters.converters.LocalDateParamConverter; @@ -472,6 +474,10 @@ private ParameterConverterSupplier extractConverter(String elementType, IndexVie return delegate; } else if (elementType.equals(PathSegment.class.getName())) { return new PathSegmentParamConverter.Supplier(); + } else if (elementType.equals("char")) { + return new CharParamConverter.Supplier(); + } else if (elementType.equals(Character.class.getName())) { + return new CharacterParamConverter.Supplier(); } return converterSupplierIndexerExtension.extractConverterImpl(elementType, indexView, existingConverters, errorLocation, hasRuntimeConverters); diff --git a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/parameters/converters/CharParamConverter.java b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/parameters/converters/CharParamConverter.java new file mode 100644 index 0000000000000..987d2f3370849 --- /dev/null +++ b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/parameters/converters/CharParamConverter.java @@ -0,0 +1,26 @@ +package org.jboss.resteasy.reactive.server.core.parameters.converters; + +public class CharParamConverter implements ParameterConverter { + + @Override + public Object convert(Object parameter) { + String str = parameter.toString(); + if (str.length() != 1) { + throw new IllegalArgumentException("invalid char value: " + str); + } + return str.charAt(0); + } + + public static class Supplier implements ParameterConverterSupplier { + + @Override + public String getClassName() { + return CharParamConverter.class.getName(); + } + + @Override + public CharParamConverter get() { + return new CharParamConverter(); + } + } +} diff --git a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/parameters/converters/CharacterParamConverter.java b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/parameters/converters/CharacterParamConverter.java new file mode 100644 index 0000000000000..d92353de2d7b6 --- /dev/null +++ b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/parameters/converters/CharacterParamConverter.java @@ -0,0 +1,26 @@ +package org.jboss.resteasy.reactive.server.core.parameters.converters; + +public class CharacterParamConverter implements ParameterConverter { + + @Override + public Object convert(Object parameter) { + String str = parameter.toString(); + if (str.length() != 1) { + throw new IllegalArgumentException("invalid Character value: " + str); + } + return Character.valueOf(str.charAt(0)); + } + + public static class Supplier implements ParameterConverterSupplier { + + @Override + public String getClassName() { + return CharacterParamConverter.class.getName(); + } + + @Override + public CharacterParamConverter get() { + return new CharacterParamConverter(); + } + } +} diff --git a/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/simple/SimpleQuarkusRestResource.java b/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/simple/SimpleQuarkusRestResource.java index 46f3dc34565c7..11470c6f9ce9e 100644 --- a/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/simple/SimpleQuarkusRestResource.java +++ b/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/simple/SimpleQuarkusRestResource.java @@ -75,8 +75,10 @@ public String get(@PathParam("id") String id) { public String params(@PathParam("p") String p, @QueryParam("q") String q, @HeaderParam("h") int h, + @HeaderParam("h2") char h2, + @HeaderParam("h3") Character h3, @FormParam("f") String f) { - return "params: p: " + p + ", q: " + q + ", h: " + h + ", f: " + f; + return "params: p: " + p + ", q: " + q + ", h: " + h + ", h2: " + h2 + ", h3: " + h3 + ", f: " + f; } @POST diff --git a/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/simple/SimpleQuarkusRestTestCase.java b/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/simple/SimpleQuarkusRestTestCase.java index 66cea63b123e4..84ad075b1cd7f 100644 --- a/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/simple/SimpleQuarkusRestTestCase.java +++ b/independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/simple/SimpleQuarkusRestTestCase.java @@ -109,9 +109,11 @@ public void testParams() { RestAssured.with() .queryParam("q", "qv") .header("h", "123") + .header("h2", "a") + .header("h3", "b") .formParam("f", "fv") .post("/simple/params/pv") - .then().body(Matchers.equalTo("params: p: pv, q: qv, h: 123, f: fv")); + .then().statusCode(200).body(Matchers.equalTo("params: p: pv, q: qv, h: 123, h2: a, h3: b, f: fv")); } @Test