From 2b3c9b5e3aecf42cd6da3df3de3beff18903901d Mon Sep 17 00:00:00 2001 From: Pawel Sadlo Date: Wed, 11 Sep 2024 13:50:14 +0200 Subject: [PATCH 1/4] Enabling partial provision of environment to routes when provision results in intersection type. --- .../src/test/scala/zio/http/RouteSpec.scala | 44 +++++++++++++++++++ .../http/RoutesCompanionVersionSpecific.scala | 2 - 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/zio-http/jvm/src/test/scala/zio/http/RouteSpec.scala b/zio-http/jvm/src/test/scala/zio/http/RouteSpec.scala index 82b4b2a3ab..06e77ef498 100644 --- a/zio-http/jvm/src/test/scala/zio/http/RouteSpec.scala +++ b/zio-http/jvm/src/test/scala/zio/http/RouteSpec.scala @@ -19,6 +19,9 @@ package zio.http import zio._ import zio.test._ +import zio.http.codec.{HttpCodec, StatusCodec} +import zio.http.endpoint.{AuthType, Endpoint} + object RouteSpec extends ZIOHttpSpec { def extractStatus(response: Response): Status = response.status @@ -163,6 +166,47 @@ object RouteSpec extends ZIOHttpSpec { bodyString <- response.body.asString } yield assertTrue(extractStatus(response) == Status.InternalServerError, bodyString == "error") }, + test( + "Routes with context can eliminate environment type partially when elimination produces intersection type environment", + ) { + + val authContext: HandlerAspect[Any, String] = HandlerAspect.customAuthProviding[String] { request => + { + request.headers.get(Header.Authorization).flatMap { + case Header.Authorization.Basic(uname, secret) if uname.reverse == secret.value.mkString => + Some(uname) + case _ => + None + } + } + } + + val endpoint = Endpoint(RoutePattern(Method.GET, Path.root)) + .outCodec[String](StatusCodec.Ok ++ HttpCodec.content[String]) + .auth(AuthType.Basic) + + val effectWithTwoDependency: ZIO[Int & Long, Nothing, String] = for { + int <- ZIO.service[Int] + long <- ZIO.service[Long] + } yield s"effectWithTwoDependencyResult $int $long" + + val route: Route[Int & Long & String, Nothing] = + endpoint.implement((_: Unit) => withContext((_: String) => "") *> effectWithTwoDependency) + val routes = Routes(route).@@[Int & Long](authContext) + + val env: ZEnvironment[Int with Long] = ZEnvironment(1).add(2L) + for { + response <- routes + .provideEnvironment(env) + .apply( + Request( + headers = Headers("accept", "text") ++ Headers(Header.Authorization.Basic("123", "321")), + method = Method.GET, + ).path(Path.root), + ) + bodyString <- response.body.asString + } yield assertTrue(bodyString == s"\"effectWithTwoDependencyResult 1 2\"") + }, ), ) } diff --git a/zio-http/shared/src/main/scala-2/zio/http/RoutesCompanionVersionSpecific.scala b/zio-http/shared/src/main/scala-2/zio/http/RoutesCompanionVersionSpecific.scala index b225afdeb5..d08191ee52 100644 --- a/zio-http/shared/src/main/scala-2/zio/http/RoutesCompanionVersionSpecific.scala +++ b/zio-http/shared/src/main/scala-2/zio/http/RoutesCompanionVersionSpecific.scala @@ -6,8 +6,6 @@ trait RoutesCompanionVersionSpecific { private[http] class ApplyContextAspect[-Env, +Err, Env0](private val self: Routes[Env, Err]) { def apply[Env1, Env2 <: Env, Ctx: Tag](aspect: HandlerAspect[Env1, Ctx])(implicit ev: Env0 with Ctx <:< Env, - tag: Tag[Env0], - tag1: Tag[Env1], ): Routes[Env0 with Env1, Err] = self.transform(_.@@[Env0](aspect)) } From c98a057f7aab4fb93e8f9f398ebc0ceb6041487d Mon Sep 17 00:00:00 2001 From: Pawel Sadlo Date: Wed, 11 Sep 2024 13:50:14 +0200 Subject: [PATCH 2/4] fix --- zio-http/jvm/src/test/scala/zio/http/RouteSpec.scala | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/zio-http/jvm/src/test/scala/zio/http/RouteSpec.scala b/zio-http/jvm/src/test/scala/zio/http/RouteSpec.scala index 06e77ef498..26e9ac4c33 100644 --- a/zio-http/jvm/src/test/scala/zio/http/RouteSpec.scala +++ b/zio-http/jvm/src/test/scala/zio/http/RouteSpec.scala @@ -195,6 +195,7 @@ object RouteSpec extends ZIOHttpSpec { val routes = Routes(route).@@[Int & Long](authContext) val env: ZEnvironment[Int with Long] = ZEnvironment(1).add(2L) + val expected = s"\"effectWithTwoDependencyResult 1 2\"" for { response <- routes .provideEnvironment(env) @@ -205,7 +206,7 @@ object RouteSpec extends ZIOHttpSpec { ).path(Path.root), ) bodyString <- response.body.asString - } yield assertTrue(bodyString == s"\"effectWithTwoDependencyResult 1 2\"") + } yield assertTrue(bodyString == expected) }, ), ) From d90a5bc165312bb77e4aaacea953fe442de9d33f Mon Sep 17 00:00:00 2001 From: Pawel Sadlo Date: Wed, 11 Sep 2024 14:01:41 +0200 Subject: [PATCH 3/4] fix --- zio-http/jvm/src/test/scala/zio/http/RouteSpec.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zio-http/jvm/src/test/scala/zio/http/RouteSpec.scala b/zio-http/jvm/src/test/scala/zio/http/RouteSpec.scala index 26e9ac4c33..28afbc1925 100644 --- a/zio-http/jvm/src/test/scala/zio/http/RouteSpec.scala +++ b/zio-http/jvm/src/test/scala/zio/http/RouteSpec.scala @@ -195,7 +195,7 @@ object RouteSpec extends ZIOHttpSpec { val routes = Routes(route).@@[Int & Long](authContext) val env: ZEnvironment[Int with Long] = ZEnvironment(1).add(2L) - val expected = s"\"effectWithTwoDependencyResult 1 2\"" + val expected = "\"effectWithTwoDependencyResult 1 2\"" for { response <- routes .provideEnvironment(env) From e6a1990bc79eee0f594b3a4021c907822c17e59c Mon Sep 17 00:00:00 2001 From: Pawel Sadlo Date: Wed, 11 Sep 2024 14:04:33 +0200 Subject: [PATCH 4/4] format --- zio-http/jvm/src/test/scala/zio/http/RouteSpec.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zio-http/jvm/src/test/scala/zio/http/RouteSpec.scala b/zio-http/jvm/src/test/scala/zio/http/RouteSpec.scala index 28afbc1925..a0fc0f8b0a 100644 --- a/zio-http/jvm/src/test/scala/zio/http/RouteSpec.scala +++ b/zio-http/jvm/src/test/scala/zio/http/RouteSpec.scala @@ -195,7 +195,7 @@ object RouteSpec extends ZIOHttpSpec { val routes = Routes(route).@@[Int & Long](authContext) val env: ZEnvironment[Int with Long] = ZEnvironment(1).add(2L) - val expected = "\"effectWithTwoDependencyResult 1 2\"" + val expected = "\"effectWithTwoDependencyResult 1 2\"" for { response <- routes .provideEnvironment(env)