spray Marshaller for futures not in implicit scope after upgrading to spray 1.2
Asked Answered
D

1

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...

Depredate answered 6/11, 2013 at 10:45 Comment(3)
Version spray version is that exactly?Pennon
Spray-Version is 1.2-RC2, but the same occurs with the current nightly build. Akka is 2.2.3 and Scala is 2.10.3.Depredate
Just added the case class definition and definition of the JsonFormat...Depredate
D
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

Depredate answered 6/11, 2013 at 18:46 Comment(3)
Is the behavior really different in 1.1-RC2 and 1.2-RC2?Pennon
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.Pennon
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!Galvano

© 2022 - 2024 — McMap. All rights reserved.