Skip to content

Commit 7e0b1c1

Browse files
authored
Fix input cycle validation for non-null lists (#4386)
1 parent 8c10974 commit 7e0b1c1

2 files changed

Lines changed: 54 additions & 11 deletions

File tree

src/main/java/graphql/schema/validation/NoUnbrokenInputCycles.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import graphql.schema.GraphQLInputObjectField;
77
import graphql.schema.GraphQLInputObjectType;
88
import graphql.schema.GraphQLInputType;
9-
import graphql.schema.GraphQLList;
109
import graphql.schema.GraphQLNonNull;
1110
import graphql.schema.GraphQLSchemaElement;
1211
import graphql.schema.GraphQLType;
@@ -63,17 +62,11 @@ private void check(GraphQLInputObjectType type, Set<GraphQLType> seen, List<Stri
6362
}
6463

6564
private GraphQLType unwrapNonNull(GraphQLNonNull type) {
66-
if (isList(type.getWrappedType())) {
67-
//we only care about [type!]! i.e. non-null lists of non-nulls
68-
GraphQLList listType = (GraphQLList) type.getWrappedType();
69-
if (isNonNull(listType.getWrappedType())) {
70-
return unwrapAll(listType.getWrappedType());
71-
} else {
72-
return type.getWrappedType();
73-
}
74-
} else {
75-
return unwrapAll(type.getWrappedType());
65+
GraphQLType wrappedType = type.getWrappedType();
66+
if (isList(wrappedType)) {
67+
return wrappedType;
7668
}
69+
return unwrapAll(wrappedType);
7770
}
7871

7972
private String getErrorMessage(List<String> path) {

src/test/groovy/graphql/schema/validation/NoUnbrokenInputCyclesTest.groovy

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package graphql.schema.validation
22

3+
import graphql.TestUtil
34
import graphql.schema.GraphQLArgument
45
import graphql.schema.GraphQLFieldDefinition
56
import graphql.schema.GraphQLInputObjectField
67
import graphql.schema.GraphQLInputObjectType
8+
import graphql.schema.idl.SchemaGenerator
9+
import graphql.schema.idl.SchemaParser
710
import graphql.util.TraverserContext
811
import spock.lang.Specification
912

@@ -43,4 +46,51 @@ class NoUnbrokenInputCyclesTest extends Specification {
4346
then:
4447
errorCollector.containsValidationError(SchemaValidationErrorType.UnbrokenInputCycle)
4548
}
49+
50+
def "input object cycles through a non-null list are allowed"() {
51+
def sdl = """
52+
input Example {
53+
self: [Example!]!
54+
value: String
55+
}
56+
57+
type Query {
58+
example(example: Example): String
59+
}
60+
"""
61+
62+
when:
63+
def registry = new SchemaParser().parse(sdl)
64+
new SchemaGenerator().makeExecutableSchema(registry, TestUtil.getMockRuntimeWiring())
65+
66+
then:
67+
noExceptionThrown()
68+
}
69+
70+
def "longer input object cycles through a non-null list are allowed"() {
71+
def sdl = """
72+
input Foo {
73+
bar: Bar!
74+
}
75+
76+
input Bar {
77+
baz: Baz!
78+
}
79+
80+
input Baz {
81+
foos: [Foo!]!
82+
}
83+
84+
type Query {
85+
foo(foo: Foo): String
86+
}
87+
"""
88+
89+
when:
90+
def registry = new SchemaParser().parse(sdl)
91+
new SchemaGenerator().makeExecutableSchema(registry, TestUtil.getMockRuntimeWiring())
92+
93+
then:
94+
noExceptionThrown()
95+
}
4696
}

0 commit comments

Comments
 (0)