diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 252ff31af..31c78e038 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -78,6 +78,8 @@ jobs: cache: sbt - uses: coursier/setup-action@v1 + with: + apps: sbt - name: Check formatting if: matrix.scala == '2.13.14' @@ -90,6 +92,8 @@ jobs: run: sbt '++ ${{ matrix.scala }}' test - uses: coursier/setup-action@v1 + with: + apps: sbt - name: Check doc generation if: ${{ github.event_name == 'pull_request' }} @@ -238,6 +242,8 @@ jobs: fetch-depth: 0 - uses: coursier/setup-action@v1 + with: + apps: sbt - name: Setup Java (temurin@21) if: matrix.java == 'temurin@21' diff --git a/.github/workflows/site.yml b/.github/workflows/site.yml index f8bc4e9ef..25ebc0a31 100644 --- a/.github/workflows/site.yml +++ b/.github/workflows/site.yml @@ -24,6 +24,8 @@ jobs: with: fetch-depth: '0' - uses: coursier/setup-action@v1 + with: + apps: 'sbt' - name: Setup Scala uses: actions/setup-java@v3.9.0 with: @@ -50,6 +52,8 @@ jobs: java-version: 17 check-latest: true - uses: coursier/setup-action@v1 + with: + apps: 'sbt' - name: Setup NodeJs uses: actions/setup-node@v3 with: @@ -70,6 +74,8 @@ jobs: with: fetch-depth: '0' - uses: coursier/setup-action@v1 + with: + apps: 'sbt' - name: Install libuv run: sudo apt-get update && sudo apt-get install -y libuv1-dev - name: Setup Scala diff --git a/build.sbt b/build.sbt index c35a4dd9f..82a8453fa 100644 --- a/build.sbt +++ b/build.sbt @@ -27,7 +27,14 @@ ThisBuild / githubWorkflowPREventTypes := Seq( PREventType.Edited, PREventType.Labeled, ) -ThisBuild / githubWorkflowAddedJobs := + +val coursierSetup = + WorkflowStep.Use( + UseRef.Public("coursier", "setup-action", "v1"), + params = Map("apps" -> "sbt"), + ) + +ThisBuild / githubWorkflowAddedJobs := Seq( WorkflowJob( id = "update_release_draft", @@ -40,7 +47,7 @@ ThisBuild / githubWorkflowAddedJobs := name = "Mima Check", steps = List( WorkflowStep.Use(UseRef.Public("actions", "checkout", "v4"), Map("fetch-depth" -> "0")), - WorkflowStep.Use(UseRef.Public("coursier", "setup-action", "v1")), + coursierSetup, ) ++ WorkflowStep.SetupJava(List(JavaSpec.temurin("21"))) :+ WorkflowStep.Sbt(List("mimaChecks")), cond = Option("${{ github.event_name == 'pull_request' }}"), javas = List(JavaSpec.temurin("21")), @@ -78,7 +85,7 @@ ThisBuild / githubWorkflowPublish := //scala fix isn't available for scala 3 so ensure we only run the fmt check //using the latest scala 2.13 ThisBuild / githubWorkflowBuildPreamble := Seq( - WorkflowStep.Use(UseRef.Public("coursier", "setup-action", "v1")), + coursierSetup, WorkflowStep.Run( name = Some("Check formatting"), commands = List(s"sbt ++${Scala213} fmtCheck"), @@ -91,7 +98,7 @@ ThisBuild / githubWorkflowBuildPostamble := "checkDocGeneration", "Check doc generation", List( - WorkflowStep.Use(UseRef.Public("coursier", "setup-action", "v1")), + coursierSetup, WorkflowStep.Run( commands = List(s"sbt ++${Scala213} doc"), name = Some("Check doc generation"), diff --git a/zio-http/jvm/src/test/scala/zio/http/ClientSpec.scala b/zio-http/jvm/src/test/scala/zio/http/ClientSpec.scala index 51695c4c0..48a0519b7 100644 --- a/zio-http/jvm/src/test/scala/zio/http/ClientSpec.scala +++ b/zio-http/jvm/src/test/scala/zio/http/ClientSpec.scala @@ -120,6 +120,16 @@ object ClientSpec extends RoutesRunnableSpec { app.deploy(Request(headers = Headers(Header.Authorization.Unparsed("", "my-token")))).flatMap(_.body.asString) assertZIO(responseContent)(equalTo("my-token")) } @@ timeout(5.seconds), + test("URL and path manipulation on client level") { + for { + baseURL <- DynamicServer.httpURL + _ <- + Handler.ok.toRoutes.deployAndRequest { c => + (c.updatePath(_ / "my-service") @@ ZClientAspect.requestLogging()).batched.get("/hello") + }.runZIO(()) + loggedUrl <- ZTestLogger.logOutput.map(_.collectFirst { case m => m.annotations("url") }.mkString) + } yield assertTrue(loggedUrl == baseURL + "/my-service/hello") + }, ) override def spec = { diff --git a/zio-http/shared/src/main/scala/zio/http/Body.scala b/zio-http/shared/src/main/scala/zio/http/Body.scala index a72fdce45..00ba269aa 100644 --- a/zio-http/shared/src/main/scala/zio/http/Body.scala +++ b/zio-http/shared/src/main/scala/zio/http/Body.scala @@ -567,21 +567,27 @@ object Body { override def asStream(implicit trace: Trace): ZStream[Any, Throwable, Byte] = ZStream.unwrap { - for { - file <- ZIO.attempt(file) - fs <- ZIO.attemptBlocking(new FileInputStream(file)) - size <- ZIO.attemptBlocking(Math.min(chunkSize.toLong, file.length()).toInt) - } yield ZStream - .repeatZIOOption[Any, Throwable, Chunk[Byte]] { - for { - buffer <- ZIO.succeed(new Array[Byte](size)) - len <- ZIO.attemptBlocking(fs.read(buffer)).mapError(Some(_)) - bytes <- - if (len > 0) ZIO.succeed(Chunk.fromArray(buffer.slice(0, len))) - else ZIO.fail(None) - } yield bytes - } - .ensuring(ZIO.attemptBlocking(fs.close()).ignoreLogged) + ZIO.blocking { + for { + r <- ZIO.attempt { + val fs = new FileInputStream(file) + val size = Math.min(chunkSize.toLong, file.length()).toInt + + (fs, size) + } + (fs, size) = r + } yield ZStream + .repeatZIOOption[Any, Throwable, Chunk[Byte]] { + for { + buffer <- ZIO.succeed(new Array[Byte](size)) + len <- ZIO.attempt(fs.read(buffer)).mapError(Some(_)) + bytes <- + if (len > 0) ZIO.succeed(Chunk.fromArray(buffer.slice(0, len))) + else ZIO.fail(None) + } yield bytes + } + .ensuring(ZIO.attempt(fs.close()).ignoreLogged) + } }.flattenChunks override def contentType(newContentType: Body.ContentType): Body = copy(contentType = Some(newContentType)) diff --git a/zio-http/shared/src/main/scala/zio/http/ZClient.scala b/zio-http/shared/src/main/scala/zio/http/ZClient.scala index 014d77cca..ce6d47b33 100644 --- a/zio-http/shared/src/main/scala/zio/http/ZClient.scala +++ b/zio-http/shared/src/main/scala/zio/http/ZClient.scala @@ -151,8 +151,10 @@ final case class ZClient[-Env, ReqEnv, -In, +Err, +Out]( def path(path: String): ZClient[Env, ReqEnv, In, Err, Out] = self.path(Path(path)) - def path(path: Path): ZClient[Env, ReqEnv, In, Err, Out] = - copy(url = url.copy(path = path)) + def path(path: Path): ZClient[Env, ReqEnv, In, Err, Out] = updatePath(_ => path) + + def updatePath(f: Path => Path): ZClient[Env, ReqEnv, In, Err, Out] = + copy(url = url.copy(path = f(url.path))) def patch(suffix: String)(implicit ev: Body <:< In, trace: Trace): ZIO[Env & ReqEnv, Err, Out] = request(Method.PATCH, suffix)(ev(Body.empty)) @@ -263,6 +265,8 @@ final case class ZClient[-Env, ReqEnv, -In, +Err, +Out]( def uri(uri: URI): ZClient[Env, ReqEnv, In, Err, Out] = url(URL.fromURI(uri).getOrElse(URL.empty)) def url(url: URL): ZClient[Env, ReqEnv, In, Err, Out] = copy(url = url) + + def updateURL(f: URL => URL): ZClient[Env, ReqEnv, In, Err, Out] = copy(url = f(url)) } object ZClient extends ZClientPlatformSpecific { diff --git a/zio-http/shared/src/main/scala/zio/http/codec/internal/EncoderDecoder.scala b/zio-http/shared/src/main/scala/zio/http/codec/internal/EncoderDecoder.scala index 705b432cb..44d99b72d 100644 --- a/zio-http/shared/src/main/scala/zio/http/codec/internal/EncoderDecoder.scala +++ b/zio-http/shared/src/main/scala/zio/http/codec/internal/EncoderDecoder.scala @@ -419,18 +419,21 @@ private[codec] object EncoderDecoder { private def decodeBody(config: CodecConfig, body: Body, inputs: Array[Any])(implicit trace: Trace, ): Task[Unit] = { - val codecs = flattened.content + val isNonMultiPart = inputs.length < 2 + if (isNonMultiPart) { + val codecs = flattened.content - if (inputs.length < 2) { - // non multi-part - codecs.headOption.map { codec => + // noinspection SimplifyUnlessInspection + if (codecs.isEmpty) ZIO.unit + else { + val codec = codecs.head codec .decodeFromBody(body, config) .mapBoth( - { err => HttpCodecError.MalformedBody(err.getMessage(), Some(err)) }, + { err => HttpCodecError.MalformedBody(err.getMessage, Some(err)) }, result => inputs(0) = result, ) - }.getOrElse(ZIO.unit) + } } else { // multi-part decodeForm(body.asMultipartFormStream, inputs, config) *> check(inputs)