9

After updating to spray 1.2 I got a problem regarding my JSON-Marshallers that worked perfectly with 1.1. Doing the following inside a HttpService

trait TestHttpService extends HttpService with SprayJsonSupport with DefaultJsonProtocol{ self : ActorLogging =>
    case class Test(hallo: String, test: String)
    implicit val storyJsonFormat = jsonFormat2(Test.apply)

    def test(implicit m : Marshaller[Future[Test]]) = 17
    def hallo = test 
}

leads to the following error:

could not find implicit value for parameter marshaller:
spray.httpx.marshalling.Marshaller[scala.concurrent.Future[amanuensis.story.Story]]

When I just remove the future everything works well:

trait TestHttpService extends HttpService with SprayJsonSupport with DefaultJsonProtocol { self : ActorLogging =>
    case class Test(hallo: String, test: String)
    implicit val storyJsonFormat = jsonFormat2(Test.apply)

    def test(implicit m : Marshaller[Test]) = 17
    def hallo = test

}

So the Marshaller for Story itself seems to be in implicit-scope. I am confused now since I never had to do anything else to be able to marshal futures before.

I really would appreciate a hint, what I am doing wrong here...

jens
  • 443
  • 2
  • 12

1 Answers1

18

Ok, solution is easy but quite hard to find since there is no error message pointing to it:

You need to specify an implicit execution context in scope to be able to use the also implicit Marshaller[Future[...]]. In my case:

trait TestHttpService extends HttpService with SprayJsonSupport with DefaultJsonProtocol{ self : ActorLogging =>
    //the following line was missing
    implicit def executionContext = actorRefFactory.dispatcher
    //
    case class Test(hallo: String, test: String)
    implicit val storyJsonFormat = jsonFormat2(Test.apply)

    def test(implicit m : Marshaller[Future[Test]]) = 17
    def hallo = test 
}

This was not the case with spray 1.1, Scala 2.10.0 and akka 2.1

jens
  • 443
  • 2
  • 12
  • Is the behavior really different in 1.1-RC2 and 1.2-RC2? – jrudolph Nov 07 '13 at 17:06
  • I just tried it, and it's the same with 1.1-RC2 and 1.2-RC2. However, the rest of your analysis is spot on. And yes, it's really a pain that the scala compiler doesn't give better error messages for nested implicits but there's nothing we can currently do about that. – jrudolph Nov 07 '13 at 17:10
  • 1
    Oh god thanks. I killed 7 hours to figure that out - the compiler was complaining that I was passing a Future[String] instead of ToResponseMarshallable, and no hints of a missing implicit import. Importing context.dispatcher helped! – Sergey Sep 19 '14 at 12:55