Http4s EntityDecoder not being auto derived for simple case class

2.6k Views Asked by At

I am getting this error:

Cannot decode into a value of type com.blah.rest.model.UserProfile, 
because no EntityDecoder[cats.effect.IO, com.blah.rest.model.UserProfile] 
instance could be found.

for the following case class:

case class UserProfile(id: Option[Int], firstName: String, lastName: String)

Encountered the error on POST code:

case req @ POST -> Root / "v1" / "profiles" =>
  req.as[UserProfile] flatMap {(up: UserProfile) =>
    Ok(service.createProfile(up).asJson)
  }

With the following POST body:

{
   "firstName": "Jack",
   "lastName": "Appleseed"
}

I think this happens when the body is being converted to UserProfile in req.as[UserProfile]!

But, this is a plain vanilla case class, the EntityDecoder should be auto-derived! I know akka-http does it!

Any ideas/suggestions?

Please Note: Http4sVersion = "0.18.0-M4" and circe version "0.9.0-M1"

2

There are 2 best solutions below

1
On BEST ANSWER

Adding this dependency: "io.circe" %% "circe-generic" % "0.9.1"

resolved the auto-encoding of case classes to JSON for me.

It allows for the required import: import io.circe.generic.auto._

1
On

The answer is:

req.decodeJson[UserProfile] flatMap {(up: UserProfile) =>
    Ok(service.createProfile(up).asJson)
  }

The reason you get that is the bridge from a Circe decoder to an http4s EntityDecoder is not implicit. You can make one if you're purely a JSON API, but that's not an assumption the library can generally make.