Workaround for configuration parsing issue in Dropwizard 0.9 with Scala

If you are using Dropwizard 0.9 in Scala, you might have encountered the following error when starting your application:

Failed to parse configuration; Could not find creator property with name 'duration' (in class io.dropwizard.util.Duration)  

From the discussion on the dropwizard-dev Google group, the cause of this error is some incompatibility between Jackson 2.6.x (on which Dropwizard 0.9 depends) and the DefaultScalaModule from jackson-module-scala.

Typically, if you are using Dropwizard in Scala, you will have some bootstrapping code that looks like this:

bootstrap.getObjectMapper.registerModule(DefaultScalaModule)  

Registering the DefaultScalaModule for the ObjectMapper enables JSON serialization (and deserialization) for Scala-specific objects, such as case classes or Scala collections.

There seems to be a recent conflict between Jackson 2.6.x and one of the traits used to build the DefaultScalaModule, specifically the ParanamerAnnotationIntrospector. Lucky, you can easily write your own Scala module, without mixing in the problematic trait, and this does not affect the JSON parsing!

For example:

class FixedScalaModule  
  extends JacksonModule
    with IteratorModule
    with EnumerationModule
    with OptionModule
    with SeqModule
    with IterableModule
    with TupleModule
    with MapModule
    with SetModule
    with ScalaNumberDeserializersModule
    with FixedScalaAnnotationIntrospectorModule
    with UntypedObjectDeserializerModule
    with EitherModule
{
  override def getModuleName: String = "FixedScalaModule"
}

object FixedScalaModule extends FixedScalaModule

trait FixedScalaAnnotationIntrospectorModule extends JacksonModule {  
  this += { _.appendAnnotationIntrospector(ScalaAnnotationIntrospector) }
}

All you have to do then is use this new object in place of the DefaultScalaModule in your bootstrapping code:

bootstrap.getObjectMapper.registerModule(FixedScalaModule)  

Your application should start correctly now!

Credit goes to Filipe Cristóvão for his post on the dropwizard-dev Google group.

Update

Francois Laroche pointed out a much simpler way to achieve the same result, without having to define your own module:

bootstrap.getObjectMapper  
      .registerModule(DefaultScalaModule)
      .setAnnotationIntrospector(new AnnotationIntrospectorPair(ScalaAnnotationIntrospector, new JacksonAnnotationIntrospector()))