Skip to content

Commit cdd0cbc

Browse files
committed
non null directive args with defaults can be missing
1 parent 5e9eb16 commit cdd0cbc

2 files changed

Lines changed: 63 additions & 18 deletions

File tree

src/main/java/graphql/validation/rules/ProvidedNonNullArguments.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,15 @@ public void checkField(Field field) {
3333

3434
for (GraphQLArgument graphQLArgument : fieldDef.getArguments()) {
3535
Argument argument = argumentMap.get(graphQLArgument.getName());
36-
if (argument == null
37-
&& (isNonNull(graphQLArgument.getType()))
38-
&& (graphQLArgument.getDefaultValue() == null)) {
36+
boolean nonNullType = isNonNull(graphQLArgument.getType());
37+
boolean noDefaultValue = graphQLArgument.getDefaultValue() == null;
38+
if (argument == null && nonNullType && noDefaultValue) {
3939
String message = String.format("Missing field argument %s", graphQLArgument.getName());
4040
addError(ValidationErrorType.MissingFieldArgument, field.getSourceLocation(), message);
4141
}
4242
}
4343
}
4444

45-
private Map<String, Argument> argumentMap(List<Argument> arguments) {
46-
Map<String, Argument> result = new LinkedHashMap<>();
47-
for (Argument argument : arguments) {
48-
result.put(argument.getName(), argument);
49-
}
50-
return result;
51-
}
52-
5345

5446
@Override
5547
public void checkDirective(Directive directive, List<Node> ancestors) {
@@ -59,11 +51,20 @@ public void checkDirective(Directive directive, List<Node> ancestors) {
5951

6052
for (GraphQLArgument graphQLArgument : graphQLDirective.getArguments()) {
6153
Argument argument = argumentMap.get(graphQLArgument.getName());
62-
if (argument == null
63-
&& (isNonNull(graphQLArgument.getType()))) {
54+
boolean nonNullType = isNonNull(graphQLArgument.getType());
55+
boolean noDefaultValue = graphQLArgument.getDefaultValue() == null;
56+
if (argument == null && nonNullType && noDefaultValue) {
6457
String message = String.format("Missing directive argument %s", graphQLArgument.getName());
6558
addError(ValidationErrorType.MissingDirectiveArgument, directive.getSourceLocation(), message);
6659
}
6760
}
6861
}
62+
63+
private Map<String, Argument> argumentMap(List<Argument> arguments) {
64+
Map<String, Argument> result = new LinkedHashMap<>();
65+
for (Argument argument : arguments) {
66+
result.put(argument.getName(), argument);
67+
}
68+
return result;
69+
}
6970
}

src/test/groovy/graphql/validation/rules/ProvidedNonNullArgumentsTest.groovy

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ class ProvidedNonNullArgumentsTest extends Specification {
2121
ValidationErrorCollector errorCollector = new ValidationErrorCollector()
2222
ProvidedNonNullArguments providedNonNullArguments = new ProvidedNonNullArguments(validationContext, errorCollector)
2323

24-
def "not provided field argument"() {
24+
def "not provided and not defaulted non null field argument"() {
2525
given:
26-
def fieldArg = GraphQLArgument.newArgument().name("arg").type(GraphQLNonNull.nonNull(GraphQLString))
26+
def fieldArg = GraphQLArgument.newArgument().name("arg")
27+
.type(GraphQLNonNull.nonNull(GraphQLString))
2728
def fieldDef = GraphQLFieldDefinition.newFieldDefinition()
2829
.name("field")
2930
.type(GraphQLString)
@@ -40,10 +41,32 @@ class ProvidedNonNullArgumentsTest extends Specification {
4041
errorCollector.containsValidationError(ValidationErrorType.MissingFieldArgument)
4142
}
4243

44+
def "not provided and but defaulted non null field argument"() {
45+
given:
46+
def fieldArg = GraphQLArgument.newArgument().name("arg")
47+
.type(GraphQLNonNull.nonNull(GraphQLString))
48+
.defaultValue("defaultVal")
49+
def fieldDef = GraphQLFieldDefinition.newFieldDefinition()
50+
.name("field")
51+
.type(GraphQLString)
52+
.argument(fieldArg)
53+
.build()
54+
validationContext.getFieldDef() >> fieldDef
55+
56+
def field = new Field("field")
57+
58+
when:
59+
providedNonNullArguments.checkField(field)
60+
61+
then:
62+
errorCollector.getErrors().isEmpty()
63+
}
64+
4365

4466
def "all field arguments are provided"() {
4567
given:
46-
def fieldArg = GraphQLArgument.newArgument().name("arg").type(GraphQLNonNull.nonNull(GraphQLString))
68+
def fieldArg = GraphQLArgument.newArgument().name("arg")
69+
.type(GraphQLNonNull.nonNull(GraphQLString))
4770
def fieldDef = GraphQLFieldDefinition.newFieldDefinition()
4871
.name("field")
4972
.type(GraphQLString)
@@ -60,9 +83,10 @@ class ProvidedNonNullArgumentsTest extends Specification {
6083
errorCollector.getErrors().isEmpty()
6184
}
6285

63-
def "not provided directive argument"() {
86+
def "not provided not defaulted directive argument"() {
6487
given:
65-
def directiveArg = GraphQLArgument.newArgument().name("arg").type(GraphQLNonNull.nonNull(GraphQLString))
88+
def directiveArg = GraphQLArgument.newArgument()
89+
.name("arg").type(GraphQLNonNull.nonNull(GraphQLString))
6690
def graphQLDirective = GraphQLDirective.newDirective()
6791
.name("directive")
6892
.argument(directiveArg)
@@ -78,6 +102,26 @@ class ProvidedNonNullArgumentsTest extends Specification {
78102
errorCollector.containsValidationError(ValidationErrorType.MissingDirectiveArgument)
79103
}
80104

105+
def "not provided but defaulted directive argument"() {
106+
given:
107+
def directiveArg = GraphQLArgument.newArgument()
108+
.name("arg").type(GraphQLNonNull.nonNull(GraphQLString))
109+
.defaultValue("defaultVal")
110+
def graphQLDirective = GraphQLDirective.newDirective()
111+
.name("directive")
112+
.argument(directiveArg)
113+
.build()
114+
validationContext.getDirective() >> graphQLDirective
115+
116+
def directive = new Directive("directive")
117+
118+
when:
119+
providedNonNullArguments.checkDirective(directive, [])
120+
121+
then:
122+
errorCollector.getErrors().isEmpty()
123+
}
124+
81125

82126
def "all directive arguments are provided"() {
83127
given:

0 commit comments

Comments
 (0)