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

Allow string inputs for Long Scalar type #1163

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions modules/core/src/main/scala/sangria/schema/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sangria

import sangria.marshalling.MarshallerCapability
import sangria.validation._
import scala.util.Try

/** Types that describe a GraphQL schema.
*
Expand Down Expand Up @@ -47,12 +48,14 @@ package object schema {
case i: BigInt => Right(i.longValue)
case d: Double if d.isWhole => Right(d.toLong)
case d: BigDecimal if d.isValidLong => Right(d.longValue)
case s: String if Try(s.toLong).isSuccess => Right(s.toLong)
case _ => Left(LongCoercionViolation)
},
coerceInput = {
case ast.IntValue(i, _, _) => Right(i: Long)
case ast.BigIntValue(i, _, _) if !i.isValidLong => Left(BigLongCoercionViolation)
case ast.BigIntValue(i, _, _) => Right(i.longValue)
case ast.StringValue(s, _, _, _, _) if Try(s.toLong).isSuccess => Right(s.toLong)
case _ => Left(LongCoercionViolation)
}
)
Expand Down
12 changes: 10 additions & 2 deletions modules/core/src/test/scala/sangria/schema/CoercionSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,17 @@ class CoercionSpec extends AnyWordSpec with Matchers {
LongType.coerceInput(BigIntValue(BigInt("123234"))) should be(Right(123234L))
LongType.coerceInput(BigIntValue(BigInt("1232342131243432"))) should be(
Right(1232342131243432L))
LongType.coerceInput(StringValue(Long.MaxValue.toString)) should be(Right(Long.MaxValue))
LongType
.coerceInput(BigIntValue(BigInt("123234438749837964783648763284768372648723684763287")))
.isLeft should be(true)

LongType.coerceInput(FloatValue(12.34)).isLeft should be(true)
LongType.coerceInput(BigDecimalValue(BigDecimal(12.34))).isLeft should be(true)
LongType.coerceInput(BooleanValue(true)).isLeft should be(true)
LongType.coerceInput(StringValue("123")).isLeft should be(true)
LongType
.coerceInput(StringValue("123234438749837964783648763284768372648723684763287"))
.isLeft should be(true)
}

"BigInt" in {
Expand Down Expand Up @@ -223,7 +226,12 @@ class CoercionSpec extends AnyWordSpec with Matchers {
LongType.coerceUserInput(12.34d).isLeft should be(true)
LongType.coerceUserInput(BigDecimal(12.34)).isLeft should be(true)
LongType.coerceUserInput(true).isLeft should be(true)
LongType.coerceUserInput("123").isLeft should be(true)
LongType.coerceUserInput("123") should be(Right(123L))
LongType.coerceUserInput("").isLeft should be(true)
LongType
.coerceUserInput("1232344387498237498732974982334324234325435")
.isLeft should be(true)
LongType.coerceUserInput("123.0").isLeft should be(true)
LongType.coerceUserInput(new Date).isLeft should be(true)
}

Expand Down
Loading