From 6f286122cd055add1e5867cb92ea199b76581f52 Mon Sep 17 00:00:00 2001 From: Marc-Andre Giroux Date: Sun, 1 Nov 2026 11:49:59 -0500 Subject: [PATCH 1/2] Avoid TypeInfo allocations and repeated lookups in isSubTypeOf --- .../schema/idl/TypeDefinitionRegistry.java | 93 ++++++++++++------- 1 file changed, 59 insertions(+), 34 deletions(-) diff --git a/src/main/java/graphql/schema/idl/TypeDefinitionRegistry.java b/src/main/java/graphql/schema/idl/TypeDefinitionRegistry.java index 9fb678eb39..165e78ea4a 100644 --- a/src/main/java/graphql/schema/idl/TypeDefinitionRegistry.java +++ b/src/main/java/graphql/schema/idl/TypeDefinitionRegistry.java @@ -11,6 +11,8 @@ import graphql.language.InputObjectTypeExtensionDefinition; import graphql.language.InterfaceTypeDefinition; import graphql.language.InterfaceTypeExtensionDefinition; +import graphql.language.ListType; +import graphql.language.NonNullType; import graphql.language.ObjectTypeDefinition; import graphql.language.ObjectTypeExtensionDefinition; import graphql.language.OperationTypeDefinition; @@ -40,7 +42,6 @@ import java.util.Map; import java.util.Optional; import java.util.function.Function; -import java.util.stream.Stream; import static graphql.Assert.assertNotNull; import static graphql.schema.idl.SchemaExtensionsChecker.defineOperationDefs; @@ -796,6 +797,10 @@ public boolean isPossibleType(Type abstractType, Type possibleType) { } TypeDefinition possibleTypeDef = assertNotNull(getTypeOrNull(possibleType)); TypeDefinition abstractTypeDef = assertNotNull(getTypeOrNull(abstractType)); + return isPossibleTypeDef(abstractTypeDef, possibleTypeDef); + } + + private boolean isPossibleTypeDef(TypeDefinition abstractTypeDef, TypeDefinition possibleTypeDef) { if (abstractTypeDef instanceof UnionTypeDefinition) { return isUnionMember((UnionTypeDefinition) abstractTypeDef, possibleTypeDef); } @@ -807,12 +812,25 @@ public boolean isPossibleType(Type abstractType, Type possibleType) { private boolean implementsInterface( ImplementingTypeDefinition implementingType, InterfaceTypeDefinition targetInterface) { - return Stream.concat( - Stream.of(implementingType), - getImplementingTypeExtensions(implementingType).stream()) - .flatMap(type -> type.getImplements().stream()) - .map(TypeInfo::typeName) - .anyMatch(targetInterface.getName()::equals); + String targetName = targetInterface.getName(); + if (containsTypeNamed(implementingType.getImplements(), targetName)) { + return true; + } + for (ImplementingTypeDefinition extension : getImplementingTypeExtensions(implementingType)) { + if (containsTypeNamed(extension.getImplements(), targetName)) { + return true; + } + } + return false; + } + + private static boolean containsTypeNamed(List types, String name) { + for (Type type : types) { + if (TypeInfo.getTypeName(type).getName().equals(name)) { + return true; + } + } + return false; } private List> getImplementingTypeExtensions( @@ -824,12 +842,16 @@ private List> getImplementingTypeExtensi } private boolean isUnionMember(UnionTypeDefinition unionType, TypeDefinition possibleType) { - return Stream.concat( - unionType.getMemberTypes().stream(), - unionTypeExtensions.getOrDefault(unionType.getName(), List.of()).stream() - .flatMap(extension -> extension.getMemberTypes().stream())) - .map(TypeInfo::typeName) - .anyMatch(possibleType.getName()::equals); + String possibleName = possibleType.getName(); + if (containsTypeNamed(unionType.getMemberTypes(), possibleName)) { + return true; + } + for (UnionTypeExtensionDefinition extension : unionTypeExtensions.getOrDefault(unionType.getName(), List.of())) { + if (containsTypeNamed(extension.getMemberTypes(), possibleName)) { + return true; + } + } + return false; } /** @@ -840,50 +862,53 @@ private boolean isUnionMember(UnionTypeDefinition unionType, TypeDefinition p * * @return true if maybeSubType is covariant or equal to superType */ - @SuppressWarnings("SimplifiableIfStatement") public boolean isSubTypeOf(Type maybeSubType, Type superType) { - TypeInfo maybeSubTypeInfo = TypeInfo.typeInfo(maybeSubType); - TypeInfo superTypeInfo = TypeInfo.typeInfo(superType); - // Equivalent type is a valid subtype - if (maybeSubTypeInfo.equals(superTypeInfo)) { + if (hasSameOuterWrappingAndName(maybeSubType, superType)) { return true; } - // If superType is non-null, maybeSubType must also be non-null. - if (superTypeInfo.isNonNull()) { - if (maybeSubTypeInfo.isNonNull()) { - return isSubTypeOf(maybeSubTypeInfo.unwrapOneType(), superTypeInfo.unwrapOneType()); + if (superType instanceof NonNullType) { + if (maybeSubType instanceof NonNullType) { + return isSubTypeOf(((NonNullType) maybeSubType).getType(), ((NonNullType) superType).getType()); } return false; } - if (maybeSubTypeInfo.isNonNull()) { + if (maybeSubType instanceof NonNullType) { // If superType is nullable, maybeSubType may be non-null or nullable. - return isSubTypeOf(maybeSubTypeInfo.unwrapOneType(), superType); + return isSubTypeOf(((NonNullType) maybeSubType).getType(), superType); } // If superType type is a list, maybeSubType type must also be a list. - if (superTypeInfo.isList()) { - if (maybeSubTypeInfo.isList()) { - return isSubTypeOf(maybeSubTypeInfo.unwrapOneType(), superTypeInfo.unwrapOneType()); + if (superType instanceof ListType) { + if (maybeSubType instanceof ListType) { + return isSubTypeOf(((ListType) maybeSubType).getType(), ((ListType) superType).getType()); } return false; } - if (maybeSubTypeInfo.isList()) { + if (maybeSubType instanceof ListType) { // If superType is not a list, maybeSubType must also be not a list. return false; } // If superType type is an abstract type, maybeSubType type may be a currently // possible object type. - if (isInterfaceOrUnion(superType) && - isObjectTypeOrInterface(maybeSubType) && - isPossibleType(superType, maybeSubType)) { - return true; + TypeDefinition superTypeDef = getTypeOrNull(superType); + if (!(superTypeDef instanceof UnionTypeDefinition || superTypeDef instanceof InterfaceTypeDefinition)) { + return false; + } + TypeDefinition maybeSubTypeDef = getTypeOrNull(maybeSubType); + if (!(maybeSubTypeDef instanceof ObjectTypeDefinition || maybeSubTypeDef instanceof InterfaceTypeDefinition)) { + return false; } + return isPossibleTypeDef(superTypeDef, maybeSubTypeDef); + } - // Otherwise, the child type is not a valid subtype of the parent type. - return false; + // mirrors TypeInfo.equals, which only compares the outermost wrapper and the innermost name + private static boolean hasSameOuterWrappingAndName(Type a, Type b) { + return (a instanceof NonNullType) == (b instanceof NonNullType) + && (a instanceof ListType) == (b instanceof ListType) + && TypeInfo.getTypeName(a).getName().equals(TypeInfo.getTypeName(b).getName()); } } From 912ed916d614894241098cc239c0366f113da6ef Mon Sep 17 00:00:00 2001 From: Marc-Andre Giroux Date: Mon, 2 Nov 2026 10:22:47 -0500 Subject: [PATCH 2/2] Fix isSubTypeOf treating types with different inner wrapping as equivalent The equivalence shortcut relied on TypeInfo.equals, which only compared the outermost wrapper and the innermost name, so [String] was a subtype of [String!] and [[String]] of [String]. Compare names only once both sides are unwrapped, and fix TypeInfo.equals/hashCode to compare the full wrapper chain. --- .../schema/idl/TypeDefinitionRegistry.java | 16 ++++------ .../java/graphql/schema/idl/TypeInfo.java | 25 ++++++++++++---- .../schema/idl/SchemaTypeCheckerTest.groovy | 29 +++++++++++++++++++ .../idl/TypeDefinitionRegistryTest.groovy | 8 +++++ .../graphql/schema/idl/TypeInfoTest.groovy | 5 ++++ 5 files changed, 67 insertions(+), 16 deletions(-) diff --git a/src/main/java/graphql/schema/idl/TypeDefinitionRegistry.java b/src/main/java/graphql/schema/idl/TypeDefinitionRegistry.java index 165e78ea4a..07d2673b2e 100644 --- a/src/main/java/graphql/schema/idl/TypeDefinitionRegistry.java +++ b/src/main/java/graphql/schema/idl/TypeDefinitionRegistry.java @@ -863,10 +863,6 @@ private boolean isUnionMember(UnionTypeDefinition unionType, TypeDefinition p * @return true if maybeSubType is covariant or equal to superType */ public boolean isSubTypeOf(Type maybeSubType, Type superType) { - if (hasSameOuterWrappingAndName(maybeSubType, superType)) { - return true; - } - // If superType is non-null, maybeSubType must also be non-null. if (superType instanceof NonNullType) { if (maybeSubType instanceof NonNullType) { @@ -891,6 +887,11 @@ public boolean isSubTypeOf(Type maybeSubType, Type superType) { return false; } + // Equivalent type is a valid subtype + if (typeName(maybeSubType).equals(typeName(superType))) { + return true; + } + // If superType type is an abstract type, maybeSubType type may be a currently // possible object type. TypeDefinition superTypeDef = getTypeOrNull(superType); @@ -904,11 +905,4 @@ public boolean isSubTypeOf(Type maybeSubType, Type superType) { return isPossibleTypeDef(superTypeDef, maybeSubTypeDef); } - // mirrors TypeInfo.equals, which only compares the outermost wrapper and the innermost name - private static boolean hasSameOuterWrappingAndName(Type a, Type b) { - return (a instanceof NonNullType) == (b instanceof NonNullType) - && (a instanceof ListType) == (b instanceof ListType) - && TypeInfo.getTypeName(a).getName().equals(TypeInfo.getTypeName(b).getName()); - } - } diff --git a/src/main/java/graphql/schema/idl/TypeInfo.java b/src/main/java/graphql/schema/idl/TypeInfo.java index 65a6c0e5a1..bff3903f02 100644 --- a/src/main/java/graphql/schema/idl/TypeInfo.java +++ b/src/main/java/graphql/schema/idl/TypeInfo.java @@ -10,6 +10,7 @@ import java.util.ArrayDeque; import java.util.Deque; +import java.util.Iterator; import java.util.Objects; import static graphql.Assert.assertNotNull; @@ -174,17 +175,31 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; TypeInfo typeInfo = (TypeInfo) o; - return isNonNull() == typeInfo.isNonNull() && - isList() == typeInfo.isList() && - Objects.equals(typeName.getName(), typeInfo.typeName.getName()); + return Objects.equals(typeName.getName(), typeInfo.typeName.getName()) && + sameDecoration(decoration, typeInfo.decoration); + } + + private static boolean sameDecoration(Deque> a, Deque> b) { + if (a.size() != b.size()) { + return false; + } + Iterator> itA = a.iterator(); + Iterator> itB = b.iterator(); + while (itA.hasNext()) { + if (itA.next() != itB.next()) { + return false; + } + } + return true; } @Override public int hashCode() { int result = 1; result = 31 * result + Objects.hashCode(typeName.getName()); - result = 31 * result + Boolean.hashCode(isNonNull()); - result = 31 * result + Boolean.hashCode(isList()); + for (Class wrapper : decoration) { + result = 31 * result + wrapper.hashCode(); + } return result; } diff --git a/src/test/groovy/graphql/schema/idl/SchemaTypeCheckerTest.groovy b/src/test/groovy/graphql/schema/idl/SchemaTypeCheckerTest.groovy index fb8ec0b609..2ffab49666 100644 --- a/src/test/groovy/graphql/schema/idl/SchemaTypeCheckerTest.groovy +++ b/src/test/groovy/graphql/schema/idl/SchemaTypeCheckerTest.groovy @@ -12,6 +12,7 @@ import graphql.schema.TypeResolver import graphql.schema.idl.errors.DirectiveIllegalArgumentTypeError import graphql.schema.idl.errors.DirectiveIllegalLocationError import graphql.schema.idl.errors.DirectiveUndeclaredError +import graphql.schema.idl.errors.InterfaceFieldRedefinitionError import graphql.schema.idl.errors.MissingTypeError import graphql.schema.idl.errors.NonUniqueNameError import graphql.schema.idl.errors.QueryOperationMissingError @@ -595,6 +596,34 @@ class SchemaTypeCheckerTest extends Specification { result.get(0).getMessage().contains("has tried to redefine field 'fieldA' defined via interface 'InterfaceType'") } + def "test object when redefining interface field with different inner list wrapping"() { + + def spec = """ + interface InterfaceType { + nullability : [String!] + depth : [String] + } + + type BaseType implements InterfaceType { + nullability : [String] + depth : [[String]] + } + + schema { + query : BaseType + } + """ + + def result = check(spec) + + expect: + + result.size() == 2 + result.every { it instanceof InterfaceFieldRedefinitionError } + result.any { it.getMessage().contains("has tried to redefine field 'nullability' defined via interface 'InterfaceType'") } + result.any { it.getMessage().contains("has tried to redefine field 'depth' defined via interface 'InterfaceType'") } + } + def "test type extension when redefining interface field"() { def spec = """ diff --git a/src/test/groovy/graphql/schema/idl/TypeDefinitionRegistryTest.groovy b/src/test/groovy/graphql/schema/idl/TypeDefinitionRegistryTest.groovy index 6ec0d9d85a..96ede99dd8 100644 --- a/src/test/groovy/graphql/schema/idl/TypeDefinitionRegistryTest.groovy +++ b/src/test/groovy/graphql/schema/idl/TypeDefinitionRegistryTest.groovy @@ -628,6 +628,14 @@ class TypeDefinitionRegistryTest extends Specification { registry.isSubTypeOf(listType(nonNullType(listType(type("Canine")))), listType(nonNullType(listType(type("Mammal"))))) !registry.isSubTypeOf(listType(nonNullType(listType(type("Turtle")))), listType(nonNullType(listType(type("Mammal"))))) + // inner wrappers must match, not just the outermost one + registry.isSubTypeOf(listType(nonNullType("Mammal")), listType(nonNullType("Mammal"))) + registry.isSubTypeOf(listType(nonNullType("Mammal")), listType(type("Mammal"))) + !registry.isSubTypeOf(listType(type("Mammal")), listType(nonNullType("Mammal"))) + !registry.isSubTypeOf(listType(listType(type("Mammal"))), listType(type("Mammal"))) + !registry.isSubTypeOf(listType(type("Mammal")), listType(listType(type("Mammal")))) + !registry.isSubTypeOf(nonNullType(listType(type("Mammal"))), nonNullType(listType(nonNullType("Mammal")))) + where: typeOfReg | _ diff --git a/src/test/groovy/graphql/schema/idl/TypeInfoTest.groovy b/src/test/groovy/graphql/schema/idl/TypeInfoTest.groovy index 556755e436..e50d51fe53 100644 --- a/src/test/groovy/graphql/schema/idl/TypeInfoTest.groovy +++ b/src/test/groovy/graphql/schema/idl/TypeInfoTest.groovy @@ -114,6 +114,11 @@ class TypeInfoTest extends Specification { assertNotEqualsAndHashCode(new NonNullType(new ListType(new TypeName("A"))), new ListType(new TypeName("A"))) assertNotEqualsAndHashCode(new NonNullType(new ListType(new TypeName("A"))), new NonNullType(new ListType(new TypeName("B")))) + + assertEqualsAndHashCode(new ListType(new NonNullType(new TypeName("A"))), new ListType(new NonNullType(new TypeName("A")))) + assertNotEqualsAndHashCode(new ListType(new TypeName("A")), new ListType(new NonNullType(new TypeName("A")))) + assertNotEqualsAndHashCode(new ListType(new ListType(new TypeName("A"))), new ListType(new TypeName("A"))) + assertNotEqualsAndHashCode(new NonNullType(new ListType(new TypeName("A"))), new NonNullType(new ListType(new NonNullType(new TypeName("A"))))) }