openapi-tools / swagger-maven-plugin

Maven plugin to activate the Swagger Core library to generate OpenAPI documentation.
MIT License
70 stars 45 forks source link

Getting a NullPointerException at java.util.TreeMap.putAll (TreeMap:312) #85

Open aortiztala opened 2 years ago

aortiztala commented 2 years ago

Describe the bug I'm getting a NullPointerException at java.util.TreeMap.putAll (TreeMap:312) after adding the plugin to my project.

[ERROR] Failed to execute goal io.openapitools.swagger:swagger-maven-plugin:2.1.5:generate (default) on project my-service: Execution default of goal io.openapitools.swagger:swagger-maven-plugin:2.1.5:generate failed.: NullPointerException -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal io.openapitools.swagger:swagger-maven-plugin:2.1.5:generate (default) on project my-service: Execution default of goal io.openapitools.swagger:swagger-maven-plugin:2.1.5:generate failed.
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:566)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: org.apache.maven.plugin.PluginExecutionException: Execution default of goal io.openapitools.swagger:swagger-maven-plugin:2.1.5:generate failed.
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:148)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:566)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: java.lang.NullPointerException
    at java.util.TreeMap.putAll (TreeMap.java:312)
    at java.util.TreeMap.<init> (TreeMap.java:185)
    at io.openapitools.swagger.OpenAPISorter.sortPaths (OpenAPISorter.java:50)
    at io.openapitools.swagger.OpenAPISorter.sort (OpenAPISorter.java:41)
    at io.openapitools.swagger.GenerateMojo.execute (GenerateMojo.java:127)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:566)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
[ERROR] 
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException

To Reproduce This is how I defined the plugin in my POM file:

<plugin>
  <groupId>io.openapitools.swagger</groupId>
  <artifactId>swagger-maven-plugin</artifactId>
  <version>2.1.5</version>
  <configuration>
      <resourcePackages>
          <resourcePackage>com.example.my.service.common</resourcePackage>
          <resourcePackage>com.example.my.service.controller</resourcePackage>
      </resourcePackages>
      <outputDirectory>${basedir}/target/</outputDirectory>
      <outputFilename>swagger</outputFilename>
      <outputFormats>JSON,YAML</outputFormats>
      <prettyPrint>true</prettyPrint>

      <!-- This information will be included in the output swagger file -->
      <swaggerConfig>
          <servers>
              <server>
                  <url>https://prod.my-server.com</url>
                  <description>Production Server</description>
              </server>
              <server>
                  <url>https://dev.my-server.com</url>
                  <description>Development Server</description>
              </server>
          </servers>
          <info>
              <title>My Service</title>
              <version>${project.version}</version>
              <description>
                  This service provides blah blah
              </description>
              <termsOfService>
                  TBD
              </termsOfService>
              <contact>
                  <email>someone@etc.com</email>
                  <name>Someone</name>
                  <url>mailto:someone@etc.com</url>
              </contact>
              <license>
                  <url>TBD</url>
                  <name>TBD</name>
              </license>
          </info>
      </swaggerConfig>

  </configuration>
  <executions>
      <execution>
          <goals>
              <goal>generate</goal>
          </goals>
      </execution>
  </executions>
</plugin>

Expected behavior I expected the project to build properly and generate the JSON and YAML files.

Additional context I'm experiencing this issue with versions from 2.1.3 to 2.1.6 (latest as of report date). It works with 2.1.2.

langecode commented 2 years ago

Thanks for reporting this. While this should certainly not happen it seems to be a result of no endpoints being discovered. Do you have any JAX-RS annotated resources in com.example.my.service.common or com.example.my.service.controller?

I'll make sure we do a NPE for this. The 2.1.3 introduced sorting of the resource paths (and other things) to ensure a deterministic output from running the generation and this indeed seems to be an issue from the sorting when no resource paths has been discovered.

aortiztala commented 2 years ago

No, I don't have any JAX-RS annotated resources. I thought maybe this plugin would work with @Api* annotated classes, now I know it doesn't.

langecode commented 2 years ago

It is intended to work with JAX-RS annotations - the same as Swagger basically. But shouldn't be throwing around NPE's anyway :)

JonasCir commented 1 year ago

Before hitting #96 I was running into this issue, but I cannot really tell at what point unfortunately. This definitely is affecting a couple of folks.


Edit: Could reproduce by just using it:

<plugin>
    <groupId>io.openapitools.swagger</groupId>
    <artifactId>swagger-maven-plugin</artifactId>
    <version>2.1.4</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

I'm using JAX-RS.

in-fke commented 5 months ago

It looks like resourcePackage is not recursive? That may be a common misconception and then nothing gets picked up, resulting in the NPE. I find it irritating that there is not activity on this project? I would glady prefer this plugin over smallrye-open-api plugin since this plugin picks up the Swagger Annotations. (I am yet to compare the YAML output to see which works better - especially for generating client code).

As for the NPE, my fix was to actually configure resourcePackage explicitly to the packages containing class files with annotations (assuming that resourcePackage does not recurse).

Caused by: java.lang.NullPointerException
    at java.util.TreeMap.putAll (TreeMap.java:312)
    at java.util.TreeMap.<init> (TreeMap.java:185)
    at io.openapitools.swagger.OpenAPISorter.sortPaths (OpenAPISorter.java:50)
    at io.openapitools.swagger.OpenAPISorter.sort (OpenAPISorter.java:41)
    at io.openapitools.swagger.GenerateMojo.execute (GenerateMojo.java:127)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:370)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:351)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:171)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:163)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.builder.multithreaded.MultiThreadedBuilder$1.call (MultiThreadedBuilder.java:210)
    at org.apache.maven.lifecycle.internal.builder.multithreaded.MultiThreadedBuilder$1.call (MultiThreadedBuilder.java:195)
    at java.util.concurrent.FutureTask.run (FutureTask.java:264)
    at java.util.concurrent.Executors$RunnableAdapter.call (Executors.java:515)
    at java.util.concurrent.FutureTask.run (FutureTask.java:264)
    at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1128)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:628)
    at java.lang.Thread.run (Thread.java:829)
luizleiteagrotis commented 4 months ago

With 2.1.2 version, the result is only this:

openapi: 3.0.1
info:
  title: My Title
  description: My Descrition
  version: 1.0.0

With 2.1.3 up to 2.1.5, the result is Nullpointer.

apeiris commented 1 month ago

I had similar issue at java.util.Map.size(), switching to 2.1.2 resolved the issue. Now I have swagger.json generated as annotated in the @OpenAPIDefinition { "openapi" : "3.0.1", "info" : { "title" : "OpenAS2 ", "description" : "OpenAS2 OpenAPI Implementeation", "version" : "1.0.0" }, "servers" : [ { "url" : "http://localhost:8080/api", "description" : "Test server", "variables" : { } } ] }