Skip to content

Commit cc8425d

Browse files
committed
Vert.x web - support RX routing context
- resolves #1589 - also validate method return type
1 parent 820bc6c commit cc8425d

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

extensions/scheduler/deployment/src/main/java/io/quarkus/scheduler/deployment/SchedulerProcessor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,13 @@ public void validate(ValidationContext validationContext) {
171171
if (params.size() > 1
172172
|| (params.size() == 1 && !params.get(0).equals(SCHEDULED_EXECUTION_TYPE))) {
173173
throw new IllegalStateException(String.format(
174-
"Invalid scheduled business method parameters %s [method: %s, bean:%s", params,
174+
"Invalid scheduled business method parameters %s [method: %s, bean: %s]", params,
175175
method, bean));
176176
}
177177
if (!method.returnType().kind().equals(Type.Kind.VOID)) {
178178
throw new IllegalStateException(
179-
String.format("Scheduled business method must return void [method: %s, bean:%s",
180-
method.returnType(), method, bean));
179+
String.format("Scheduled business method must return void [method: %s, bean: %s]",
180+
method, bean));
181181
}
182182
// Validate cron() and every() expressions
183183
for (AnnotationInstance scheduled : schedules) {

extensions/vertx-web/deployment/src/main/java/io/quarkus/vertx/web/deployment/VertxWebProcessor.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class VertxWebProcessor {
6464
private static final DotName ROUTE = DotName.createSimple(Route.class.getName());
6565
private static final DotName ROUTES = DotName.createSimple(Route.Routes.class.getName());
6666
private static final DotName ROUTING_CONTEXT = DotName.createSimple(RoutingContext.class.getName());
67+
private static final DotName RX_ROUTING_CONTEXT = DotName
68+
.createSimple(io.vertx.reactivex.ext.web.RoutingContext.class.getName());
6769
private static final DotName ROUTING_EXCHANGE = DotName.createSimple(RoutingExchange.class.getName());
6870
private static final String HANDLER_SUFFIX = "_RouteHandler";
6971

@@ -93,13 +95,13 @@ public void validate(ValidationContext validationContext) {
9395
List<AnnotationInstance> routes = new LinkedList<>();
9496
AnnotationInstance routeAnnotation = annotationStore.getAnnotation(method, ROUTE);
9597
if (routeAnnotation != null) {
96-
validateMethodParameters(bean, method);
98+
validateMethod(bean, method);
9799
routes.add(routeAnnotation);
98100
}
99101
if (routes.isEmpty()) {
100102
AnnotationInstance routesAnnotation = annotationStore.getAnnotation(method, ROUTES);
101103
if (routesAnnotation != null) {
102-
validateMethodParameters(bean, method);
104+
validateMethod(bean, method);
103105
Collections.addAll(routes, routesAnnotation.value().asNestedArray());
104106
}
105107
}
@@ -169,12 +171,23 @@ public void transform(TransformationContext context) {
169171
});
170172
}
171173

172-
private void validateMethodParameters(BeanInfo bean, MethodInfo method) {
174+
private void validateMethod(BeanInfo bean, MethodInfo method) {
175+
if (!method.returnType().kind().equals(Type.Kind.VOID)) {
176+
throw new IllegalStateException(
177+
String.format("Route handler business method must return void [method: %s, bean: %s]", method, bean));
178+
}
173179
List<Type> params = method.parameters();
174-
if (params.size() != 1
175-
|| !(params.get(0).name().equals(ROUTING_CONTEXT) || params.get(0).name().equals(ROUTING_EXCHANGE))) {
180+
boolean hasInvalidParam = true;
181+
if (params.size() == 1) {
182+
DotName paramTypeName = params.get(0).name();
183+
if (ROUTING_CONTEXT.equals(paramTypeName) || RX_ROUTING_CONTEXT.equals(paramTypeName)
184+
|| ROUTING_EXCHANGE.equals(paramTypeName)) {
185+
hasInvalidParam = false;
186+
}
187+
}
188+
if (hasInvalidParam) {
176189
throw new IllegalStateException(String.format(
177-
"Route handler business method must accept exactly one parameter of type RoutingContext/RoutingExchange: %s [method: %s, bean:%s",
190+
"Route handler business method must accept exactly one parameter of type RoutingContext/RoutingExchange: %s [method: %s, bean: %s]",
178191
params, method, bean));
179192
}
180193
}
@@ -223,6 +236,12 @@ private String generateHandler(BeanInfo bean, MethodInfo method, ClassOutput cla
223236
paramHandle = invoke.getMethodParam(0);
224237
methodDescriptor = MethodDescriptor.ofMethod(bean.getImplClazz().name().toString(), method.name(), void.class,
225238
RoutingContext.class);
239+
} else if (method.parameters().get(0).name().equals(RX_ROUTING_CONTEXT)) {
240+
paramHandle = invoke.newInstance(
241+
MethodDescriptor.ofConstructor(io.vertx.reactivex.ext.web.RoutingContext.class, RoutingContext.class),
242+
invoke.getMethodParam(0));
243+
methodDescriptor = MethodDescriptor.ofMethod(bean.getImplClazz().name().toString(), method.name(), void.class,
244+
io.vertx.reactivex.ext.web.RoutingContext.class);
226245
} else {
227246
paramHandle = invoke.newInstance(MethodDescriptor.ofConstructor(RoutingExchangeImpl.class, RoutingContext.class),
228247
invoke.getMethodParam(0));

extensions/vertx-web/deployment/src/test/java/io/quarkus/vertx/web/SimpleRouteTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class SimpleRouteTest {
3333
@Test
3434
public void testSimpleRoute() {
3535
RestAssured.when().get("/hello").then().statusCode(200).body(is("Hello world!"));
36+
RestAssured.when().get("/rx-hello").then().statusCode(200).body(is("Hello world!"));
3637
RestAssured.when().get("/bzuk").then().statusCode(200).body(is("Hello world!"));
3738
RestAssured.when().get("/hello-event-bus?name=ping").then().statusCode(200).body(is("Hello PING!"));
3839
RestAssured.when().get("/foo?name=foo").then().statusCode(200).body(is("Hello foo!"));
@@ -52,6 +53,12 @@ void hello(RoutingContext context) {
5253
context.response().setStatusCode(200).end("Hello " + (name != null ? name : "world") + "!");
5354
}
5455

56+
@Route(path = "/rx-hello")
57+
void rxHello(io.vertx.reactivex.ext.web.RoutingContext context) {
58+
String name = context.request().getParam("name");
59+
context.response().setStatusCode(200).end("Hello " + (name != null ? name : "world") + "!");
60+
}
61+
5562
@Route(path = "/bzuk")
5663
void bzuk(RoutingExchange exchange) {
5764
exchange.ok("Hello " + exchange.getParam("name").orElse("world") + "!");

extensions/vertx-web/runtime/src/main/java/io/quarkus/vertx/web/Route.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
/**
1616
* Annotation used to configure a {@link io.quarkus.vertx.web.Route} in a declarative way.
17-
*
18-
* The target business method must accept exacly one argument of type {@link RoutingContext}.
17+
* <p>
18+
* The target business method must return {@code void} and accept exacly one argument of type {@link RoutingContext}.
1919
*/
2020
@Repeatable(Routes.class)
2121
@Retention(RetentionPolicy.RUNTIME)

0 commit comments

Comments
 (0)