Skip to content

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

Open
@shades198

Description

@shades198
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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions