Skip to content

Commit

Permalink
Merge pull request #761 from vaslabs/fix/tail-capture-segment-class-cast
Browse files Browse the repository at this point in the history
Fix/tail capture segment class cast
  • Loading branch information
FrancescoSerra authored Sep 21, 2022
2 parents 35fa148 + 83085d1 commit 528d789
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 12 deletions.
3 changes: 2 additions & 1 deletion core/src/main/scala/org/http4s/rho/PathBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.http4s
package rho

import cats.Functor
import org.http4s.Uri.Path.Segment
import org.http4s.rho.bits.PathAST._
import org.http4s.rho.bits.RequestAST.{EmptyRule, RequestRule}
import org.http4s.rho.bits._
Expand Down Expand Up @@ -40,7 +41,7 @@ final class PathBuilder[F[_], T <: HList](val method: Method, val path: PathRule
* @param tail
* @return a [[Router]]
*/
def /(tail: CaptureTail.type): Router[F, List[String] :: T] =
def /(tail: CaptureTail.type): Router[F, List[Segment] :: T] =
new Router(method, PathAnd(path, tail), EmptyRule[F]())

/** Match against a `String`
Expand Down
6 changes: 3 additions & 3 deletions core/src/test/scala/ApiExamples.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import java.util.Date
import java.util.UUID
import java.time.Instant
import java.util.concurrent.atomic.AtomicInteger

import cats.effect.IO
import cats.syntax.all._
import munit.FunSuite
import org.http4s.Uri.Path.Segment
import org.http4s.headers.{ETag, `Content-Length`}
import org.http4s.rho.bits.TypedQuery
import org.http4s.rho.RhoRoutes
Expand Down Expand Up @@ -58,8 +58,8 @@ class ApiExamples extends FunSuite {
/// src_inlined CaptureTail
new RhoRoutes[IO] {
// You can capture the entire rest of the tail using *
GET / "hello" / * |>> { r: List[String] =>
Ok(s"Got the rest: ${r.mkString}")
GET / "hello" / * |>> { r: List[Segment] =>
Ok(s"Got the rest: ${r.map(_.encoded).mkString}")
}
}
/// end_src_inlined
Expand Down
5 changes: 3 additions & 2 deletions core/src/test/scala/org/http4s/rho/ApiTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import cats.data.OptionT
import cats.effect.{IO, Sync}
import cats.syntax.parallel._
import munit.CatsEffectSuite
import org.http4s.Uri.Path.Segment
import org.http4s.headers.Accept
import org.http4s.headers.{ETag, `Content-Length`}
import org.http4s.rho.bits._
Expand Down Expand Up @@ -370,7 +371,7 @@ class ApiTest extends CatsEffectSuite {
test("A PathValidator should capture end with nothing") {
val stuff = GET / "hello" / *
val req = Request[IO](uri = Uri.fromString("/hello").getOrElse(sys.error("Failed.")))
val f = runWith(stuff) { path: List[String] =>
val f = runWith(stuff) { path: List[Segment] =>
Ok("Cool.").map(_.putHeaders(ETag(ETag.EntityTag(if (path.isEmpty) "go" else "nogo"))))
}

Expand All @@ -381,7 +382,7 @@ class ApiTest extends CatsEffectSuite {
val stuff = GET / "hello" / *
val req =
Request[IO](uri = Uri.fromString("/hello/world/foo").getOrElse(sys.error("Failed.")))
val f = runWith(stuff) { path: List[String] =>
val f = runWith(stuff) { path: List[Segment] =>
Ok("Cool.").map(_.putHeaders(ETag(ETag.EntityTag(path.mkString))))
}

Expand Down
5 changes: 3 additions & 2 deletions core/src/test/scala/org/http4s/rho/RhoRoutesSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package org.http4s
package rho

import java.util.concurrent.atomic.AtomicInteger

import cats.effect.IO
import fs2.Stream
import munit.CatsEffectSuite
import org.http4s.headers.{`Content-Length`, `Content-Type`}
import org.http4s.rho.io._
import org.http4s.Uri.Path
import org.http4s.Uri.Path.Segment
import org.typelevel.ci.CIString

import scala.collection.compat.immutable.ArraySeq
Expand Down Expand Up @@ -63,7 +63,8 @@ class RhoRoutesSuite extends CatsEffectSuite with RequestRunner {
Ok("twoparams2_" + foo + bar.getOrElse("cat"))
}

GET / "variadic" / * |>> { tail: List[String] => Ok("route8_" + tail.mkString("/")) }
GET / "variadic" / * |>> { tail: List[Segment] =>
Ok("route8_" + tail.map(_.encoded).mkString("/")) }

val or = "or1" || "or2"
GET / or |>> { () => Ok("route9") }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.http4s.rho.swagger.demo

import cats.effect.{IO}
import org.http4s.Uri.Path.Segment
import org.http4s.dsl.io._
import org.http4s.rho.RhoRoutes
import org.http4s.{HttpRoutes, Request, Response, StaticFile}
Expand All @@ -13,13 +14,13 @@ object StaticContentService {
*/
def routes: HttpRoutes[IO] = new RhoRoutes[IO] {
// Swagger User Interface
GET / "css" / * |>> { (req: Request[IO], _: List[String]) =>
GET / "css" / * |>> { (req: Request[IO], _: List[Segment]) =>
fetchResource(swaggerUiDir + req.pathInfo, req)
}
GET / "images" / * |>> { (req: Request[IO], _: List[String]) =>
GET / "images" / * |>> { (req: Request[IO], _: List[Segment]) =>
fetchResource(swaggerUiDir + req.pathInfo, req)
}
GET / "lib" / * |>> { (req: Request[IO], _: List[String]) =>
GET / "lib" / * |>> { (req: Request[IO], _: List[Segment]) =>
fetchResource(swaggerUiDir + req.pathInfo, req)
}
GET / "swagger-ui" |>> { req: Request[IO] =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.http4s.rho.swagger.ui

import cats.effect.{Sync}
import cats.implicits._
import org.http4s.Uri.Path.Segment
import org.http4s.headers.`Content-Type`
import org.http4s.rho.RhoRoutes
import org.http4s.rho.bits.PathAST.CaptureTail
Expand Down Expand Up @@ -29,7 +30,7 @@ class SwaggerUiRoutes[F[_]: Sync](
Ok(indexHtml)(implicitly, htmlEncoder)
}

GET / swaggerUiPath / CaptureTail |>> { (req: Request[F], path: List[String]) =>
GET / swaggerUiPath / CaptureTail |>> { (req: Request[F], path: List[Segment]) =>
fetchResource(swaggerUiResourcesPath + path.mkString("/", "/", ""), req)
}

Expand Down

0 comments on commit 528d789

Please sign in to comment.