Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes int binder error message and provides specs (bp #10399) #10413

Merged
merged 1 commit into from Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}
}
}