OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Java] java-vertx generated code fails to run with nullpointerexception @ SwaggerRouter.getBasePath(SwaggerRouter.java:129)

Open
#7,732 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug Server: Java
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

java-vertx generated code fails to run with nullpointerexception @ SwaggerRouter.getBasePath(SwaggerRouter.java:129)

openapi-generator version

openapi-generator-cli version: 4.1.2

Platform config:
OS: RHEL 7.8
Java: 11

OpenAPI declaration file content or url
{
  "openapi" : "3.0.1",
  "info" : {
    "description" : "Transaction API",
    "license" : {
      "name" : "Apache 2.0",
      "url" : "http://www.apache.org/licenses/LICENSE-2.0.html"
    },
    "termsOfService" : "http://yettoformed.com/",
    "title" : "transactions",
    "version" : "1.0.0"
  },
  "servers" : [ {
    "url" : "https://localhost/v2"
  }, {
    "url" : "http://localhost/v2"
  } ],
  "tags" : [ {
    "description" : "Everything about your transactions",
    "name" : "transaction"
  } ],
  "paths" : {
    "/transaction" : {
      "post" : {
        "operationId" : "addTransaction",
        "requestBody" : {
          "content" : {
            "application/json" : {
              "schema" : {
                "$ref" : "#/components/schemas/Transaction"
              }
            }
          },
          "description" : "Transaction object that needs to be added",
          "required" : true
        },
        "responses" : {
          "405" : {
            "content" : { },
            "description" : "Invalid input"
          }
        },
        "summary" : "Add a new transaction",
        "tags" : [ "transaction" ],
        "x-codegen-request-body-name" : "body",
        "x-contentType" : "application/json",
        "x-accepts" : "application/json",
        "x-serviceid" : "addTransaction",
        "x-serviceid-varname" : "ADDTRANSACTION_SERVICE_ID"
      }
    },
    "/compensatingTxn" : {
      "post" : {
        "operationId" : "compensatingTxn",
        "requestBody" : {
          "content" : {
            "application/json" : {
              "schema" : {
                "$ref" : "#/components/schemas/Transaction"
              }
            }
          },
          "description" : "Transaction object that needs to be added to the store",
          "required" : true
        },
        "responses" : {
          "400" : {
            "content" : { },
            "description" : "Invalid ID supplied"
          },
          "404" : {
            "content" : { },
            "description" : "Transaction not found"
          },
          "405" : {
            "content" : { },
            "description" : "Validation exception"
          }
        },
        "summary" : "Compensating API ",
        "tags" : [ "transaction" ],
        "x-codegen-request-body-name" : "body",
        "x-contentType" : "application/json",
        "x-accepts" : "application/json",
        "x-serviceid" : "compensatingTxn",
        "x-serviceid-varname" : "COMPENSATINGTXN_SERVICE_ID"
      }
    },
    "/transaction/" : {
      "get" : {
        "description" : "Get a transaction based on a ID",
        "operationId" : "findTransactionByTxnId",
        "parameters" : [ {
          "description" : "Transaction id you would like to fetch",
          "in" : "query",
          "name" : "txnId",
          "required" : true,
          "schema" : {
            "type" : "string"
          }
        } ],
        "responses" : {
          "200" : {
            "content" : {
              "application/json" : {
                "schema" : {
                  "items" : {
                    "$ref" : "#/components/schemas/Transaction"
                  },
                  "type" : "array"
                }
              }
            },
            "description" : "successful operation"
          },
          "400" : {
            "content" : { },
            "description" : "Invalid status value"
          }
        },
        "summary" : "Get a transaction based on a ID",
        "tags" : [ "transaction" ],
        "x-accepts" : "application/json",
        "x-serviceid" : "findTransactionByTxnId",
        "x-serviceid-varname" : "FINDTRANSACTIONBYTXNID_SERVICE_ID"
      }
    }
  },
  "components" : {
    "schemas" : {
      "Transaction" : {
        "example" : {
          "accountId" : "accountId",
          "amount" : 0,
          "typeOfTxn" : "credit",
          "id" : "id"
        },
        "properties" : {
          "id" : {
            "type" : "string"
          },
          "amount" : {
            "format" : "int64",
            "type" : "integer"
          },
          "accountId" : {
            "type" : "string"
          },
          "typeOfTxn" : {
            "description" : "Type of transaction",
            "enum" : [ "credit", "debit" ],
            "type" : "string"
          }
        },
        "type" : "object"
      }
    }
  }
}
Generation Details

java -jar openapi-generator-cli.jar generate -g java-vertx -i ~/Documents/Work/swaggers\ for\ testing/Cards.yaml -o ~/GeneratedCode/openapi-java-vertx

Given swagger is correct without any issues. It opens jst fine on editor.swagger.io

Code was successfully generated. But had two issues

  1. In pom.xml vertx-swagger-router.version was empty, no provided
  2. After fixing above version, code failed to start with a nullpointerexception.

Stacktrace for pt.2

java.lang.NullPointerException
	at com.github.phiz71.vertx.swagger.router.SwaggerRouter.getBasePath(SwaggerRouter.java:129)
	at com.github.phiz71.vertx.swagger.router.SwaggerRouter.swaggerRouter(SwaggerRouter.java:75)
	at com.github.phiz71.vertx.swagger.router.SwaggerRouter.swaggerRouter(SwaggerRouter.java:70)
	at org.openapitools.server.api.MainApiVerticle.lambda$start$1(MainApiVerticle.java:50)
	at io.vertx.core.impl.FutureImpl.checkCallHandler(FutureImpl.java:188)
	at io.vertx.core.impl.FutureImpl.setHandler(FutureImpl.java:100)
	at io.vertx.core.impl.ContextImpl.lambda$null$0(ContextImpl.java:287)
	at io.vertx.core.impl.ContextImpl.lambda$wrapTask$2(ContextImpl.java:337)
	at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
	at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:403)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:445)
	at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858)
	at java.base/java.lang.Thread.run(Thread.java:834)

Inside MainApiVerticle.java are below line

FileSystem vertxFileSystem = vertx.fileSystem();
        vertxFileSystem.readFile("openapi.json", readFile -> {
            if (readFile.succeeded()) {
                Swagger swagger = new SwaggerParser().parse(readFile.result().toString(Charset.forName("utf-8")));
                Router swaggerRouter = SwaggerRouter.swaggerRouter(router, swagger, vertx.eventBus(), new OperationIdServiceIdResolver());

readFile() operation succeeds, however in next line value of swagger comes out as null. openapi.json was indeed present inside src/main/resources.

Also digging down some more I found out these lines from io.swagger.parser.Swagger20Parser

 // must have swagger node set
JsonNode swaggerNode = rootNode.get("swagger");
if (swaggerNode == null) {
    return null;
}

Now since generated openapi.json has "openapi": "3.0.1" at the start, why is the code expecting root node as swagger?
Please help.

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 generated MainApiVerticle.java, especially the readFile callback and SwaggerParser call, then inspect the referenced Swagger20Parser implementation and the generated openapi.json. Reproduce the Java 11 startup failure with openapi-generator-cli 4.1.2 and determine why the parsed document is null. Done means the generated java-vertx application starts successfully with this OpenAPI 3.0.1 input and the regression is covered by an appropriate test.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.