From 8df7746f85f92b31e8fd408cd5715766a8d42b16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20W=C3=A4rnsberg?= Date: Thu, 14 Oct 2021 13:54:56 +0200 Subject: [PATCH 1/5] fix input object field null-validation --- core/src/main/scala/caliban/validation/Validator.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/scala/caliban/validation/Validator.scala b/core/src/main/scala/caliban/validation/Validator.scala index 0940a4e7e..6ad1c5147 100644 --- a/core/src/main/scala/caliban/validation/Validator.scala +++ b/core/src/main/scala/caliban/validation/Validator.scala @@ -472,7 +472,7 @@ object Validator { IO.when( inputField.defaultValue.isEmpty && inputField.`type`().kind == __TypeKind.NON_NULL && - !fields.contains(inputField.name) + fields.get(inputField.name).getOrElse(NullValue) == NullValue )( failValidation( s"Required field '${inputField.name}' on object '${inputType.name.getOrElse("?")}' was not provided.", From 18f049206f10f98a7560afcbd01edd2c999ddc29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20W=C3=A4rnsberg?= Date: Thu, 14 Oct 2021 13:55:29 +0200 Subject: [PATCH 2/5] add test --- .../caliban/validation/InputObjectSpec.scala | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 core/src/test/scala/caliban/validation/InputObjectSpec.scala diff --git a/core/src/test/scala/caliban/validation/InputObjectSpec.scala b/core/src/test/scala/caliban/validation/InputObjectSpec.scala new file mode 100644 index 000000000..a78112e05 --- /dev/null +++ b/core/src/test/scala/caliban/validation/InputObjectSpec.scala @@ -0,0 +1,36 @@ +package caliban.validation + +import caliban.CalibanError +import caliban.GraphQL._ +import caliban.Macros.gqldoc +import caliban.RootResolver +import caliban.schema.Annotations.GQLDefault +import zio.test.Assertion._ +import zio.test._ +import zio.test.environment.TestEnvironment + +import java.util.UUID + +object InputObjectSpec extends DefaultRunnableSpec { + override def spec: ZSpec[TestEnvironment, Any] = + suite("InputObjectSpect")( + testM("fails is a non-null field on an input object is null") { + val query = + """query { + | query(input: {string: null}) + |}""".stripMargin + + case class TestInputObject(string: String) + case class TestInput(input: TestInputObject) + case class Query(query: TestInput => String) + val gql = graphQL(RootResolver(Query(_.input.string))) + + for { + int <- gql.interpreter + res <- int.execute(query) + } yield assert(res.errors.headOption)( + isSome((isSubtype[CalibanError.ValidationError](anything))) + ) + } + ) +} From 2d1a494b2f01496b444fd8524893b7e6483354f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20W=C3=A4rnsberg?= Date: Thu, 14 Oct 2021 13:56:41 +0200 Subject: [PATCH 3/5] cleanup imports --- core/src/test/scala/caliban/validation/InputObjectSpec.scala | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core/src/test/scala/caliban/validation/InputObjectSpec.scala b/core/src/test/scala/caliban/validation/InputObjectSpec.scala index a78112e05..1e01a07ed 100644 --- a/core/src/test/scala/caliban/validation/InputObjectSpec.scala +++ b/core/src/test/scala/caliban/validation/InputObjectSpec.scala @@ -2,15 +2,11 @@ package caliban.validation import caliban.CalibanError import caliban.GraphQL._ -import caliban.Macros.gqldoc import caliban.RootResolver -import caliban.schema.Annotations.GQLDefault import zio.test.Assertion._ import zio.test._ import zio.test.environment.TestEnvironment -import java.util.UUID - object InputObjectSpec extends DefaultRunnableSpec { override def spec: ZSpec[TestEnvironment, Any] = suite("InputObjectSpect")( From 3abc446dbe42cf15c50501f8bece1052e7d46523 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20W=C3=A4rnsberg?= Date: Thu, 14 Oct 2021 13:57:26 +0200 Subject: [PATCH 4/5] typo --- core/src/test/scala/caliban/validation/InputObjectSpec.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/test/scala/caliban/validation/InputObjectSpec.scala b/core/src/test/scala/caliban/validation/InputObjectSpec.scala index 1e01a07ed..78e47a532 100644 --- a/core/src/test/scala/caliban/validation/InputObjectSpec.scala +++ b/core/src/test/scala/caliban/validation/InputObjectSpec.scala @@ -10,7 +10,7 @@ import zio.test.environment.TestEnvironment object InputObjectSpec extends DefaultRunnableSpec { override def spec: ZSpec[TestEnvironment, Any] = suite("InputObjectSpect")( - testM("fails is a non-null field on an input object is null") { + testM("fails if a non-null field on an input object is null") { val query = """query { | query(input: {string: null}) From 8fe2327c18ae5dcaacbda6344fad589ceea18691 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20W=C3=A4rnsberg?= Date: Thu, 14 Oct 2021 15:40:19 +0200 Subject: [PATCH 5/5] Update core/src/test/scala/caliban/validation/InputObjectSpec.scala Co-authored-by: Pierre Ricadat --- core/src/test/scala/caliban/validation/InputObjectSpec.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/test/scala/caliban/validation/InputObjectSpec.scala b/core/src/test/scala/caliban/validation/InputObjectSpec.scala index 78e47a532..ac4755e5a 100644 --- a/core/src/test/scala/caliban/validation/InputObjectSpec.scala +++ b/core/src/test/scala/caliban/validation/InputObjectSpec.scala @@ -9,7 +9,7 @@ import zio.test.environment.TestEnvironment object InputObjectSpec extends DefaultRunnableSpec { override def spec: ZSpec[TestEnvironment, Any] = - suite("InputObjectSpect")( + suite("InputObjectSpec")( testM("fails if a non-null field on an input object is null") { val query = """query {