Skip to content

Commit ea740ef

Browse files
committed
1764: Fix bug with circular dependency
1 parent ecb52d3 commit ea740ef

3 files changed

Lines changed: 132 additions & 52 deletions

File tree

‎src/main/java/graphql/schema/idl/ImplementingTypesChecker.java‎

Lines changed: 84 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
import graphql.schema.idl.errors.InterfaceFieldArgumentRedefinitionError;
1919
import graphql.schema.idl.errors.InterfaceFieldRedefinitionError;
2020
import graphql.schema.idl.errors.InterfaceImplementingItselfError;
21+
import graphql.schema.idl.errors.InterfaceWithCircularImplementationHierarchyError;
2122
import graphql.schema.idl.errors.MissingInterfaceFieldArgumentsError;
2223
import graphql.schema.idl.errors.MissingInterfaceFieldError;
2324
import graphql.schema.idl.errors.MissingTransitiveInterfaceError;
2425

2526
import java.util.Collection;
26-
import java.util.HashSet;
27+
import java.util.LinkedHashSet;
2728
import java.util.List;
2829
import java.util.Map;
2930
import java.util.Optional;
@@ -34,6 +35,7 @@
3435
import java.util.stream.Collectors;
3536

3637
import static java.util.stream.Collectors.toList;
38+
import static java.util.stream.Collectors.toSet;
3739

