Skip to content

ClassCassException when using HandlerAspect and Path parameters. #3141

@Petter-K

Description

@Petter-K

Describe the bug
We are getting ClassCastExceptions when using a route with Path paramters, and a middleware that adds context.
class zio.http.Request cannot be cast to class scala.Tuple2 (zio.http.Request and scala.Tuple2 are in unnamed module of loader 'app')
Tuple2 is increasing if we add more path params

To Reproduce
Run this snipped

import zio.http._
import zio._

import scala.annotation.nowarn

object Reproducer extends ZIOAppDefault {
  case class WebSession(id: Int)

  def maybeWebSession: HandlerAspect[Any, Option[WebSession]] =
    HandlerAspect.interceptIncomingHandler(
      Handler.fromFunctionZIO[Request] { req =>
        ZIO.succeed((req, None))
      }
    )

  @nowarn("msg=dead code following this construct")
  override def run: ZIO[Any, Any, Any] = {
    // fails at Handler.scala line 57 with 
    val route = Method.GET / "base" / string("1") -> handler((a: String, req: Request) => {
      withContext((c: Option[WebSession]) => {
        ZIO.logInfo("Hello").as(Response.ok)
      })
    }) @@ maybeWebSession



    (for {
      port <- Server.install(Routes(route))
      _ <- ZIO.logInfo(s"Installed on port " + port)
      _ <- ZIO.never
    } yield ()).provide(Server.default)

  }
}

run: curl -v 127.0.0.1:8080/base/1

Expected behaviour
Log Hello and return 200 Ok

Additional context
Same error in scala 2 and 3.
Using Intellij breakpoints on all exception, it stops at Handler.scala - line 57

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions