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

Fix generic handling of ParamConverter #35778

Merged
merged 1 commit into from
Sep 6, 2023
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
@@ -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