Skip to content

Commit

Permalink
Warn when JAX-RS @path is used along with MP Health annotations
Browse files Browse the repository at this point in the history
It's not unreasonable for users to expect these annotations to work
together, so let's be explicit that they don't.

Resolves: quarkusio#10720
  • Loading branch information
geoand committed Jul 23, 2020
1 parent 100bc39 commit 8d37078
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.microprofile.health.Readiness;
import org.eclipse.microprofile.health.spi.HealthCheckResponseProvider;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationTarget.Kind;
import org.jboss.jandex.DotName;
import org.jboss.jandex.IndexView;
Expand Down Expand Up @@ -88,6 +89,7 @@ class SmallRyeHealthProcessor {
private static final DotName READINESS = DotName.createSimple(Readiness.class.getName());
private static final DotName HEALTH_GROUP = DotName.createSimple(HealthGroup.class.getName());
private static final DotName HEALTH_GROUPS = DotName.createSimple(HealthGroups.class.getName());
private static final DotName JAX_RS_PATH = DotName.createSimple("javax.ws.rs.Path");

// For the UI
private static final String HEALTH_UI_WEBJAR_GROUP_ID = "io.smallrye";
Expand Down Expand Up @@ -176,6 +178,10 @@ public void defineHealthRoutes(BuildProducer<RouteBuildItem> routes,
BeanArchiveIndexBuildItem beanArchiveIndex) {
IndexView index = beanArchiveIndex.getIndex();

// log a warning if users try to use @Liveness or @Readiness with JAX-RS @Path
warnIfJaxRsPathUsed(index, LIVENESS);
warnIfJaxRsPathUsed(index, READINESS);

// Register the health handler
routes.produce(new RouteBuildItem(health.rootPath, new SmallRyeHealthHandler(), HandlerType.BLOCKING));

Expand Down Expand Up @@ -215,6 +221,29 @@ public void defineHealthRoutes(BuildProducer<RouteBuildItem> routes,
}
}

private void warnIfJaxRsPathUsed(IndexView index, DotName healthAnnotation) {
Collection<AnnotationInstance> instances = index.getAnnotations(healthAnnotation);
for (AnnotationInstance instance : instances) {
boolean containsPath = false;

AnnotationTarget target = instance.target();
if (target.kind() == Kind.CLASS) {
if (target.asClass().classAnnotation(JAX_RS_PATH) != null) {
containsPath = true;
}
} else if (target.kind() == Kind.METHOD) {
if (target.asMethod().hasAnnotation(JAX_RS_PATH)) {
containsPath = true;
}
}
if (containsPath) {
LOG.warnv(
"The use of @Path has no effect when @{0} is used and should therefore be removed. Offending target is {1}: {2}",
healthAnnotation.withoutPackagePrefix(), target.kind(), target);
}
}
}

@BuildStep
public void kubernetes(HttpBuildTimeConfig httpConfig,
BuildProducer<KubernetesHealthLivenessPathBuildItem> livenessPathItemProducer,
Expand Down

0 comments on commit 8d37078

Please sign in to comment.