Skip to content

Commit

Permalink
Fix annotation check for controller methods in RESTEasy
Browse files Browse the repository at this point in the history
While fixing #334 (PR #335) we introduced a small bug during the check
if a method needs to be processed by the general validator or not.
Because of this bug, the implementation checked only if a method is
annotated with @controller, but not if this is the case for the whole
class. The result was Krazo not handling invalid input correctly.

This fix adds the ability to check if the method OR the declaring class
is annotated with @controller, so we ensure we really have something
that needs to be handled by MVC.

This fix needs to be backported to Krazo 3.0.x.

fixes: #342
  • Loading branch information
erdlet committed Sep 30, 2022
1 parent 91498d5 commit 18b2772
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
14 changes: 14 additions & 0 deletions core/src/main/java/org/eclipse/krazo/util/AnnotationUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,20 @@ public static <T extends Annotation> boolean hasAnnotationOnClassOrMethod(Class<
|| Arrays.stream(clazz.getMethods()).anyMatch(m -> hasAnnotation(m, annotationType));
}

/**
* Determines if an annotation is present on a method or its declaring class.
*
* @param method the {@link Method} to check for a specific annotation
* @param annotationType the annotation's type
* @param <T> the type of the annotation
* @return <code>true</code> in case the method or its declaring class is annotated with the passed annotationType
* @see #hasAnnotation(Class, Class)
* @see #hasAnnotation(Method, Class)
*/
public static <T extends Annotation> boolean hasAnnotationOnClassOrMethod(final Method method, Class<T> annotationType) {
return hasAnnotation(method.getDeclaringClass(), annotationType) || hasAnnotation(method, annotationType);
}

/**
* Determines if a method has one or more MVC or JAX-RS annotations on it.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.eclipse.krazo.util;

import jakarta.ws.rs.POST;
import org.junit.Test;

import jakarta.enterprise.context.RequestScoped;
Expand Down Expand Up @@ -97,6 +98,13 @@ public void hasAnnotationOnMethod() throws NoSuchMethodException {
assertFalse(AnnotationUtils.hasAnnotation(NoInheritanceController.class.getMethod("start"), Path.class));
}

@Test
public void hasAnnotationOnClassOrMethodForMethod() throws NoSuchMethodException {
assertTrue(AnnotationUtils.hasAnnotationOnClassOrMethod(someController.getClass().getMethod("start"), Controller.class));
assertTrue(AnnotationUtils.hasAnnotationOnClassOrMethod(someController.getClass().getMethod("start"), View.class));
assertFalse(AnnotationUtils.hasAnnotationOnClassOrMethod(someController.getClass().getMethod("start"), POST.class));
}

@Controller
@Path("start")
static class SomeController {
Expand Down Expand Up @@ -145,6 +153,7 @@ public static class NoInheritanceController extends BaseController {
public void start() {
}
}

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public boolean isValidatable(Class<?> clazz) {

@Override
public boolean isMethodValidatable(Method method) {

boolean mvcControllerMethod = AnnotationUtils.hasAnnotation(method, Controller.class);
// method is validatable when either class or method is annotated.
boolean mvcControllerMethod = AnnotationUtils.hasAnnotationOnClassOrMethod(method, Controller.class);

return !mvcControllerMethod && delegate.isMethodValidatable(method);

Expand Down

0 comments on commit 18b2772

Please sign in to comment.