swagger-api / swagger-api/swagger-codegen

[Kotlin] Swagger with array of unknown item type cause NPE

Open
#8,895 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

When generating model types from a swagger.json which includes arrays of undefined item type, the plugin produces a null pointer exception:

java.lang.RuntimeException: Could not process model 'RoleContainerModel'.Please make sure that your schema is correct!
at io.swagger.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:370)
at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:743)
at io.swagger.codegen.plugin.CodeGenMojo.execute(CodeGenMojo.java:534)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
at org.codehaus.classworlds.Launcher.main(Launcher.java:47)
Caused by: java.lang.NullPointerException
at io.swagger.codegen.languages.KotlinClientCodegen.toModelName(KotlinClientCodegen.java:360)
at io.swagger.codegen.languages.KotlinClientCodegen.getSwaggerType(KotlinClientCodegen.java:398)
at io.swagger.codegen.DefaultCodegen.getTypeDeclaration(DefaultCodegen.java:1209)
at io.swagger.codegen.languages.KotlinClientCodegen.getTypeDeclaration(KotlinClientCodegen.java:420)
at io.swagger.codegen.languages.KotlinClientCodegen.getTypeDeclaration(KotlinClientCodegen.java:412)
at io.swagger.codegen.DefaultCodegen.fromProperty(DefaultCodegen.java:1770)
at io.swagger.codegen.DefaultCodegen.addVars(DefaultCodegen.java:3108)
at io.swagger.codegen.DefaultCodegen.addVars(DefaultCodegen.java:3079)
at io.swagger.codegen.DefaultCodegen.addVars(DefaultCodegen.java:3065)
at io.swagger.codegen.DefaultCodegen.fromModel(DefaultCodegen.java:1451)
at io.swagger.codegen.DefaultGenerator.processModels(DefaultGenerator.java:1011)
at io.swagger.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:365)
... 25 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.032 s
[INFO] Finished at: 2018-11-07T16:14:15-05:00
[INFO] Final Memory: 19M/35M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal io.swagger:swagger-codegen-maven-plugin:2.3.1:generate (default) on project olympus-user-management: Code generation failed. See above for the full exception. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[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/MojoExecutionException

Swagger-codegen version

Maven plugin 2.3.1

Swagger declaration file content or url
{
  "swagger": "2.0",
  "definitions": {
     "RoleContainerModel": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string"
        },
        "roles": {
          "type": "array",
          "uniqueItems": true
        },
        "defaultRoles": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      }
    }
  }
}

Note the JSON above is just a snip of a huge .json document, where said error occurs multiple times. I can provide the full JSON if needed, but I think this cleanly creates the issue

Command line used for generation

From POM.xml

<plugins>
            <!-- Codegen -->
            <plugin>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-codegen-maven-plugin</artifactId>
                <version>2.3.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/mini.json</inputSpec>
                            <language>kotlin</language>
                            <configOptions>
                                <sourceFolder>${project.basedir}/target/gen/main/kotlin</sourceFolder>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
  <!-- SNIP -->
Steps to reproduce

mvn clean generate-sources

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in io.swagger.codegen.languages.KotlinClientCodegen, especially toModelName and getSwaggerType, using the provided Swagger definition with an array whose items are omitted. Reproduce with mvn clean generate-sources and confirm generation completes without the NullPointerException for RoleContainerModel.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.