3840
/**
3941
* A support class to help break up the large SchemaTypeChecker class. This handles
@@ -48,75 +50,82 @@ void checkImplementingTypes(List<GraphQLError> errors, TypeDefinitionRegistry ty
4850
// objects
4951
List<ObjectTypeDefinition> objectTypes = filterTo(typesMap, ObjectTypeDefinition.class);
5052
objectTypes.forEach(type -> {
51-
checkImplementingType(errors, typeRegistry, type, "object");
53+
checkImplementingType("object", errors, typeRegistry, type);
5254
});
5355

54-
5556
List<ObjectTypeExtensionDefinition> objectExtensions = typeRegistry.objectTypeExtensions().values()
5657
.stream().flatMap(Collection::stream).collect(toList());
5758

5859
objectExtensions.forEach(type -> {
59-
checkImplementingType(errors, typeRegistry, type, "object extension");
60+
checkImplementingType("object extension", errors, typeRegistry, type);
6061
});
6162

6263
// interfaces
6364
List<InterfaceTypeDefinition> interfacesTypes = filterTo(typesMap, InterfaceTypeDefinition.class);
6465
interfacesTypes.forEach(type -> {
65-
checkImplementingType(errors, typeRegistry, type, "interface");
66+
checkImplementingType("interface", errors, typeRegistry, type);
6667
});
6768

6869
List<InterfaceTypeExtensionDefinition> interfaceExtensions = typeRegistry.interfaceTypeExtensions().values()
6970
.stream().flatMap(Collection::stream).collect(toList());
7071

7172
interfaceExtensions.forEach(type -> {
72-
checkImplementingType(errors, typeRegistry, type, "interface extension");
73+
checkImplementingType("interface extension", errors, typeRegistry, type);
7374
});
7475
}
7576

7677
private void checkImplementingType(
78+
String typeOfType,
7779
List<GraphQLError> errors,
7880
TypeDefinitionRegistry typeRegistry,
79-
ImplementingTypeDefinition type,
80-
String typeName) {
81+
ImplementingTypeDefinition type) {
8182

8283
List<Type> implementsTypes = type.getImplements();
8384

84-
implementsTypes.forEach(checkInterfaceIsImplemented(typeName, typeRegistry, errors, type));
85+
implementsTypes.forEach(checkInterfaceIsImplemented(typeOfType, typeRegistry, errors, type));
86+
87+
Set<String> interfacesToImplement = findInterfacesToImplement(type, typeRegistry);
88+
Set<String> implementedInterfaces = findImplementedInterfaces(type, typeRegistry);
8589

8690
if (type instanceof InterfaceTypeDefinition) {
87-
final boolean implementItself = checkInterfaceDoesntImplementItself(typeName, typeRegistry, errors, (InterfaceTypeDefinition) type, implementsTypes);
88-
// Don't try to navigate the hierarchy if the interface implements itself, as this would result in an infinite loop
89-
if (!implementItself) {
90-
checkTransitiveInterfacesAreDeclared(typeName, typeRegistry, errors, type, implementsTypes);
91+
if (!checkInterfaceImplementItself(typeOfType, errors, type, implementedInterfaces)) {
92+
if (!checkInterfaceHasCircularHierarchy(typeOfType, errors, interfacesToImplement, type)) {
93+
checkTransitiveInterfacesAreDeclared(typeOfType, errors, type, interfacesToImplement, implementedInterfaces);
94+
}
9195
}
9296
} else {
93-
checkTransitiveInterfacesAreDeclared(typeName, typeRegistry, errors, type, implementsTypes);
97+
checkTransitiveInterfacesAreDeclared(typeOfType, errors, type, interfacesToImplement, implementedInterfaces);
9498
}
9599
}
96100

97-
private boolean checkInterfaceDoesntImplementItself(
101+
private boolean checkInterfaceHasCircularHierarchy(
98102
String typeOfType,
99-
TypeDefinitionRegistry typeRegistry,
100103
List<GraphQLError> errors,
101-
InterfaceTypeDefinition typeDefinition,
102-
List<Type> implementsInterfaces
103-
) {
104-
Set<String> implementsInterfacesNames = implementsInterfaces.stream()
105-
.map(t -> toInterfaceTypeDefinition(t, typeRegistry))
106-
.filter(Optional::isPresent)
107-
.map(Optional::get)
108-
.map(TypeDefinition::getName)
109-
.collect(Collectors.toSet());
104+
Set<String> interfacesToImplement,
105+
ImplementingTypeDefinition type) {
106+
107+
if (interfacesToImplement.contains(type.getName())) {
108+
errors.add(new InterfaceWithCircularImplementationHierarchyError(typeOfType, type, interfacesToImplement));
109+
return true;
110+
}
111+
112+
return false;
113+
}
110114

115+
private boolean checkInterfaceImplementItself(
116+
String typeOfType,
117+
List<GraphQLError> errors,
118+
ImplementingTypeDefinition typeDefinition,
119+
Set<String> implementedInterfacesNames
120+
) {
111121
String thisInterfaceName = typeDefinition.getName();
112122

113-
if (implementsInterfacesNames.contains(thisInterfaceName)) {
123+
if (implementedInterfacesNames.contains(thisInterfaceName)) {
114124
errors.add(new InterfaceImplementingItselfError(typeOfType, typeDefinition));
115125
return true;
116126
}
117127

118128
return false;
119-
120129
}
121130

122131
private Consumer<? super Type> checkInterfaceIsImplemented(
@@ -161,27 +170,27 @@ private Consumer<? super Type> checkInterfaceIsImplemented(
161170
};
162171
}
163172

164-
private void checkTransitiveInterfacesAreDeclared(
165-
String typeOfType,
166-
TypeDefinitionRegistry typeRegistry,
167-
List<GraphQLError> errors,
168-
ImplementingTypeDefinition typeDefinition,
169-
List<Type> implementsInterfaces
170-
) {
171-
Set<String> interfacesToBeImplemented = new HashSet<>();
172-
173-
findImplementingInterfaces(typeDefinition, typeRegistry, interfacesToBeImplemented);
173+
private Set<String> findImplementedInterfaces(ImplementingTypeDefinition typeDefinition, TypeDefinitionRegistry typeRegistry) {
174+
List<Type> implementz = typeDefinition.getImplements();
174175

175-
Set<String> implementsInterfacesNames = implementsInterfaces.stream()
176+
return implementz.stream()
176177
.map(t -> toInterfaceTypeDefinition(t, typeRegistry))
177178
.filter(Optional::isPresent)
178179
.map(Optional::get)
179180
.map(TypeDefinition::getName)
180-
.collect(Collectors.toSet());
181+
.collect(toSet());
182+
}
181183

182-
Set<String> missingInterfaces = interfacesToBeImplemented.stream()
183-
.filter(i -> !implementsInterfacesNames.contains(i))
184-
.collect(Collectors.toSet());
184+
private void checkTransitiveInterfacesAreDeclared(
185+
String typeOfType,
186+
List<GraphQLError> errors,
187+
ImplementingTypeDefinition typeDefinition,
188+
Set<String> interfaceHierarchy,
189+
Set<String> implementedInterfaces
190+
) {
191+
Set<String> missingInterfaces = interfaceHierarchy.stream()
192+
.filter(i -> !implementedInterfaces.contains(i))
193+
.collect(toSet());
185194

186195
if (!missingInterfaces.isEmpty()) {
187196
errors.add(new MissingTransitiveInterfaceError(typeOfType, typeDefinition, missingInterfaces));
@@ -230,17 +239,31 @@ private <T> BinaryOperator<T> mergeFirstValue() {
230239
return (v1, v2) -> v1;
231240
}
232241

233-
private void findImplementingInterfaces(ImplementingTypeDefinition type, TypeDefinitionRegistry typeRegistry, Set<String> allInterfaces) {
234-
final List<Type> implementsTypes = type.getImplements();
242+
private Set<String> findInterfacesToImplement(ImplementingTypeDefinition type, TypeDefinitionRegistry typeRegistry) {
243+
Set<String> interfaceHierarchy = new LinkedHashSet<>();
235244

236-
implementsTypes.stream()
237-
.map(t -> toInterfaceTypeDefinition(t, typeRegistry))
238-
.filter(Optional::isPresent)
239-
.map(Optional::get)
240-
.forEach(t -> {
241-
allInterfaces.add(t.getName());
242-
findImplementingInterfaces(t, typeRegistry, allInterfaces);
243-
});
245+
List<Type> implementsTypes = type.getImplements();
246+
247+
Set<InterfaceTypeDefinition> interfaces = toInterfaceTypeDefinitions(typeRegistry, implementsTypes);
248+
249+
findInterfacesToImplement(type, interfaces, typeRegistry, interfaceHierarchy);
250+
251+
return interfaceHierarchy;
252+
}
253+
254+
255+
private void findInterfacesToImplement(ImplementingTypeDefinition rootType, Set<InterfaceTypeDefinition> interfaces, TypeDefinitionRegistry typeRegistry, Set<String> allInterfaces) {
256+
allInterfaces.addAll(interfaces.stream().map(InterfaceTypeDefinition::getName).collect(toSet()));
257+
258+
if (allInterfaces.contains(rootType.getName())) {
259+
return;
260+
}
261+
262+
interfaces.stream().map(InterfaceTypeDefinition::getImplements).forEach(i -> {
263+
Set<InterfaceTypeDefinition> innerInterface = toInterfaceTypeDefinitions(typeRegistry, i);
264+
265+
findInterfacesToImplement(rootType, innerInterface, typeRegistry, allInterfaces);
266+
});
244267
}
245268

246269
private Optional<InterfaceTypeDefinition> toInterfaceTypeDefinition(Type type, TypeDefinitionRegistry typeRegistry) {
@@ -250,4 +273,13 @@ private Optional<InterfaceTypeDefinition> toInterfaceTypeDefinition(Type type, T
250273
return typeRegistry.getType(unwrapped, InterfaceTypeDefinition.class);
251274
}
252275

276+
private Set<InterfaceTypeDefinition> toInterfaceTypeDefinitions(TypeDefinitionRegistry typeRegistry, List<Type> implementsTypes) {
277+
return implementsTypes.stream()
278+
.map(t -> toInterfaceTypeDefinition(t, typeRegistry))
279+
.filter(Optional::isPresent)
280+
.map(Optional::get)
281+
.collect(toSet());
282+
}
283+
284+
253285
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package graphql.schema.idl.errors;
2+
3+
import graphql.language.ImplementingTypeDefinition;
4+
5+
import java.util.Set;
6+
7+
import static java.lang.String.format;
8+
9+
public class InterfaceWithCircularImplementationHierarchyError extends BaseError {
10+
public InterfaceWithCircularImplementationHierarchyError(String typeOfType, ImplementingTypeDefinition typeDefinition, Set<String> interfacesToImplement) {
11+
super(typeDefinition, format("The interface hierarchy in %s type '%s' %s results in a circular dependency [%s]",
12+
typeOfType, typeDefinition.getName(), lineCol(typeDefinition),
13+
typeDefinition.getName() + " -> " + String.join(" -> ", interfacesToImplement)
14+
));
15+
}
16+
}

‎src/test/groovy/graphql/InterfacesImplementingInterfacesTest.groovy‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,4 +366,36 @@ class InterfacesImplementingInterfacesTest extends Specification {
366366
def error = thrown(AssertionError)
367367
error.getMessage() ==~ ".*The interface extension type 'Image'.*does not implement the following transitive interfaces: \\[Node\\].*"
368368
}
369+
370+
def 'When hierarchy results in circular reference, then parsing fails'() {
371+
when:
372+
TestUtil.schema("""
373+
type Query {
374+
find(id: String!): Interface1
375+
}
376+
377+
interface Interface1 implements Interface3 & Interface2 {
378+
field1: String
379+
field2: String
380+
field3: String
381+
}
382+
383+
interface Interface2 implements Interface1 & Interface3 {
384+
field1: String
385+
field2: String
386+
field3: String
387+
}
388+
389+
interface Interface3 implements Interface2 & Interface1 {
390+
field1: String
391+
field2: String
392+
field3: String
393+
}
394+
395+
""")
396+
then:
397+
def error = thrown(AssertionError)
398+
println(error.getMessage())
399+
error.getMessage() ==~ ".*The interface hierarchy in interface type 'Interface1' .* results in a circular dependency.*"
400+
}
369401
}

0 commit comments

Comments
 (0)