Skip to content

Commit

Permalink
Merge pull request #1163 from jakobmerrild/coerce-incoming-strings-fo…
Browse files Browse the repository at this point in the history
…r-long-type

Allow string inputs for Long Scalar type
  • Loading branch information
yanns authored Dec 16, 2024
2 parents 6b1214d + 257adb0 commit e86440f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
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

0 comments on commit e86440f

Please sign in to comment.