Skip to content

Commit 3c4808d

Browse files
author
BURJA Lucian
committed
Fix regression: if null is passed as parameter to a resolver method, use Java default value
1 parent c694d1a commit 3c4808d

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/main/kotlin/graphql/kickstart/tools/resolver/MethodFieldResolver.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ internal class MethodFieldResolver(
8080
return@add Optional.empty<Any>()
8181
}
8282

83-
if (value != null && shouldValueBeConverted(value, definition, parameterType, environment)) {
83+
if (value == null || shouldValueBeConverted(value, definition, parameterType, environment)) {
8484
return@add mapper.convertValue(value, object : TypeReference<Any>() {
8585
override fun getType() = parameterType
8686
})
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package graphql.kickstart.tools;
2+
3+
import graphql.ExecutionInput;
4+
import graphql.ExecutionResult;
5+
import graphql.GraphQL;
6+
import graphql.schema.GraphQLSchema;
7+
import org.junit.Test;
8+
9+
import java.util.Map;
10+
11+
import static org.junit.Assert.assertEquals;
12+
import static org.junit.Assert.assertTrue;
13+
14+
public class ResolverMethodsTest {
15+
// Note: don't convert this code to Kotlin or Groovy, since it's quite important that the
16+
// resolver method is defined with an argument of primitive type, like 'boolean', not 'Boolean':
17+
// String testOmittedBoolean(boolean value1, Boolean value2)
18+
@Test
19+
public void testOmittedBooleanArgument() {
20+
// In this schema, the 'value1' argument is optional, but the Java resolver defines it as 'boolean'
21+
// Instead of failing with an error, we expect the argument to be set to the Java default (i.e. false for booleans)
22+
GraphQLSchema schema = SchemaParser.newParser()
23+
.schemaString("" +
24+
"type Query {" +
25+
" testOmittedBoolean(value1: Boolean, value2: Boolean): String" +
26+
"}")
27+
.resolvers(new Resolver())
28+
.build()
29+
.makeExecutableSchema();
30+
31+
GraphQL gql = GraphQL.newGraphQL(schema).build();
32+
33+
ExecutionResult result = gql
34+
.execute(ExecutionInput.newExecutionInput()
35+
.query("" +
36+
"query { " +
37+
" testOmittedBoolean" +
38+
"}")
39+
.context(new Object())
40+
.root(new Object()));
41+
42+
assertTrue(result.getErrors().isEmpty());
43+
assertEquals("false,null", ((Map<?, ?>) result.getData()).get("testOmittedBoolean"));
44+
}
45+
46+
static class Resolver implements GraphQLQueryResolver {
47+
@SuppressWarnings("unused")
48+
public String testOmittedBoolean(boolean value1, Boolean value2) {
49+
return value1 + "," + value2;
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)