Skip to content

Commit

Permalink
Add tag class names in generated REST Data Panache resources
Browse files Browse the repository at this point in the history
Fix #26400

(cherry picked from commit 308f538)
  • Loading branch information
Sgitario authored and gsmet committed Jun 28, 2022
1 parent 8a29421 commit 71924e6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
Expand Up @@ -49,12 +49,16 @@ public void testOpenApiForGeneratedResources() {
.header("Content-Type", "application/json;charset=UTF-8")
.body("info.title", Matchers.equalTo("quarkus-hibernate-orm-rest-data-panache-deployment API"))
.body("paths.'/collections'", Matchers.hasKey("get"))
.body("paths.'/collections'.get.tags", Matchers.hasItem("CollectionsResource"))
.body("paths.'/collections'", Matchers.hasKey("post"))
.body("paths.'/collections'.post.tags", Matchers.hasItem("CollectionsResource"))
.body("paths.'/collections/{id}'", Matchers.hasKey("get"))
.body("paths.'/collections/{id}'", Matchers.hasKey("put"))
.body("paths.'/collections/{id}'", Matchers.hasKey("delete"))
.body("paths.'/empty-list-items'", Matchers.hasKey("get"))
.body("paths.'/empty-list-items'.get.tags", Matchers.hasItem("EmptyListItemsResource"))
.body("paths.'/empty-list-items'", Matchers.hasKey("post"))
.body("paths.'/empty-list-items'.post.tags", Matchers.hasItem("EmptyListItemsResource"))
.body("paths.'/empty-list-items/{id}'", Matchers.hasKey("get"))
.body("paths.'/empty-list-items/{id}'", Matchers.hasKey("put"))
.body("paths.'/empty-list-items/{id}'", Matchers.hasKey("delete"))
Expand Down
Expand Up @@ -49,12 +49,16 @@ public void testOpenApiForGeneratedResources() {
.header("Content-Type", "application/json;charset=UTF-8")
.body("info.title", Matchers.equalTo("quarkus-hibernate-reactive-rest-data-panache-deployment API"))
.body("paths.'/collections'", Matchers.hasKey("get"))
.body("paths.'/collections'.get.tags", Matchers.hasItem("CollectionsResource"))
.body("paths.'/collections'", Matchers.hasKey("post"))
.body("paths.'/collections'.post.tags", Matchers.hasItem("CollectionsResource"))
.body("paths.'/collections/{id}'", Matchers.hasKey("get"))
.body("paths.'/collections/{id}'", Matchers.hasKey("put"))
.body("paths.'/collections/{id}'", Matchers.hasKey("delete"))
.body("paths.'/empty-list-items'", Matchers.hasKey("get"))
.body("paths.'/empty-list-items'.get.tags", Matchers.hasItem("EmptyListItemsResource"))
.body("paths.'/empty-list-items'", Matchers.hasKey("post"))
.body("paths.'/empty-list-items'.post.tags", Matchers.hasItem("EmptyListItemsResource"))
.body("paths.'/empty-list-items/{id}'", Matchers.hasKey("get"))
.body("paths.'/empty-list-items/{id}'", Matchers.hasKey("put"))
.body("paths.'/empty-list-items/{id}'", Matchers.hasKey("delete"))
Expand Down
Expand Up @@ -7,8 +7,11 @@
import javax.inject.Inject;
import javax.ws.rs.Path;

import org.apache.commons.lang3.StringUtils;
import org.jboss.logging.Logger;

import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Capability;
import io.quarkus.gizmo.ClassCreator;
import io.quarkus.gizmo.ClassOutput;
import io.quarkus.gizmo.FieldCreator;
Expand All @@ -29,6 +32,7 @@
class JaxRsResourceImplementor {

private static final Logger LOGGER = Logger.getLogger(JaxRsResourceImplementor.class);
private static final String OPENAPI_TAG_ANNOTATION = "org.eclipse.microprofile.openapi.annotations.tags.Tag";

private final List<MethodImplementor> methodImplementors;

Expand Down Expand Up @@ -58,24 +62,30 @@ class JaxRsResourceImplementor {
* }
* </pre>
*/
void implement(ClassOutput classOutput, ResourceMetadata resourceMetadata, ResourceProperties resourceProperties) {
void implement(ClassOutput classOutput, ResourceMetadata resourceMetadata, ResourceProperties resourceProperties,
Capabilities capabilities) {
String controllerClassName = resourceMetadata.getResourceInterface() + "JaxRs_" +
HashUtil.sha1(resourceMetadata.getResourceInterface());
LOGGER.tracef("Starting generation of '%s'", controllerClassName);
ClassCreator classCreator = ClassCreator.builder()
.classOutput(classOutput).className(controllerClassName)
.build();

implementClassAnnotations(classCreator, resourceProperties);
implementClassAnnotations(classCreator, resourceMetadata, resourceProperties, capabilities);
FieldDescriptor resourceField = implementResourceField(classCreator, resourceMetadata);
implementMethods(classCreator, resourceMetadata, resourceProperties, resourceField);

classCreator.close();
LOGGER.tracef("Completed generation of '%s'", controllerClassName);
}

private void implementClassAnnotations(ClassCreator classCreator, ResourceProperties resourceProperties) {
private void implementClassAnnotations(ClassCreator classCreator, ResourceMetadata resourceMetadata,
ResourceProperties resourceProperties, Capabilities capabilities) {
classCreator.addAnnotation(Path.class).addValue("value", resourceProperties.getPath());
if (capabilities.isPresent(Capability.SMALLRYE_OPENAPI)) {
String className = StringUtils.substringAfterLast(resourceMetadata.getResourceInterface(), ".");
classCreator.addAnnotation(OPENAPI_TAG_ANNOTATION).add("name", className);
}
}

private FieldDescriptor implementResourceField(ClassCreator classCreator, ResourceMetadata resourceMetadata) {
Expand Down
Expand Up @@ -84,7 +84,7 @@ void implementResources(CombinedIndexBuildItem index, List<RestDataResourceBuild

}
if (resourceProperties.isExposed()) {
jaxRsResourceImplementor.implement(classOutput, resourceMetadata, resourceProperties);
jaxRsResourceImplementor.implement(classOutput, resourceMetadata, resourceProperties, capabilities);
}
}
}
Expand Down

0 comments on commit 71924e6

Please sign in to comment.