swagger-api / swagger-play

Apache License 2.0
330 stars 181 forks source link

Play 2.4 ModelConverter throws NullPointerException #85

Open osbornk opened 8 years ago

osbornk commented 8 years ago

I created a custom model converter in a Play 2.4 Scala application. This is just a simple case, but I just want to exclude any properties in a model that are of type String.

class CustomConverter extends ModelConverter {
  def resolveProperty(`type`: Type, context: ModelConverterContext, annotations: Array[Annotation], chain: util.Iterator[ModelConverter]): Property = {
    Option(Json.mapper.constructType(`type`)) match {
      case Some(jType) if jType.getRawClass.equals(classOf[String]) =>
        logger.info("AAA")
        null
      case _ => if (chain.hasNext) chain.next().resolveProperty(`type`, context, annotations, chain) else null
    }
  }

  def resolve(`type`: Type, context: ModelConverterContext, chain: util.Iterator[ModelConverter]): Model =
    if (chain.hasNext) chain.next.resolve(`type`, context, chain) else null
}

I was also unsure where to hook in the converter. So, I created a module for that. The documentation was very unclear on how to setup the converter. So, it is very possible that I am having a conflict between my module and the SwaggerPlugin.

class SetupSwaggerModule extends AbstractModule {
  def configure() = {
    ModelConverters.getInstance().addConverter(new CustomConverter)
  }
}

And it gives me this error. Notice my log statements "AAA". So, it has loaded my module and started parsing my models. And then in the middle, it throws a fatal exception.

...
[info] c.c.e.i.s.CustomConverter - [][] - AAA
[info] c.c.e.i.s.CustomConverter - [][] - AAA
[error] application - [][] -

! @70a23nnj9 - Internal server error, for (HEAD) [/health/check] ->

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$1)
  while locating play.modules.swagger.SwaggerPlugin

1 error]
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(DevServerStart.scala:165)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(DevServerStart.scala:121)
    at scala.Option.map(Option.scala:146)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1.apply(DevServerStart.scala:121)
    at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1.apply(DevServerStart.scala:119)
Caused by: com.google.inject.CreationException: Unable to create injector, see the following errors:

Any help or advice here would be greatly appreciated.

jonathanstowe commented 7 years ago

Strangely I get the identical message in a completely different circumstance, in my case it is where an application that runs fine using the "activator run" but fails like this when packaged as an rpm by activator and then installed.

I'd settle for some clue as to what it is trying to tell me to be honest ::)

UnconventionalMindset commented 6 years ago

Hi @osbornk is just a guess, but have you tried checking on null on .getRawClass?

https://github.com/swagger-api/swagger-scala-module/blob/develop/src/main/scala/io/swagger/scala/converter/SwaggerScalaModelConverter.scala#L25