swagger-api / swagger-play

Apache License 2.0
331 stars 180 forks source link

Play 2.6 error upon construction due to NullPointerException caused by routes #185

Open mpgirro opened 6 years ago

mpgirro commented 6 years ago

I am experiencing a NullPointerException upon startup in Play 2.6 with swagger-play2 1.6.0. The complete error message and strack trace is:

Oops, cannot start the server.
com.google.inject.CreationException: Unable to create injector, see the following errors:

1) Error injecting constructor, java.lang.NullPointerException
  at play.modules.swagger.SwaggerPluginImpl.<init>(SwaggerPlugin.scala:35)
  while locating play.modules.swagger.SwaggerPluginImpl
  at play.modules.swagger.SwaggerModule.bindings(SwaggerModule.scala:11):
Binding(interface play.modules.swagger.SwaggerPlugin to ConstructionTarget(class play.modules.swagger.SwaggerPluginImpl) eagerly) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)
  while locating play.modules.swagger.SwaggerPlugin

1 error
    at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:543)
    at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:186)
    at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:109)
    at com.google.inject.Guice.createInjector(Guice.java:87)
    at com.google.inject.Guice.createInjector(Guice.java:78)
    at play.api.inject.guice.GuiceBuilder.injector(GuiceInjectorBuilder.scala:185)
    at play.api.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.scala:137)
    at play.api.inject.guice.GuiceApplicationLoader.load(GuiceApplicationLoader.scala:21)
    at play.core.server.ProdServerStart$.start(ProdServerStart.scala:51)
    at play.core.server.ProdServerStart$.main(ProdServerStart.scala:25)
    at play.core.server.ProdServerStart.main(ProdServerStart.scala)
Caused by: java.lang.NullPointerException
    at java.io.Reader.<init>(Reader.java:78)
    at java.io.InputStreamReader.<init>(InputStreamReader.java:129)
    at scala.io.BufferedSource.reader(BufferedSource.scala:22)
    at scala.io.BufferedSource.bufferedReader(BufferedSource.scala:23)
    at scala.io.BufferedSource.charReader$lzycompute(BufferedSource.scala:33)
    at scala.io.BufferedSource.charReader(BufferedSource.scala:31)
    at scala.io.BufferedSource.scala$io$BufferedSource$$decachedReader(BufferedSource.scala:60)
    at scala.io.BufferedSource.mkString(BufferedSource.scala:89)
    at play.modules.swagger.SwaggerPluginImpl.play$modules$swagger$SwaggerPluginImpl$$parseRoutesHelper$1(SwaggerPlugin.scala:122)
    at play.modules.swagger.SwaggerPluginImpl$$anonfun$1.applyOrElse(SwaggerPlugin.scala:131)
    at play.modules.swagger.SwaggerPluginImpl$$anonfun$1.applyOrElse(SwaggerPlugin.scala:124)
    at scala.collection.immutable.List.collect(List.scala:313)
    at play.modules.swagger.SwaggerPluginImpl.play$modules$swagger$SwaggerPluginImpl$$parseRoutesHelper$1(SwaggerPlugin.scala:124)
    at play.modules.swagger.SwaggerPluginImpl.parseRoutes(SwaggerPlugin.scala:137)
    at play.modules.swagger.SwaggerPluginImpl.<init>(SwaggerPlugin.scala:106)
    at play.modules.swagger.SwaggerPluginImpl$$FastClassByGuice$$de7219b8.newInstance(<generated>)
    at com.google.inject.internal.DefaultConstructionProxyFactory$FastClassProxy.newInstance(DefaultConstructionProxyFactory.java:89)
    at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:114)
    at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:91)
    at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:306)
    at com.google.inject.internal.FactoryProxy.get(FactoryProxy.java:62)
    at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
    at com.google.inject.internal.SingletonScope$1.get(SingletonScope.java:148)
    at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:39)
    at com.google.inject.internal.InternalInjectorCreator.loadEagerSingletons(InternalInjectorCreator.java:211)
    at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:182)
    ... 9 more

The cause seems to be related to the routes file. After studying SwaggerPlugin.scala, it appears to me that swagger-play2 does not read the content of the default config/routes file, and therefore experiences this Nullpointer.

I'm merely using the standard setup for this library:

GET   /swagger.json     controllers.ApiHelpController.getResources
GET   /docs/            controllers.Assets.at(path="/public/swagger",file="index.html")
GET   /docs/*file       controllers.Assets.at(path="/public/swagger",file)

It this a bug or does anybody have an explication why this happens?

romerobrjp commented 5 years ago

I'm facing exactly the same problem. Any progress?

[error] application - 

! @7ao995naj - Internal server error, for (GET) [/assets/docs] ->

play.api.UnexpectedException: Unexpected exception[CreationException: Unable to create injector, see the following errors:

1) Error injecting constructor, java.lang.NullPointerException
  at play.modules.swagger.SwaggerPluginImpl.<init>(SwaggerPlugin.scala:35)
  while locating play.modules.swagger.SwaggerPluginImpl
  at play.modules.swagger.SwaggerModule.bindings(SwaggerModule.scala:11):
Binding(interface play.modules.swagger.SwaggerPlugin to ConstructionTarget(class play.modules.swagger.SwaggerPluginImpl) eagerly) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$4)
  while locating play.modules.swagger.SwaggerPlugin

1 error]
    at play.core.server.DevServerStart$$anon$1.reload(DevServerStart.scala:186)
    at play.core.server.DevServerStart$$anon$1.get(DevServerStart.scala:124)
    at play.core.server.AkkaHttpServer.handleRequest(AkkaHttpServer.scala:241)
    at play.core.server.AkkaHttpServer.$anonfun$createServerBinding$1(AkkaHttpServer.scala:138)
    at akka.stream.impl.fusing.MapAsyncUnordered$$anon$31.onPush(Ops.scala:1318)
    at akka.stream.impl.fusing.GraphInterpreter.processPush(GraphInterpreter.scala:519)
    at akka.stream.impl.fusing.GraphInterpreter.processEvent(GraphInterpreter.scala:482)
    at akka.stream.impl.fusing.GraphInterpreter.execute(GraphInterpreter.scala:378)
    at akka.stream.impl.fusing.GraphInterpreterShell.runBatch(ActorGraphInterpreter.scala:588)
    at akka.stream.impl.fusing.GraphInterpreterShell$AsyncInput.execute(ActorGraphInterpreter.scala:472)
Caused by: com.google.inject.CreationException: Unable to create injector, see the following errors:

1) Error injecting constructor, java.lang.NullPointerException
  at play.modules.swagger.SwaggerPluginImpl.<init>(SwaggerPlugin.scala:35)
  while locating play.modules.swagger.SwaggerPluginImpl
  at play.modules.swagger.SwaggerModule.bindings(SwaggerModule.scala:11):
Binding(interface play.modules.swagger.SwaggerPlugin to ConstructionTarget(class play.modules.swagger.SwaggerPluginImpl) eagerly) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$4)
  while locating play.modules.swagger.SwaggerPlugin

1 error
    at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:470)
    at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:184)
    at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:110)
    at com.google.inject.Guice.createInjector(Guice.java:99)
    at com.google.inject.Guice.createInjector(Guice.java:84)
    at play.api.inject.guice.GuiceBuilder.injector(GuiceInjectorBuilder.scala:185)
    at play.api.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.scala:137)
    at play.api.inject.guice.GuiceApplicationLoader.load(GuiceApplicationLoader.scala:21)
    at play.core.server.DevServerStart$$anon$1.$anonfun$reload$3(DevServerStart.scala:174)
    at play.utils.Threads$.withContextClassLoader(Threads.scala:21)
Caused by: java.lang.NullPointerException: null
    at java.io.Reader.<init>(Reader.java:78)
    at java.io.InputStreamReader.<init>(InputStreamReader.java:129)
    at scala.io.BufferedSource.reader(BufferedSource.scala:26)
    at scala.io.BufferedSource.bufferedReader(BufferedSource.scala:27)
    at scala.io.BufferedSource.charReader$lzycompute(BufferedSource.scala:37)
    at scala.io.BufferedSource.charReader(BufferedSource.scala:35)
    at scala.io.BufferedSource.scala$io$BufferedSource$$decachedReader(BufferedSource.scala:64)
    at scala.io.BufferedSource.mkString(BufferedSource.scala:93)
    at play.modules.swagger.SwaggerPluginImpl.play$modules$swagger$SwaggerPluginImpl$$parseRoutesHelper$1(SwaggerPlugin.scala:122)
    at play.modules.swagger.SwaggerPluginImpl$$anonfun$1.applyOrElse(SwaggerPlugin.scala:131)
[error] application - 

My conf/routes:

# Include API v1 Routes
-> /api/v1  v1.Routes

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.versioned(path="/public", file: Asset)
GET     /docs                       controllers.ApiHelpController.getResources

I renamed the Swagger 'dist' folder to 'docs' and put it into my project 'public' folder.

Added play.modules.enabled += "play.modules.swagger.SwaggerModule" into my application.conf

Added "io.swagger" %% "swagger-play2" % "1.6.0", into my build.sbt

gaeljw commented 5 years ago

We have this same error sometimes (I guess when we change the routes file..).

mgosk commented 5 years ago

@romeromfm @gaeljw Can you publish a complete project with this error on github. I will look into it and try to prepare new version of swagger-play.

mpgirro commented 5 years ago

@mgosk I have created an example project with this error here https://github.com/mpgirro/play2.6-swagger-issue

The project structure builds on the official playframework/play-scala-rest-api-example (2.6.x branch) and applies the steps to add swagger-play as in my original description at the top of this issue.

Thanks for looking into it!