Skip to content

Commit

Permalink
Merge pull request #10413 from playframework/mergify/bp/2.8.x/pr-10399
Browse files Browse the repository at this point in the history
Fixes int binder error message and provides specs (bp #10399)
  • Loading branch information
mergify[bot] committed Aug 7, 2020
2 parents cf4e0cb + 5895151 commit 5fb95df
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/play/src/main/scala/play/api/mvc/Binders.scala
Expand Up @@ -338,7 +338,7 @@ object QueryStringBindable {
* QueryString binder for Int.
*/
implicit object bindableInt
extends Parsing[Int](_.toInt, _.toString, (s, e) => s"Cannot parse parameter $e as Int: ${e.getMessage}")
extends Parsing[Int](_.toInt, _.toString, (s, e) => s"Cannot parse parameter $s as Int: ${e.getMessage}")

/**
* QueryString binder for Integer.
Expand Down
31 changes: 31 additions & 0 deletions core/play/src/test/scala/play/api/mvc/BindersSpec.scala
Expand Up @@ -241,4 +241,35 @@ class BindersSpec extends Specification {
implicitly[QueryStringBindable[Hase]].unbind("key", Hase("Disney_Land")) must equalTo("key=Disney_Land")
}
}

"URL QueryStringBindable Int" should {
val subject = implicitly[QueryStringBindable[Int]]
val int = 6182
val string = "6182"

"Unbind query string int as string" in {
subject.unbind("key", int) must equalTo(s"key=${string}")
}
"Bind query string as int" in {
subject.bind("key", Map("key" -> Seq(string))) must beSome(Right(int))
}
"Fail on value must contain only digits" in {
subject.bind("key", Map("key" -> Seq("foo"))) must beSome(
Left("Cannot parse parameter key as Int: For input string: \"foo\"")
)
}
"Fail on value < -2147483648" in {
subject.bind("key", Map("key" -> Seq("-2147483649"))) must beSome(
Left("Cannot parse parameter key as Int: For input string: \"-2147483649\"")
)
}
"Fail on value > 2147483647" in {
subject.bind("key", Map("key" -> Seq("2147483648"))) must beSome(
Left("Cannot parse parameter key as Int: For input string: \"2147483648\"")
)
}
"Be None on empty" in {
subject.bind("key", Map("key" -> Seq(""))) must beNone
}
}
}

0 comments on commit 5fb95df

Please sign in to comment.