zio / zio-http

A next-generation Scala framework for building scalable, correct, and efficient HTTP clients and servers
https://zio.dev/zio-http
Apache License 2.0
781 stars 392 forks source link

ClassCassException when using HandlerAspect and Path parameters. #3141

Open Petter-K opened 3 days ago

Petter-K commented 3 days ago

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

jdegoes commented 1 day ago

/bounty $150 for fix and test case.

algora-pbc[bot] commented 1 day ago

💎 $150 bounty • ZIO

Steps to solve:

  1. Start working: Comment /attempt #3141 with your implementation plan
  2. Submit work: Create a pull request including /claim #3141 in the PR body to claim the bounty
  3. Receive payment: 100% of the bounty is received 2-5 days post-reward. Make sure you are eligible for payouts

Thank you for contributing to zio/zio-http!

Add a bountyShare on socials

Attempt Started (GMT+0) Solution
🟢 @varshith257 Sep 18, 2024, 1:46:20 PM WIP
🟢 @987Nabil #3150
varshith257 commented 1 day ago

/attempt #3141

Algora profile Completed bounties Tech Active attempts Options
@varshith257    4 ZIO bounties
+ 9 bounties from 6 projects
TypeScript, Go
Cancel attempt
algora-pbc[bot] commented 1 day ago

💡 @987Nabil submitted a pull request that claims the bounty. You can visit your bounty board to reward.