akka-http-json
akka-http-json copied to clipboard
Compile fails in scala 3 with implicit errors for circe
this passes
complete(ToResponseMarshallable(MyResponse(???))(marshaller[MyResponse]))
but this fails
complete(MyResponse(???))
with error
[error] 21 | complete(MyResponse(???))
[error] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[error] |Found: my.dumb.class.MyResponse
[error] |Required: akka.http.scaladsl.marshalling.ToResponseMarshallable
[error] |
[error] |One of the following imports might make progress towards fixing the problem:
[error] |
[error] | import akka.http.impl.util.JavaMapping.Implicits.convertToScala
[error] | import akka.http.javadsl.server.RoutingJavaMapping.Implicits.convertToScala
[error] |
[error] Explanation
[error] ===========
[error]
[error] I tried to show that
[error] my.dumb.class.MyResponse
[error] conforms to
[error] akka.http.scaladsl.marshalling.ToResponseMarshallable
[error] but the comparison trace ended with `false`:
My guess is that using circe in scala3, caused some drift with akka-http-json
Akka does not yet support Scala 3.
Akka does not yet support Scala 3.
It does now, although akka-http is still lagging in this department, iirc.
My workaround for this issue was to use crosscompiled Akka together with circe for Scala 3. Here is my minimal sbt config:
val circeVersion = "0.14.1"
val akkaVersion = "2.6.18"
val akkaHttpVersion = "10.2.7"
val akkaHttpCirceVersion = "1.39.2"
lazy val circeCore = "io.circe" %% "circe-core" % circeVersion
lazy val circeGeneric = "io.circe" %% "circe-generic" % circeVersion
lazy val circeParser = "io.circe" %% "circe-parser" % circeVersion
lazy val akkaStream = "com.typesafe.akka" %% "akka-stream" % akkaVersion
lazy val akkaHttp = "com.typesafe.akka" %% "akka-http" % akkaHttpVersion
lazy val akkaHttpCirce = "de.heikoseeberger" %% "akka-http-circe" % akkaHttpCirceVersion
lazy val scala3cross2 = project
.settings(
scalaVersion := "3.1.0",
libraryDependencies ++= List(
circeCore,
circeGeneric,
circeParser
) ++ List(
akkaStream,
akkaHttp,
akkaHttpCirce
).map(
_.cross(CrossVersion.for3Use2_13)
),
conflictWarning := ConflictWarning.disable
)
Now you can use Scala 3 circe syntax for codecs on case classes:
import io.circe.Codec
case class Example(value: String) derives Codec.AsObject
This setup works exactly like using io.circe.generic.auto.*
in 2.13 and the error is gone. I'm currently using it with FailFastCirceSupport
with no issues.