akka-http-json icon indicating copy to clipboard operation
akka-http-json copied to clipboard

Compile fails in scala 3 with implicit errors for circe

Open Marcus-Rosti opened this issue 3 years ago • 3 comments

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

Marcus-Rosti avatar Oct 08 '21 00:10 Marcus-Rosti

Akka does not yet support Scala 3.

hseeberger avatar Oct 08 '21 07:10 hseeberger

Akka does not yet support Scala 3.

It does now, although akka-http is still lagging in this department, iirc.

godenji avatar Nov 05 '21 04:11 godenji

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.

AvaPL avatar Jan 22 '22 21:01 AvaPL