Skip to content

Commit

Permalink
Route: Do not add ampersand if optional parameter is None
Browse files Browse the repository at this point in the history
  • Loading branch information
tindzk committed Jul 8, 2019
1 parent 89f7b4a commit 03ad4bd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
11 changes: 6 additions & 5 deletions shared/src/main/scala/trail/Route.scala
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,12 @@ object Route {
case class ParamRoute[A, P](route: Route[A], param: Param[P]) extends Route[(A, P)] {
override def url(value: (A, P)): String = {
val base = route.url(value._1)
val encodedParam =
param.codec.encode(value._2).map(v => param.name + "=" + URI.encode(v))
.getOrElse("")
val delimiter = if (base.contains("?")) "&" else "?"
base + delimiter + encodedParam
param.codec.encode(value._2)
.map(v => param.name + "=" + URI.encode(v))
.fold(base) { encodedParam =>
val delimiter = if (base.contains("?")) "&" else "?"
base + delimiter + encodedParam
}
}
override def parseInternal(path: Path): Option[((A, P), Path)] =
for {
Expand Down
7 changes: 7 additions & 0 deletions shared/src/test/scala/trail/UrlTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,11 @@ class UrlTests2 extends FunSpec with Matchers {

assert(url == "/list?upload=true")
}

it("url() should work with optional parameter set to None") {
val route = Root / "disk" / "view" & Param[Long]("disk") & Param[Option[Long]]("folder")
val url = route((48L, None))

assert(url == "/disk/view?disk=48")
}
}

0 comments on commit 03ad4bd

Please sign in to comment.