'Add annotation with a Quarkus extension
Here is what I like to achieve.
My Quarkus app is using a homemade Quarkus extension that is securing my API endpoints with a custom annotation:
@NameBinding
@Retention(RetentionPolicy.RUNTIME)
public @interface AuthorizationSecured {
@Nonbinding String[] permissions() default{};
}
I want this custom annotation to automatically annotate my endpoints with this openapi annotation:
// org.eclipse.microprofile.openapi.annotations.security.SecurityRequirement
@SecurityRequirement(name = "jwt")
So that in swagger-ui, I can have the padlock icon displayed and specify my token when clicking on the icon.
Any idea?
EDIT:
I tried @Ladicek suggestion, here is my processor:
class AuthorizeProcessor {
@BuildStep
FeatureBuildItem feature() {
return new FeatureBuildItem("authorize");
}
@BuildStep
AdditionalBeanBuildItem registerConfigValidator() {
// some stuff here...
}
@BuildStep
AnnotationsTransformerBuildItem transform() {
return new AnnotationsTransformerBuildItem(new AnnotationsTransformer() {
public boolean appliesTo(org.jboss.jandex.AnnotationTarget.Kind kind) {
return kind == AnnotationTarget.Kind.METHOD;
}
public void transform(TransformationContext context) {
if (context.getTarget().asMethod().hasAnnotation(DotName.createSimple("com.software.company.AuthorizationSecured"))) {
context.transform().add(DotName.createSimple("org.eclipse.microprofile.openapi.annotations.security.SecurityRequirement"), AnnotationValue.createStringValue("name", "jwt")).done();
}
}
});
}
@BuildStep
void registerAuthorizeFilter(
BuildProducer<AdditionalBeanBuildItem> additionalBeanProducer,
BuildProducer<AdditionalIndexedClassesBuildItem> additionalIndexedClassesProducer,
BuildProducer<ResteasyJaxrsProviderBuildItem> resteasyJaxrsProviderProducer) {
// some stuff here...
}
}
The annotation transformer seems to do its job, I have printed some stuff in the console:
************* Found method name = secured
************* annotations before > [@GET, @Path(value = "/secured"), @Produces(value = ["text/plain"]), @AuthorizationSecured]
************* annotations after > [@GET, @Path(value = "/secured"), @Produces(value = ["text/plain"]), @AuthorizationSecured, @org.eclipse.microprofile.openapi.annotations.security.SecurityRequirement(name = "jwt")]
But actually, I don't see any effect at runtime on the app that is using the extension. What am I missing here?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
