Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException when annotations are unavailable (not defined or private) #378

Open
randers-bit opened this issue Mar 24, 2023 · 5 comments

Comments

@randers-bit
Copy link

Hello, today i found that when in akka http all endpoints are set as private or no annotations are defined generateSwaggerJson will throw NullPointerException.
I now know that it was my mistake, but debugging NPE was time consuming and misleading.

I had code similar to this:

@Path( "user" )
case class UserRoute(){

    def apiRoute: Route = pathPrefix( "user" ) {
        concat(
            path( "1" ) {
                get(firstApi)
            },
            path( "2" ) {
                get(secondApi)
            },            
        )
    }
    
    @GET
    @Path( "1" )
    private def firstApi: Route = {
        complete("Ok")
    }
    
    @GET
    @Path( "2" )
    private def secondApi: Route = {
        complete("Ok")
    }
     
}

I think custom error message like "No annotations found" would be better for future developers.

@pjfanning
Copy link
Collaborator

Please provide a full reproducible use case. Also note that I am abandoning Akka in favour of Pekko.

@pjfanning
Copy link
Collaborator

Even provide the stack trace. That partial example is not very useful

@randers-bit
Copy link
Author

randers-bit commented Mar 24, 2023

I was building on different machine reproduction without a similar result.
I copied code which i used and then i found something which caused NPE.
In my swagger generator class i had definition for removing unwanted generated classes.

override val unwantedDefinitions: Seq[ String ] = Seq( "Function1", "Function1RequestContextFutureRouteResult" )

On plain project after i added this and had definitions of endpoints as private then NPE is thrown.

24/03 19:12:18.946 -> [ERROR][SwaggerHttpService] Issue with creating swagger.json
java.lang.NullPointerException: null
	at com.github.swagger.akka.SwaggerGenerator.filteredSwagger(SwaggerHttpService.scala:119)
	at com.github.swagger.akka.SwaggerGenerator.filteredSwagger$(SwaggerHttpService.scala:116)
	at akka.swagger.example.SwaggerRoute.filteredSwagger(SwaggerRoute.scala:8)
	at com.github.swagger.akka.SwaggerGenerator.generateSwaggerJson(SwaggerHttpService.scala:88)
	at com.github.swagger.akka.SwaggerGenerator.generateSwaggerJson$(SwaggerHttpService.scala:86)
	at akka.swagger.example.SwaggerRoute.generateSwaggerJson(SwaggerRoute.scala:8)
	at akka.swagger.example.SwaggerRoute.$anonfun$apiRoute$3(SwaggerRoute.scala:35)
	at akka.http.scaladsl.server.directives.RouteDirectives.$anonfun$complete$1(RouteDirectives.scala:51)
	at akka.http.scaladsl.server.StandardRoute$$anon$1.apply(StandardRoute.scala:19)
	at akka.http.scaladsl.server.StandardRoute$$anon$1.apply(StandardRoute.scala:19)
	at akka.http.scaladsl.server.directives.ExecutionDirectives.$anonfun$handleExceptions$2(ExecutionDirectives.scala:32)
	at akka.http.scaladsl.server.directives.BasicDirectives.$anonfun$mapRequestContext$2(BasicDirectives.scala:45)
	at akka.http.scaladsl.server.directives.BasicDirectives.$anonfun$textract$2(BasicDirectives.scala:161)
	at akka.http.scaladsl.server.RouteConcatenation$RouteWithConcatenation.$anonfun$$tilde$2(RouteConcatenation.scala:47)
	at akka.http.scaladsl.util.FastFuture$.strictTransform$1(FastFuture.scala:40)
	at akka.http.scaladsl.util.FastFuture$.transformWith$extension(FastFuture.scala:44)
	at akka.http.scaladsl.util.FastFuture$.flatMap$extension(FastFuture.scala:25)
	at akka.http.scaladsl.server.RouteConcatenation$RouteWithConcatenation.$anonfun$$tilde$1(RouteConcatenation.scala:44)
	at akka.http.scaladsl.server.directives.BasicDirectives.$anonfun$mapRouteResultWith$2(BasicDirectives.scala:74)
	at akka.http.scaladsl.server.directives.BasicDirectives.$anonfun$textract$2(BasicDirectives.scala:161)
	at akka.http.scaladsl.server.directives.ExecutionDirectives.$anonfun$handleExceptions$2(ExecutionDirectives.scala:32)
	at akka.http.scaladsl.server.Route$.$anonfun$createAsyncHandler$1(Route.scala:110)
	at akka.stream.impl.fusing.MapAsyncUnordered$$anon$31.onPush(Ops.scala:1430)
	at akka.stream.impl.fusing.GraphInterpreter.processPush(GraphInterpreter.scala:542)
	at akka.stream.impl.fusing.GraphInterpreter.processEvent(GraphInterpreter.scala:496)
	at akka.stream.impl.fusing.GraphInterpreter.execute(GraphInterpreter.scala:390)
	at akka.stream.impl.fusing.GraphInterpreterShell.runBatch(ActorGraphInterpreter.scala:650)
	at akka.stream.impl.fusing.GraphInterpreterShell$AsyncInput.execute(ActorGraphInterpreter.scala:521)
	at akka.stream.impl.fusing.GraphInterpreterShell.processEvent(ActorGraphInterpreter.scala:625)
	at akka.stream.impl.fusing.ActorGraphInterpreter.akka$stream$impl$fusing$ActorGraphInterpreter$$processEvent(ActorGraphInterpreter.scala:800)
	at akka.stream.impl.fusing.ActorGraphInterpreter$$anonfun$receive$1.applyOrElse(ActorGraphInterpreter.scala:818)
	at akka.actor.Actor.aroundReceive(Actor.scala:537)
	at akka.actor.Actor.aroundReceive$(Actor.scala:535)
	at akka.stream.impl.fusing.ActorGraphInterpreter.aroundReceive(ActorGraphInterpreter.scala:716)
	at akka.actor.ActorCell.receiveMessage(ActorCell.scala:579)
	at akka.actor.ActorCell.invoke(ActorCell.scala:547)
	at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:270)
	at akka.dispatch.Mailbox.run(Mailbox.scala:231)
	at akka.dispatch.Mailbox.exec(Mailbox.scala:243)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183)

When i changed firstApi to public from private then swagger.json is generated properly.

{
  "openapi" : "3.0.1",
  "info" : {
    "title" : "",
    "description" : "",
    "termsOfService" : "",
    "version" : ""
  },
  "servers" : [ {
    "url" : "http://localhost"
  } ],
  "security" : [ ],
  "paths" : {
    "/user/1" : {
      "get" : {
        "operationId" : "firstApi",
        "responses" : {
          "default" : {
            "description" : "default response",
            "content" : {
              "*/*" : {
                "schema" : {
                  "$ref" : "#/components/schemas/Function1RequestContextFutureRouteResult"
                }
              }
            }
          }
        }
      }
    }
  },
  "components" : {
    "schemas" : { }
  }
}

When both endpoints are private and unwantedDefinitions are removed then it generates properly.

{
  "openapi" : "3.0.1",
  "info" : {
    "title" : "",
    "description" : "",
    "termsOfService" : "",
    "version" : ""
  },
  "servers" : [ {
    "url" : "http://localhost"
  } ],
  "security" : [ ]
}

@randers-bit
Copy link
Author

I'm writing this because this could be helpfull for pekko too maybe.

@randers-bit
Copy link
Author

I did reproduction here https://github.com/Randerspl/akka-swagger-npe.
Just run it and enter localhost:9000/swagger and NPE should be thrown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants