Skip to content

Commit e779fcd

Browse files
committed
Call scalar coerceUserInput before coerceOutput
1 parent 3f3a5d1 commit e779fcd

File tree

6 files changed

+20
-5
lines changed

6 files changed

+20
-5
lines changed

modules/core/src/main/scala/sangria/execution/FutureResolver.scala

+5-1
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,11 @@ private[execution] class FutureResolver[Ctx](
14051405
if (isUndefinedValue(value))
14061406
None
14071407
else {
1408-
val coerced = scalar.coerceOutput(value, marshaller.capabilities)
1408+
val corcedInput = scalar
1409+
.coerceUserInput(value)
1410+
// Do we need a specific exepction here?
1411+
.fold(e => throw new ValidationError(Vector(e), exceptionHandler), identity)
1412+
val coerced = scalar.coerceOutput(corcedInput, marshaller.capabilities)
14091413

14101414
if (isUndefinedValue(coerced)) {
14111415
None

modules/core/src/main/scala/sangria/schema/ResolverBasedAstSchemaBuilder.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ object ResolverBasedAstSchemaBuilder {
587587
case v: String => safe(v.toDouble, "Float", value)
588588
case _ => invalidType("Float", value)
589589
}
590-
case _ => coerced
590+
case _ => t.coerceUserInput(coerced).fold(e => invalidType(t.name, value), identity)
591591
}
592592
}
593593

modules/core/src/main/scala/sangria/schema/package.scala

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ package object schema {
9595
case i: BigInt if !i.isValidDouble => Left(BigDecimalCoercionViolation)
9696
case i: BigInt => Right(i.doubleValue)
9797
case d: Double => Right(d)
98+
case f: Float if f.isValidDouble =>
9899
case d: BigDecimal if !d.isDecimalDouble => Left(BigDecimalCoercionViolation)
99100
case d: BigDecimal => Right(d.doubleValue)
100101
case _ => Left(FloatCoercionViolation)

modules/core/src/test/scala/sangria/marshalling/IonSupportSpec.scala

+11-3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class IonSupportSpec extends AnyWordSpec with Matchers with FutureResultSupport
4040
else dateFormat.format(d),
4141
coerceUserInput = {
4242
case s: String => parseDate(s)
43+
case d: Date => Right(d)
4344
case _ => Left(DateCoercionViolation)
4445
},
4546
coerceInput = {
@@ -51,13 +52,20 @@ class IonSupportSpec extends AnyWordSpec with Matchers with FutureResultSupport
5152
val BlobType = ScalarType[Array[Byte]](
5253
"Blob",
5354
coerceOutput = (d, _) => d,
54-
coerceUserInput = _ => Left(BinaryCoercionViolation),
55-
coerceInput = _ => Left(BinaryCoercionViolation))
55+
coerceUserInput = {
56+
case bs: Array[Byte] => Right(bs)
57+
case _ => Left(BinaryCoercionViolation)
58+
},
59+
coerceInput = _ => Left(BinaryCoercionViolation)
60+
)
5661

5762
val ClobType = ScalarType[Array[Byte]](
5863
"Clob",
5964
coerceOutput = (d, _) => d,
60-
coerceUserInput = _ => Left(BinaryCoercionViolation),
65+
coerceUserInput = {
66+
case bs: Array[Byte] => Right(bs)
67+
case _ => Left(BinaryCoercionViolation)
68+
},
6169
coerceInput = _ => Left(BinaryCoercionViolation),
6270
scalarInfo = Set(IonClobScalar)
6371
)

modules/core/src/test/scala/sangria/schema/CustomScalarSpec.scala

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class CustomScalarSpec extends AnyWordSpec with Matchers {
3030
coerceOutput = (d, _) => dateFormat.format(d),
3131
coerceUserInput = {
3232
case s: String => parseDate(s)
33+
case d: Date => Right(d)
3334
case _ => Left(DateCoercionViolation)
3435
},
3536
coerceInput = {

modules/core/src/test/scala/sangria/schema/ResolverBasedAstSchemaBuilderSpec.scala

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ResolverBasedAstSchemaBuilderSpec extends AnyWordSpec with Matchers with F
3434
"UUID",
3535
coerceOutput = (v, _) => v.toString,
3636
coerceUserInput = {
37+
case uuid: UUID => Right(uuid)
3738
case s: String => parseUuid(s)
3839
case _ => Left(UUIDViolation)
3940
},

0 commit comments

Comments
 (0)