[ 
https://issues.apache.org/jira/browse/TOMEE-4117?focusedWorklogId=828571&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-828571
 ]

ASF GitHub Bot logged work on TOMEE-4117:
-----------------------------------------

                Author: ASF GitHub Bot
            Created on: 24/Nov/22 06:57
            Start Date: 24/Nov/22 06:57
    Worklog Time Spent: 10m 
      Work Description: rzo1 commented on code in PR #978:
URL: https://github.com/apache/tomee/pull/978#discussion_r1031113569


##########
tomee/tomee-microprofile/mp-common/src/main/java/org/apache/tomee/microprofile/openapi/MicroProfileOpenApiRegistration.java:
##########
@@ -16,76 +16,202 @@
  */
 package org.apache.tomee.microprofile.openapi;
 
+import io.smallrye.config.PropertiesConfigSource;
+import io.smallrye.config.SmallRyeConfig;
+import io.smallrye.config.SmallRyeConfigBuilder;
 import io.smallrye.openapi.api.OpenApiConfig;
 import io.smallrye.openapi.api.OpenApiConfigImpl;
 import io.smallrye.openapi.api.OpenApiDocument;
 import io.smallrye.openapi.runtime.OpenApiProcessor;
 import io.smallrye.openapi.runtime.OpenApiStaticFile;
 import io.smallrye.openapi.runtime.io.Format;
+import io.smallrye.openapi.runtime.scanner.FilteredIndexView;
 import jakarta.servlet.ServletContainerInitializer;
 import jakarta.servlet.ServletContext;
 import jakarta.servlet.ServletException;
 import jakarta.servlet.ServletRegistration;
+import jakarta.servlet.annotation.HandlesTypes;
+import jakarta.ws.rs.ApplicationPath;
+import jakarta.ws.rs.Path;
+import org.apache.openejb.loader.IO;
+import org.apache.openejb.util.LogCategory;
+import org.apache.openejb.util.Logger;
+import org.apache.tomee.microprofile.health.MicroProfileHealthChecksEndpoint;
 import org.eclipse.microprofile.config.Config;
 import org.eclipse.microprofile.config.ConfigProvider;
 import org.eclipse.microprofile.openapi.models.OpenAPI;
+import org.jboss.jandex.DotName;
+import org.jboss.jandex.Index;
+import org.jboss.jandex.Indexer;
 
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.Collection;
 import java.util.Objects;
 import java.util.Optional;
+import java.util.Properties;
 import java.util.Set;
 import java.util.stream.Stream;
 
+import static io.smallrye.openapi.runtime.OpenApiProcessor.getFilter;
+import static 
io.smallrye.openapi.runtime.OpenApiProcessor.modelFromAnnotations;
+import static io.smallrye.openapi.runtime.OpenApiProcessor.modelFromReader;
 import static io.smallrye.openapi.runtime.io.Format.JSON;
 import static io.smallrye.openapi.runtime.io.Format.YAML;
+import static java.lang.Thread.currentThread;
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static java.util.Optional.ofNullable;
 
 /**
- * Responsible for adding the filter into the chain and doing all other 
initialization
- * <p>
- * todo do we want to be so restrictive with the HandlesTypes annotation?
  *
- * @HandlesTypes({Path.class, WebServlet.class,
- * WebFilter.class
- * })
+ * This servlet container initializer is responsible for registering the 
OpenAPI servlet in each application so /openapi
+ * becomes available for applications to consume.
+ *
+ * It will first try to load static openapi files in format JSON, YAML and 
YML. If not found, it will try to generate the
+ * model on the fly for the endpoints.
+ *
  */
+// we'll get all the classes of the application and when creating the index 
we'll apply the filtering
+// we could have used only @Path from JAX RS because in the end, we only need 
those to generate the OpenAPI Model
+@HandlesTypes({
+    Path.class,
+    ApplicationPath.class
+})
 public class MicroProfileOpenApiRegistration implements 
ServletContainerInitializer {
 
+    private static final Logger LOGGER = 
Logger.getInstance(LogCategory.MICROPROFILE, 
MicroProfileOpenApiRegistration.class);
+
     @Override
     public void onStartup(final Set<Class<?>> classes, final ServletContext 
ctx) throws ServletException {
+
+        LOGGER.info("Registering OpenAPI servlet on /openapi for application " 
+ ctx.getContextPath());
         final ServletRegistration.Dynamic servletRegistration =
             ctx.addServlet("mp-openapi-servlet", 
MicroProfileOpenApiEndpoint.class);
         servletRegistration.addMapping("/openapi/*");
 
-        openApi(ctx);
+        // generate the OpenAPI document from static file or model reader or 
from annotations
+        final Optional<OpenAPI> openAPI = generateOpenAPI(classes, ctx);
+        openAPI.ifPresent(openApi -> setOpenApi(ctx, openAPI.get()));
+    }
+
+    /**
+     * Builds the OpenAPI file and copies it to the deployed application.
+     *
+     * @return The generated OpenAPI model wrapped into an Optional
+     */
+    private Optional<OpenAPI> generateOpenAPI(final Set<Class<?>> classes, 
final ServletContext servletContext) {
+        final OpenApiConfig openApiConfig = config(servletContext);
+        final Index index = index(classes, servletContext, openApiConfig);
+        final ClassLoader contextClassLoader = 
currentThread().getContextClassLoader();
+
+        Optional<OpenAPI> annotationModel = 
ofNullable(modelFromAnnotations(openApiConfig, contextClassLoader, index));
+        Optional<OpenAPI> readerModel = 
ofNullable(modelFromReader(openApiConfig, contextClassLoader));
+        Optional<OpenAPI> staticFileModel = 
openApiFromStaticFile(servletContext);
+
+        final OpenApiDocument document = OpenApiDocument.INSTANCE;
+        try {
+            document.reset();
+            document.config(openApiConfig);
+            annotationModel.ifPresent(document::modelFromAnnotations);
+            readerModel.ifPresent(document::modelFromReader);
+            staticFileModel.ifPresent(document::modelFromStaticFile);
+            document.filter(getFilter(openApiConfig, contextClassLoader));
+            document.initialize();
+            return Optional.ofNullable(document.get());
+
+        } finally {
+            document.reset();
+        }
+    }
+
+    /**
+     * Provides the Jandex index.
+     */
+    private static Index index(final Set<Class<?>> classes, final 
ServletContext servletContext, final OpenApiConfig config) {
+        FilteredIndexView filteredIndexView = new FilteredIndexView(null, 
config);
+        Indexer indexer = new Indexer();
+
+        // todo load all classes and build up an index with the content

Review Comment:
   still an todo?





Issue Time Tracking
-------------------

            Worklog Id:     (was: 828571)
    Remaining Estimate: 0h
            Time Spent: 10m

> MicroProfile OpenAPI not generating model
> -----------------------------------------
>
>                 Key: TOMEE-4117
>                 URL: https://issues.apache.org/jira/browse/TOMEE-4117
>             Project: TomEE
>          Issue Type: Bug
>    Affects Versions: 9.0.0.RC1
>            Reporter: Jean-Louis Monteiro
>            Priority: Major
>             Fix For: 9.0.0
>
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> MicroProfile implementation isn't able to generate the OpenAPI model based on 
> annotations. Looks like only the YAML and JSON files in the application are 
> being picked up.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to