Search code examples
jsonscalaakka-httpcirce

Akka Http / Circe Decode Result


I apologize if I'm missing something simple, but I'm trying to use Akka HTTP with Circe (using the akka-http-json Circe module). I'm trying to retrieve the results of a GET call in a ScalaTest which mixes in the ErrorAccumulatingCirceSupport trait. The call goes through successfully, but I'm unable to unmarshal the response...It's a pretty simple test, but I'm just not sure how to unmarshal the results into a list of domain objects, eg:

    Get("/path/to/getfoos").withHeaders(auth) ~> Route.seal(service.route) ~> check {
      import io.circe.generic.auto._
      status shouldEqual StatusCodes.OK
      contentType should ===(ContentTypes.`application/json`)
      val reports = responseAs[List[Foo]]
      reports.size shouldBe 1
   }

The error I'm getting is:

Could not unmarshal response to type 'scala.collection.immutable.List' for `responseAs` assertion: de.heikoseeberger.akkahttpcirce.ErrorAccumulatingCirceSupport$DecodingFailures: DecodingFailure at [0]: CNil

If anyone can point out what I'm dong wrong I would very much appreciate the help!

Thanks!


Solution

  • I'm not sure if this is the best way, but I was able to get my case classes unmarshalled using something like the following; if there's better way, please let me know!

          val entity = responseEntity
          val foos: List[Foo] = Unmarshal(entity).to[List[Foo]].futureValue
          foos(0) shouldBe expectedFoo
          foos.size shouldBe 1
    

    (Note that I had to also mixin the org.scalatest.concurrent.ScalaFutures trait)