swagger-api / swagger-api/swagger-codegen-generators

mvn swagger-codegen:generate throws out of memory error for query parameter with allOff in OpenAPI spec file.

Open
#1,352 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
299
Forks
439
PR merge metrics
No merged PRs in 30d

Description

mvn swagger-codegen:generate throws out of memory error for the following OpenAPI spec file.

{
  "x-generator": "NSwag v14.3.0.0 (NJsonSchema v11.2.0.0 (Newtonsoft.Json v13.0.0.0))",
  "openapi": "3.0.0",
  "info": {
    "title": "test API",
    "version": "1.0.0"
  },
  "paths": {
    "/data": {
      "get": {
        "summary": "Retrieves data",
        "operationId": "GetData",
        "parameters": [
          {
            "name": "dataType",
            "in": "query",
            "required": true,
            "schema": {
              "allOf": [
                {
                  "$ref": "#/components/schemas/DataType"
                }
              ]
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "DataType": {
        "type": "string",
        "description": "",
        "x-enumNames": [
          "String",
          "Number"
        ],
        "enum": [
          "String",
          "Number"
        ]
      }
    }
  }
}

Logs

[ERROR] Java heap space -> [Help 1]
java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf (Arrays.java:3689)
    at java.util.ArrayList.grow (ArrayList.java:238)
    at java.util.ArrayList.grow (ArrayList.java:243)
    at java.util.ArrayList.add (ArrayList.java:486)
    at java.util.ArrayList.add (ArrayList.java:499)
    at io.swagger.codegen.v3.generators.DefaultCodegenConfig.fixUpParentAndInterfaces (DefaultCodegenConfig.java:303)
    at io.swagger.codegen.v3.generators.java.AbstractJavaCodegen.fixUpParentAndInterfaces (AbstractJavaCodegen.java:1061)
    at io.swagger.codegen.v3.generators.DefaultCodegenConfig.postProcessAllCodegenModels (DefaultCodegenConfig.java:274)
    at io.swagger.codegen.v3.generators.DefaultCodegenConfig.postProcessAllModels (DefaultCodegenConfig.java:266)
    at io.swagger.codegen.v3.generators.java.JavaClientCodegen.postProcessAllModels (JavaClientCodegen.java:481)
    at io.swagger.codegen.v3.DefaultGenerator.generateModels (DefaultGenerator.java:416)
    at io.swagger.codegen.v3.DefaultGenerator.generate (DefaultGenerator.java:852)
    at io.swagger.codegen.v3.maven.plugin.CodeGenMojo.execute_ (CodeGenMojo.java:567)
    at io.swagger.codegen.v3.maven.plugin.CodeGenMojo.execute (CodeGenMojo.java:326)
    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)
[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/OutOfMemoryError
Image

It seems to be happening because of the dataType query parameter that extends from DataType schema. The code builds a ComposedSchema (don't remember exact name seen during debug) identified by dataType with parent DataType. Because the names are similar without case sensitivity, it processes the same object again when it loops on the parent essentially creating an infinite loop.

If the dataType name is changed to say dataType1 it completes. It does create another model class DataType1.

The spec was generated by NSwag. I have no control over why it chose allOff for that query parameter when something simpler could have been used I suppose. Anyway the openapi-generator seems to be working fine with this - it doesn't create the DataType1 either.

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 with the supplied OpenAPI document and reproduce it with mvn swagger-codegen:generate. Inspect DefaultCodegenConfig.fixUpParentAndInterfaces and the Java generator call chain shown in the stack trace, focusing on the case-insensitive dataType/DataType parent relationship. Done means generation completes without exhausting the heap and does not repeatedly process the same model.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.