forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle generic types for ParamConverter in REST Client
Fixes: quarkusio#36639 (cherry picked from commit 1aff10f)
- Loading branch information
1 parent
396a6f3
commit 7e1e955
Showing
2 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
...oyment/src/test/java/io/quarkus/rest/client/reactive/converter/GenericsConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package io.quarkus.rest.client.reactive.converter; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
import java.net.URI; | ||
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.eclipse.microprofile.rest.client.RestClientBuilder; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.test.common.http.TestHTTPResource; | ||
|
||
public class GenericsConverterTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest(); | ||
|
||
@TestHTTPResource | ||
URI baseUri; | ||
|
||
@Test | ||
void testSingle() { | ||
TestClient client = RestClientBuilder.newBuilder().baseUri(baseUri) | ||
.build(TestClient.class); | ||
var result = client.wrapper(new WrapperClass<>(StatusEnum.ACTIVE)); | ||
assertEquals("ACTIVE", result); | ||
} | ||
|
||
@Test | ||
void testList() { | ||
TestClient client = RestClientBuilder.newBuilder().baseUri(baseUri) | ||
.build(TestClient.class); | ||
var result = client | ||
.wrapperList(List.of(new WrapperClass<>(StatusEnum.ACTIVE), new WrapperClass<>(StatusEnum.INACTIVE))); | ||
assertEquals("ACTIVE,INACTIVE", result); | ||
} | ||
|
||
public enum StatusEnum { | ||
|
||
ACTIVE, | ||
INACTIVE | ||
} | ||
|
||
public static class WrapperClass<E extends Enum<E>> { | ||
|
||
private final E value; | ||
|
||
public WrapperClass(final E value) { | ||
this.value = value; | ||
} | ||
|
||
public E getValue() { | ||
return value; | ||
} | ||
} | ||
|
||
public static class WrapperClassParamConverter implements ParamConverter<WrapperClass<?>> { | ||
|
||
@Override | ||
public WrapperClass<?> fromString(String value) { | ||
return new WrapperClass<>(Enum.valueOf(StatusEnum.class, value)); | ||
} | ||
|
||
@Override | ||
public String toString(WrapperClass<?> wrapperClass) { | ||
return wrapperClass != null ? wrapperClass.getValue().toString() : null; | ||
} | ||
|
||
} | ||
|
||
@Provider | ||
public static class WrapperClassParamConverterProvider implements ParamConverterProvider { | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { | ||
if (WrapperClass.class.isAssignableFrom(rawType)) { | ||
return (ParamConverter<T>) new WrapperClassParamConverter(); | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
|
||
@Path("/test") | ||
public static class TestResource { | ||
|
||
@GET | ||
@Path("/single") | ||
public String wrapper(@QueryParam("wrapper") final WrapperClass<StatusEnum> wrapper) { | ||
return wrapper.getValue().toString(); | ||
} | ||
|
||
@GET | ||
@Path("/list") | ||
public String wrapperList(@QueryParam("wrapperList") final List<WrapperClass<StatusEnum>> wrapperList) { | ||
return wrapperList.stream().map(WrapperClass::getValue).map(Enum::name).collect(Collectors.joining(",")); | ||
} | ||
|
||
} | ||
|
||
@Path("/test") | ||
public interface TestClient { | ||
|
||
@GET | ||
@Path("/single") | ||
String wrapper(@QueryParam("wrapper") final WrapperClass<StatusEnum> wrapper); | ||
|
||
@GET | ||
@Path("/list") | ||
String wrapperList(@QueryParam("wrapperList") final List<WrapperClass<StatusEnum>> wrapperList); | ||
|
||
} | ||
|
||
} |