Skip to content

Commit

Permalink
Merge pull request #35778 from geoand/#35774
Browse files Browse the repository at this point in the history
Fix generic handling of ParamConverter
  • Loading branch information
geoand authored Sep 6, 2023
2 parents 49a80d0 + c3479a0 commit 27da5f0
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package io.quarkus.resteasy.reactive.server.test.simple;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.List;
import java.util.stream.Collectors;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.ext.ParamConverter;
import jakarta.ws.rs.ext.ParamConverterProvider;
import jakarta.ws.rs.ext.Provider;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class GenericsParamConverterTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(TestEnum.class, Wrapper.class,
WrapperParamConverterProvider.class, WrapperParamConverterProvider.WrapperParamConverter.class,
TestResource.class));

@Test
public void wrapper() {
given()
.when().get("/test/single?wrapper=ACTIVE")
.then()
.statusCode(200)
.body(is("ACTIVE"));
}

@Test
public void wrapperList() {
given()
.when().get("/test/list?wrapperList=INACTIVE&wrapperList=ACTIVE")
.then()
.statusCode(200)
.body(is("INACTIVE,ACTIVE"));
}

@Path("/test")
public static class TestResource {

@GET
@Path("/list")
public String list(@QueryParam("wrapperList") final List<Wrapper<TestEnum>> wrapperList) {
return wrapperList.stream().map(w -> w.getValue().name()).collect(Collectors.joining(","));
}

@GET
@Path("/single")
public String single(@QueryParam("wrapper") final Wrapper<TestEnum> wrapper) {
return wrapper.getValue().toString();
}
}

public enum TestEnum {
ACTIVE,
INACTIVE
}

public static class Wrapper<E extends Enum<E>> {
private final E value;

public Wrapper(final E value) {
this.value = value;
}

public E getValue() {
return value;
}
}

@Provider
public static class WrapperParamConverterProvider implements ParamConverterProvider {

@Override
@SuppressWarnings("unchecked")
public <T> ParamConverter<T> getConverter(final Class<T> rawType, final Type genericType,
final Annotation[] annotations) {
if (Wrapper.class.isAssignableFrom(rawType)) {
return (ParamConverter<T>) new WrapperParamConverter();
}
return null;
}

public static class WrapperParamConverter implements ParamConverter<Wrapper<?>> {

@Override
public Wrapper<?> fromString(String value) {
return new Wrapper<>(Enum.valueOf(TestEnum.class, value));
}

@Override
public String toString(Wrapper<?> wrapper) {
return wrapper != null ? wrapper.getValue().toString() : null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,8 @@ private static void smartInitParameterConverter(int i, ParameterConverter quarku
Type genericType = genericArguments[0];
if (genericType instanceof Class) {
genericTypeClassName = ((Class<?>) genericType).getName();
} else if (genericType instanceof ParameterizedType) {
genericTypeClassName = ((ParameterizedType) genericType).getRawType().getTypeName();
} else if (genericType instanceof WildcardType) {
WildcardType genericTypeWildcardType = (WildcardType) genericType;
Type[] upperBounds = genericTypeWildcardType.getUpperBounds();
Expand Down

0 comments on commit 27da5f0

Please sign in to comment.