Skip to content

Commit

Permalink
Ensure suspend methods in RESTEasy Reactive use the same default medi…
Browse files Browse the repository at this point in the history
…a types

Fixes: #18145
  • Loading branch information
geoand committed Jun 25, 2021
1 parent 659435d commit 5d8ce28
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.jboss.jandex.MethodInfo;
import org.jboss.jandex.Type;
import org.jboss.resteasy.reactive.common.model.ResourceMethod;
import org.jboss.resteasy.reactive.common.processor.EndpointIndexer;
import org.jboss.resteasy.reactive.common.processor.HashUtil;
import org.jboss.resteasy.reactive.server.core.parameters.NullParamExtractor;
import org.jboss.resteasy.reactive.server.core.parameters.ParameterExtractor;
Expand Down Expand Up @@ -106,6 +107,14 @@ public ParameterExtractor handleCustomParameter(Type paramType, Map<DotName, Ann
//look for methods that take a Continuation, these are suspendable and need to be handled differently
if (paramType.name().equals(CONTINUATION)) {
methodContext.put(NAME, true);
if (paramType.kind() == Type.Kind.PARAMETERIZED_TYPE) {
Type firstGenericType = paramType.asParameterizedType().arguments().get(0);
if (firstGenericType.kind() == Type.Kind.WILDCARD_TYPE) {
methodContext.put(EndpointIndexer.METHOD_CONTEXT_CUSTOM_RETURN_TYPE_KEY,
firstGenericType.asWildcardType().superBound());
}

}
return new NullParamExtractor();
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ public abstract class EndpointIndexer<T extends EndpointIndexer<T, PARAM, METHOD
private static final String[] PRODUCES_PLAIN_TEXT = new String[] { MediaType.TEXT_PLAIN };
public static final String CDI_WRAPPER_SUFFIX = "$$CDIWrapper";

public static final String METHOD_CONTEXT_CUSTOM_RETURN_TYPE_KEY = "METHOD_CONTEXT_CUSTOM_RETURN_TYPE_KEY";

static {
Map<String, String> prims = new HashMap<>();
prims.put(byte.class.getName(), Byte.class.getName());
Expand Down Expand Up @@ -468,7 +470,9 @@ private ResourceMethod createResourceMethod(ClassInfo currentClassInfo, ClassInf
}
}

Type nonAsyncReturnType = getNonAsyncReturnType(currentMethodInfo.returnType());
Type nonAsyncReturnType = getNonAsyncReturnType(methodContext.containsKey(METHOD_CONTEXT_CUSTOM_RETURN_TYPE_KEY)
? (Type) methodContext.get(METHOD_CONTEXT_CUSTOM_RETURN_TYPE_KEY)
: currentMethodInfo.returnType());
addWriterForType(additionalWriters, nonAsyncReturnType);

String[] produces = extractProducesConsumesValues(currentMethodInfo.annotation(PRODUCES), classProduces);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static class TestEndpoint {

@Path("hello")
@Blocking
public Object hello(kotlin.coroutines.Continuation<?> cont) {
public Object hello(kotlin.coroutines.Continuation<? super Object> cont) {
throw new IllegalStateException("should never be called");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.quarkus.it.resteasy.reactive.kotlin

import javax.ws.rs.GET
import javax.ws.rs.Path

@Path("/greeting")
class GreetingResource {

@GET
suspend fun testSuspend() = Greeting("hello")
}

data class Greeting(val message:String)
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ mp.messaging.outgoing.countries-t2-out.value.serializer=org.apache.kafka.common.
mp.messaging.incoming.countries-t2-in.connector=smallrye-kafka
mp.messaging.incoming.countries-t2-in.topic=countries-t2
mp.messaging.incoming.countries-t2-in.auto.offset.reset=earliest
mp.messaging.incoming.countries-t2-in.value.deserializer=org.apache.kafka.common.serialization.StringDeserializer
mp.messaging.incoming.countries-t2-in.value.deserializer=org.apache.kafka.common.serialization.StringDeserializer
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.it.resteasy.reactive.kotlin

import io.quarkus.test.junit.QuarkusTest
import io.restassured.RestAssured
import io.restassured.http.ContentType
import org.hamcrest.CoreMatchers
import org.junit.jupiter.api.Test

@QuarkusTest
class GreetingResourceTest {

@Test
fun testDataClass() {
RestAssured.given()
.`when`()["/greeting"]
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.body("message", CoreMatchers.`is`("hello"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.quarkus.it.resteasy.reactive.kotlin

import io.quarkus.test.junit.QuarkusTest
import io.restassured.RestAssured
import io.restassured.http.ContentType
import org.hamcrest.CoreMatchers
import org.junit.jupiter.api.Test

Expand All @@ -14,6 +15,7 @@ class ReactiveClientTest {
.`when`()["/country/name/foo"]
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.body("$.size()", CoreMatchers.`is`(1),
"[0].capital", CoreMatchers.`is`("foo-capital"))
}
Expand Down

0 comments on commit 5d8ce28

Please sign in to comment.