Skip to content

Commit

Permalink
Merge pull request #24549 from geoand/#24541
Browse files Browse the repository at this point in the history
Properly handle char and Character parameter types in JAX-RS methods
  • Loading branch information
FroMage authored Mar 29, 2022
2 parents b31e400 + 18a6af3 commit 850908a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 850908a

Please sign in to comment.