Skip to content

Commit

Permalink
Call scalar coerceUserInput before coerceOutput
Browse files Browse the repository at this point in the history
Ref: #991
  • Loading branch information
filosganga committed Apr 14, 2023
1 parent 3f3a5d1 commit f17e172
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1405,7 +1405,11 @@ private[execution] class FutureResolver[Ctx](
if (isUndefinedValue(value))
None
else {
val coerced = scalar.coerceOutput(value, marshaller.capabilities)
val corcedInput = scalar
.coerceUserInput(value)
// Do we need a specific exepction here?
.fold(e => throw new ValidationError(Vector(e), exceptionHandler), identity)
val coerced = scalar.coerceOutput(corcedInput, marshaller.capabilities)

if (isUndefinedValue(coerced)) {
None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ object ResolverBasedAstSchemaBuilder {
case v: String => safe(v.toDouble, "Float", value)
case _ => invalidType("Float", value)
}
case _ => coerced
case _ => t.coerceUserInput(coerced).fold(e => invalidType(t.name, value), identity)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class IonSupportSpec extends AnyWordSpec with Matchers with FutureResultSupport
else dateFormat.format(d),
coerceUserInput = {
case s: String => parseDate(s)
case d: Date => Right(d)
case _ => Left(DateCoercionViolation)
},
coerceInput = {
Expand All @@ -51,13 +52,20 @@ class IonSupportSpec extends AnyWordSpec with Matchers with FutureResultSupport
val BlobType = ScalarType[Array[Byte]](
"Blob",
coerceOutput = (d, _) => d,
coerceUserInput = _ => Left(BinaryCoercionViolation),
coerceInput = _ => Left(BinaryCoercionViolation))
coerceUserInput = {
case bs: Array[Byte] => Right(bs)
case _ => Left(BinaryCoercionViolation)
},
coerceInput = _ => Left(BinaryCoercionViolation)
)

val ClobType = ScalarType[Array[Byte]](
"Clob",
coerceOutput = (d, _) => d,
coerceUserInput = _ => Left(BinaryCoercionViolation),
coerceUserInput = {
case bs: Array[Byte] => Right(bs)
case _ => Left(BinaryCoercionViolation)
},
coerceInput = _ => Left(BinaryCoercionViolation),
scalarInfo = Set(IonClobScalar)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class CustomScalarSpec extends AnyWordSpec with Matchers {
coerceOutput = (d, _) => dateFormat.format(d),
coerceUserInput = {
case s: String => parseDate(s)
case d: Date => Right(d)
case _ => Left(DateCoercionViolation)
},
coerceInput = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ResolverBasedAstSchemaBuilderSpec extends AnyWordSpec with Matchers with F
"UUID",
coerceOutput = (v, _) => v.toString,
coerceUserInput = {
case uuid: UUID => Right(uuid)
case s: String => parseUuid(s)
case _ => Left(UUIDViolation)
},
Expand Down

0 comments on commit f17e172

Please sign in to comment.