From fd2cd2c1ab0098b09450a1c5fc7b7d8f8bbf20f0 Mon Sep 17 00:00:00 2001 From: Phillip Kruger Date: Thu, 4 Nov 2021 15:28:58 +0200 Subject: [PATCH] OpenAPI: better auto tag when using interfaces / abstract classes Signed-off-by:Phillip Kruger --- .../deployment/SmallRyeOpenApiProcessor.java | 17 ++++++++++++++++- .../test/jaxrs/AbstractAutoTagResource.java | 14 ++++++++++++++ .../openapi/test/jaxrs/AutoTagResource.java | 11 +++++++++++ .../openapi/test/jaxrs/AutoTagTestCase.java | 11 ++++++++++- 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/AbstractAutoTagResource.java create mode 100644 extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/AutoTagResource.java diff --git a/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java b/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java index bf051288bde8c..40d1b220a8e62 100644 --- a/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java +++ b/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -427,7 +428,21 @@ private Map getClassNamesMethodReferences(OpenApiFilteredIndexVi if (ai.target().kind().equals(AnnotationTarget.Kind.METHOD)) { MethodInfo method = ai.target().asMethod(); String ref = JandexUtil.createUniqueMethodReference(method); - classNames.put(ref, method.declaringClass().simpleName()); + if (Modifier.isInterface(method.declaringClass().flags())) { + Collection allKnownImplementors = apiFilteredIndexViewBuildItem.getIndex() + .getAllKnownImplementors(method.declaringClass().name()); + for (ClassInfo impl : allKnownImplementors) { + classNames.put(ref, impl.simpleName()); + } + } else if (Modifier.isAbstract(method.declaringClass().flags())) { + Collection allKnownSubclasses = apiFilteredIndexViewBuildItem.getIndex() + .getAllKnownSubclasses(method.declaringClass().name()); + for (ClassInfo impl : allKnownSubclasses) { + classNames.put(ref, impl.simpleName()); + } + } else { + classNames.put(ref, method.declaringClass().simpleName()); + } } } return classNames; diff --git a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/AbstractAutoTagResource.java b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/AbstractAutoTagResource.java new file mode 100644 index 0000000000000..b778172e8653a --- /dev/null +++ b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/AbstractAutoTagResource.java @@ -0,0 +1,14 @@ +package io.quarkus.smallrye.openapi.test.jaxrs; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +@Produces(MediaType.TEXT_PLAIN) +public interface AbstractAutoTagResource { + @GET + @Path("/{id}") + T getById(@PathParam("id") long id); +} \ No newline at end of file diff --git a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/AutoTagResource.java b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/AutoTagResource.java new file mode 100644 index 0000000000000..b01b89aecb6c2 --- /dev/null +++ b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/AutoTagResource.java @@ -0,0 +1,11 @@ +package io.quarkus.smallrye.openapi.test.jaxrs; + +import javax.ws.rs.Path; + +@Path("/address") +public class AutoTagResource implements AbstractAutoTagResource { + @Override + public String getById(long id) { + return "Disney Land, Gate " + id; + } +} \ No newline at end of file diff --git a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/AutoTagTestCase.java b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/AutoTagTestCase.java index e9cf43e4e92ab..126c8fc2eda39 100644 --- a/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/AutoTagTestCase.java +++ b/extensions/smallrye-openapi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/AutoTagTestCase.java @@ -11,7 +11,7 @@ public class AutoTagTestCase { @RegisterExtension static QuarkusUnitTest runner = new QuarkusUnitTest() .withApplicationRoot((jar) -> jar - .addClasses(OpenApiResourceWithNoTag.class)); + .addClasses(OpenApiResourceWithNoTag.class, AutoTagResource.class, AbstractAutoTagResource.class)); @Test public void testAutoSecurityRequirement() { @@ -28,4 +28,13 @@ public void testAutoSecurityRequirement() { } + @Test + public void testTagInOpenApi() { + RestAssured.given().header("Accept", "application/json") + .when().get("/q/openapi") + .then() + .statusCode(200) + .body(Matchers.containsString("Auto Tag Resource")); + } + }