Skip to content

Commit

Permalink
Merge pull request #21193 from phillip-kruger/auto-tag-abstract-class
Browse files Browse the repository at this point in the history
OpenAPI: better auto tag when using interfaces / abstract classes
  • Loading branch information
geoand authored Nov 4, 2021
2 parents e709757 + fd2cd2c commit e910bef
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -427,7 +428,21 @@ private Map<String, String> 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<ClassInfo> allKnownImplementors = apiFilteredIndexViewBuildItem.getIndex()
.getAllKnownImplementors(method.declaringClass().name());
for (ClassInfo impl : allKnownImplementors) {
classNames.put(ref, impl.simpleName());
}
} else if (Modifier.isAbstract(method.declaringClass().flags())) {
Collection<ClassInfo> 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T> {
@GET
@Path("/{id}")
T getById(@PathParam("id") long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.quarkus.smallrye.openapi.test.jaxrs;

import javax.ws.rs.Path;

@Path("/address")
public class AutoTagResource implements AbstractAutoTagResource<String> {
@Override
public String getById(long id) {
return "Disney Land, Gate " + id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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"));
}

}

0 comments on commit e910bef

Please sign in to comment.