Search code examples
scalaunmarshallingimplicitakka-httpspray-json

Could not find implicit value for parameter um


I had an Main app with the following structure:

implicit val orgs: RootJsonFormat[GHOrg]                = jsonFormat3(GHOrg)
implicit val users: RootJsonFormat[GHUser]              = jsonFormat2(GHUser)
implicit val repos: RootJsonFormat[GHRepo]              = jsonFormat3(GHRepo)
case class GHUser(login: String, contributions: Option[Int] = None)
case class GHRepo(name: String, owner: GHUser, contributors_url: String)
case class GHOrg(name: String, repos_url: String, members_url: String)

At some point I do

Unmarshal(e).to[List[GHRepo]]

And everything works, however, I have been doing a little bit of code cleaning and I have moved the three case classes above to a different package, and now I am getting title's error. I have tried this suggestion (spray-json error: could not find implicit value for parameter um) but does not work.

Here is how the current code (Not compiling):

import akka.actor.ActorSystem
import akka.http.javadsl.model.StatusCodes
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import akka.http.scaladsl.model._
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.ActorMaterializer
import com.elbauldelprogramador.domain.{GHOrg, GHRepo, GHUser}
import org.slf4j.LoggerFactory
import spray.json.DefaultJsonProtocol._
import spray.json.RootJsonFormat

object Main extends App {

  implicit val system: ActorSystem                        = ActorSystem()
  implicit val materializer: ActorMaterializer            = ActorMaterializer()
  implicit val executionContext: ExecutionContextExecutor = system.dispatcher
  implicit val orgs: RootJsonFormat[GHOrg]                = jsonFormat3(GHOrg)
  implicit val users: RootJsonFormat[GHUser]              = jsonFormat2(GHUser)
  implicit val repos: RootJsonFormat[GHRepo]              = jsonFormat3(GHRepo)
// ....
Unmarshal(e).to[List[GHRepo]] // Error
// ....
}

Here is the full error:

could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.Unmarshaller[akka.http.scaladsl.model.ResponseEntity,List[com.elbauldelprogramador.domain.GHUser]] [error] val users = Unmarshal(e).to[List[GHUser]] [error] ^


Solution

  • I finally was able to solve it reading spray's docs:

    Basically, I created a separated file for each case class and one object called MyJsonProtocol with the following code:

    object MyJsonProtocol extends DefaultJsonProtocol {
      implicit val users = jsonFormat2(GHUser)
      // ...
    }
    

    Then in my main app I just import both things:

    import com.elbauldelprogramador.api.MyJsonProtocol._
    import com.elbauldelprogramador.domain.{GHOrg, GHRepo, GHUser}