forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly use ParamConverter to converting the values of collection types
Fixes: quarkusio#27515 (cherry picked from commit 2b85546)
- Loading branch information
Showing
6 changed files
with
162 additions
and
2 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
.../src/test/java/io/quarkus/resteasy/reactive/server/test/providers/ParamConverterTest.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,110 @@ | ||
package io.quarkus.resteasy.reactive.server.test.providers; | ||
|
||
import static io.restassured.RestAssured.get; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.QueryParam; | ||
import javax.ws.rs.ext.ParamConverter; | ||
import javax.ws.rs.ext.ParamConverterProvider; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ParamConverterTest { | ||
|
||
private static final String STATIC_UUID = "42f425f1-5923-41ca-a43c-713888762c68"; | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(UUIDResource.class, UUIDParamConverterProvider.class)); | ||
|
||
@Test | ||
public void single() { | ||
get("/uuid/single?id=whatever") | ||
.then() | ||
.statusCode(200) | ||
.body(Matchers.equalTo(STATIC_UUID)); | ||
} | ||
|
||
@Test | ||
public void set() { | ||
get("/uuid/set?id=whatever&id=whatever2") | ||
.then() | ||
.statusCode(200) | ||
.body(Matchers.equalTo(STATIC_UUID)); | ||
} | ||
|
||
@Test | ||
public void list() { | ||
get("/uuid/list?id=whatever&id=whatever2") | ||
.then() | ||
.statusCode(200) | ||
.body(Matchers.equalTo(STATIC_UUID + "," + STATIC_UUID)); | ||
} | ||
|
||
@Path("uuid") | ||
public static class UUIDResource { | ||
|
||
@Path("single") | ||
@GET | ||
public String single(@QueryParam("id") UUID uuid) { | ||
return uuid.toString(); | ||
} | ||
|
||
@Path("set") | ||
@GET | ||
public String set(@QueryParam("id") Set<UUID> uuids) { | ||
return join(uuids.stream()); | ||
} | ||
|
||
@Path("list") | ||
@GET | ||
public String list(@QueryParam("id") List<UUID> uuids) { | ||
return join(uuids.stream()); | ||
} | ||
|
||
private static String join(Stream<UUID> uuids) { | ||
return uuids.map(UUID::toString).collect(Collectors.joining(",")); | ||
} | ||
} | ||
|
||
@Provider | ||
public static class UUIDParamConverterProvider implements ParamConverterProvider { | ||
@SuppressWarnings("unchecked") | ||
@Override | ||
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { | ||
if (rawType.equals(UUID.class)) { | ||
return (ParamConverter<T>) new UUIDParamConverter(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static class UUIDParamConverter implements ParamConverter<UUID> { | ||
@Override | ||
public UUID fromString(String value) { | ||
return UUID.fromString(STATIC_UUID); | ||
} | ||
|
||
@Override | ||
public String toString(UUID value) { | ||
return value.toString(); | ||
} | ||
} | ||
|
||
} | ||
} |
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
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
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
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
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