Skip to content
Merged
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
16 changes: 11 additions & 5 deletions src/main/java/graphql/execution/ValuesResolverOneOfValidation.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,20 @@ static void validateOneOfInputTypes(GraphQLType type, Object inputValue, Value<?

if (isList(unwrappedNonNullType)
&& !ValuesResolverConversion.isNullValue(inputValue)
&& inputValue instanceof List
&& argumentValue instanceof ArrayValue) {
&& inputValue instanceof List) {
GraphQLType elementType = ((GraphQLList) unwrappedNonNullType).getWrappedType();
List<Object> inputList = (List<Object>) inputValue;
List<Value> argumentList = ((ArrayValue) argumentValue).getValues();

for (int i = 0; i < argumentList.size(); i++) {
validateOneOfInputTypes(elementType, inputList.get(i), argumentList.get(i), argumentName, locale);
if (argumentValue instanceof ArrayValue) {
List<Value> argumentList = ((ArrayValue) argumentValue).getValues();
for (int i = 0; i < argumentList.size(); i++) {
validateOneOfInputTypes(elementType, inputList.get(i), argumentList.get(i), argumentName, locale);
}
} else {
// a single literal coerced into a list, or a list resolved from a variable reference
for (Object inputElement : inputList) {
validateOneOfInputTypes(elementType, inputElement, argumentValue, argumentName, locale);
}
}
}

Expand Down
103 changes: 103 additions & 0 deletions src/test/groovy/graphql/execution/ValuesResolverTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,30 @@ class ValuesResolverTest extends Specification {
er.errors[0].message == "Exactly one key must be specified for OneOf type 'OneOf'."
}

def "can validate list input oneOf fields when the argument is not an array literal"() {
def sdl = '''
input OneOf @oneOf { a: Int, b: Int }
type Query { q(oneofs: [OneOf]): Boolean }
'''

DataFetcher q = { env -> return !env.getArguments().isEmpty() }
def graphQL = TestUtil.graphQL(sdl, [Query: [q: q]]).build()

when: "a single object literal is coerced into the list"
def er = graphQL.execute('{ q(oneofs: {a: null}) }')

then:
er.errors.size() == 1
er.errors[0].message.contains("OneOf type field 'OneOf.a' must be non-null.")

when: "the list comes from the default value of a variable"
er = graphQL.execute('query ($oneofs: [OneOf] = [{a: null}]) { q(oneofs: $oneofs) }')

then:
er.errors.size() == 1
er.errors[0].message.contains("OneOf type field 'OneOf.a' must be non-null.")
}


class Person {
def name = ""
Expand Down Expand Up @@ -787,6 +811,85 @@ class ValuesResolverTest extends Specification {

}

def "getArgumentValues: invalid oneOf list input without an array literal because element contains null value - #testCase"() {
given: "schema defining input object"
def inputObjectType = newInputObject()
.name("oneOfInputObject")
.withAppliedDirective(Directives.OneOfDirective.toAppliedDirective())
.field(newInputObjectField()
.name("a")
.type(GraphQLString)
.build())
.field(newInputObjectField()
.name("b")
.type(GraphQLInt)
.build())
.build()

when:
def argument = new Argument("arg", inputValue)
def fieldArgumentList = newArgument().name("arg").type(list(inputObjectType)).build()
ValuesResolver.getArgumentValues([fieldArgumentList], [argument], variables, graphQLContext, locale)

then:
def e = thrown(OneOfNullValueException)
e.message == "OneOf type field 'oneOfInputObject.a' must be non-null."

where:

testCase | inputValue | variables

'{ a: null } coerced to a single element list'
| buildObjectLiteral([
a: NullValue.of()
]) | CoercedVariables.emptyVariables()

'$var [{ a: null }]'
| VariableReference.of("var")
| CoercedVariables.of("var": [[a: null]])

}

def "getArgumentValues: invalid oneOf list input without an array literal because element contains duplicate key - #testCase"() {
given: "schema defining input object"
def inputObjectType = newInputObject()
.name("oneOfInputObject")
.withAppliedDirective(Directives.OneOfDirective.toAppliedDirective())
.field(newInputObjectField()
.name("a")
.type(GraphQLString)
.build())
.field(newInputObjectField()
.name("b")
.type(GraphQLInt)
.build())
.build()

when:
def argument = new Argument("arg", inputValue)
def fieldArgumentList = newArgument().name("arg").type(list(inputObjectType)).build()
ValuesResolver.getArgumentValues([fieldArgumentList], [argument], variables, graphQLContext, locale)

then:
def e = thrown(OneOfTooManyKeysException)
e.message == "Exactly one key must be specified for OneOf type 'oneOfInputObject'."

where:

testCase | inputValue | variables

'{ a: "abc", b: 123 } coerced to a single element list'
| buildObjectLiteral([
a: StringValue.of("abc"),
b: IntValue.of(123)
]) | CoercedVariables.emptyVariables()

'$var [{ a: "abc", b: 123 }]'
| VariableReference.of("var")
| CoercedVariables.of("var": [[a: "abc", b: 123]])

}

def "getArgumentValues: invalid oneOf non-null list input because element contains duplicate key - #testCase"() {
given: "schema defining input object"
def inputObjectType = newInputObject()
Expand Down
Loading