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

Call scalar coerceUserInput before coerceOutput #993

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
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)
Comment on lines +1408 to +1412
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing an exception here because the whole block is protected by try/catch


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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try to understand:
coerceUserInput is now called twice:

  • the first time to parse the user input as String into a UUID
  • the second time to confirm that a UUID is a valid value
    ?

Copy link
Contributor Author

@filosganga filosganga Apr 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it is not called, twice, but I have spent a bit of time looking at Apollo implementation, and I think, I would just need to change the scalars rather than Sangria.

In Apollo the scalar are represented by 3 functions:

  • serialize it is the same as coerceOutput, it coerces the scalar to a JSON representation
  • parseLiteral it is the same as coerceInput that is bit misleading, it basically coerces and hardcoded value into the scalar type
  • parseValue it is the same as coerceUserInput that basically coerces the values coming from the users via not hardcoded variables.

When you have an untyped underlying model, like JSON, the serialize function should take care also of parsing the type from the underlying model.

I will investigate more about that and maybe drop this PR in favour of a documentation one.

case s: String => parseUuid(s)
case _ => Left(UUIDViolation)
},
Expand Down