swagger-play icon indicating copy to clipboard operation
swagger-play copied to clipboard

Play 2.4 ModelConverter throws NullPointerException

Open osbornk opened this issue 8 years ago • 2 comments

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.

osbornk avatar Jun 02 '16 22:06 osbornk

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 ::)

jonathanstowe avatar Nov 03 '16 15:11 jonathanstowe

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

UnconventionalMindset avatar Apr 25 '18 10:04 UnconventionalMindset