From 58018dbc5b2172ba207cbaa7e38c313508947fa4 Mon Sep 17 00:00:00 2001 From: Jakob Merrild Date: Fri, 13 Dec 2024 13:13:20 +0100 Subject: [PATCH 1/3] Allow string inputs for Long Scalar type --- modules/core/src/main/scala/sangria/schema/package.scala | 2 ++ .../src/test/scala/sangria/schema/CoercionSpec.scala | 9 +++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/modules/core/src/main/scala/sangria/schema/package.scala b/modules/core/src/main/scala/sangria/schema/package.scala index e540a4ec..ac9be011 100644 --- a/modules/core/src/main/scala/sangria/schema/package.scala +++ b/modules/core/src/main/scala/sangria/schema/package.scala @@ -47,12 +47,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 s.toLongOption.isDefined => 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 s.toLongOption.isDefined => Right(s.toLong) case _ => Left(LongCoercionViolation) } ) diff --git a/modules/core/src/test/scala/sangria/schema/CoercionSpec.scala b/modules/core/src/test/scala/sangria/schema/CoercionSpec.scala index c924e21f..25abefce 100644 --- a/modules/core/src/test/scala/sangria/schema/CoercionSpec.scala +++ b/modules/core/src/test/scala/sangria/schema/CoercionSpec.scala @@ -78,14 +78,16 @@ 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 { @@ -223,7 +225,10 @@ 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) } From b2b2e37370fed68c9135a34381ba8cd4f39ad6a5 Mon Sep 17 00:00:00 2001 From: Jakob Merrild Date: Mon, 16 Dec 2024 09:55:55 +0100 Subject: [PATCH 2/3] Use `Try` to test if string value is a valid Long The `.toLongOption` function isn't available in Scala 2.12 --- modules/core/src/main/scala/sangria/schema/package.scala | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/core/src/main/scala/sangria/schema/package.scala b/modules/core/src/main/scala/sangria/schema/package.scala index ac9be011..5b77bd7c 100644 --- a/modules/core/src/main/scala/sangria/schema/package.scala +++ b/modules/core/src/main/scala/sangria/schema/package.scala @@ -2,6 +2,7 @@ package sangria import sangria.marshalling.MarshallerCapability import sangria.validation._ +import scala.util.Try /** Types that describe a GraphQL schema. * @@ -47,14 +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 s.toLongOption.isDefined => Right(s.toLong) + 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 s.toLongOption.isDefined => Right(s.toLong) + case ast.StringValue(s, _, _, _, _) if Try(s.toLong).isSuccess => Right(s.toLong) case _ => Left(LongCoercionViolation) } ) From 257adb0ba07bef23f0f94595fc96bd53a86ea375 Mon Sep 17 00:00:00 2001 From: Jakob Merrild Date: Mon, 16 Dec 2024 09:56:37 +0100 Subject: [PATCH 3/3] Format file --- .../src/test/scala/sangria/schema/CoercionSpec.scala | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/modules/core/src/test/scala/sangria/schema/CoercionSpec.scala b/modules/core/src/test/scala/sangria/schema/CoercionSpec.scala index 25abefce..5939faf3 100644 --- a/modules/core/src/test/scala/sangria/schema/CoercionSpec.scala +++ b/modules/core/src/test/scala/sangria/schema/CoercionSpec.scala @@ -83,11 +83,12 @@ class CoercionSpec extends AnyWordSpec with Matchers { .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("123234438749837964783648763284768372648723684763287")).isLeft should be(true) + LongType + .coerceInput(StringValue("123234438749837964783648763284768372648723684763287")) + .isLeft should be(true) } "BigInt" in { @@ -227,7 +228,9 @@ class CoercionSpec extends AnyWordSpec with Matchers { LongType.coerceUserInput(true).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("1232344387498237498732974982334324234325435") + .isLeft should be(true) LongType.coerceUserInput("123.0").isLeft should be(true) LongType.coerceUserInput(new Date).isLeft should be(true) }