Skip to content

Commit c4e6df5

Browse files
committed
Fixed issue with root resolver extending generic class
1 parent 6a76d21 commit c4e6df5

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

src/main/kotlin/com/coxautodev/graphql/tools/GenericType.kt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.coxautodev.graphql.tools
22

33
import com.google.common.primitives.Primitives
4+
import org.apache.commons.lang3.ClassUtils
45
import org.apache.commons.lang3.reflect.TypeUtils
56
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl
67
import sun.reflect.generics.reflectiveObjects.WildcardTypeImpl
@@ -96,11 +97,16 @@ internal open class GenericType(protected val mostSpecificType: JavaType, protec
9697
}
9798
is Class<*> -> if (type.isPrimitive) Primitives.wrap(type) else type
9899
is TypeVariable<*> -> {
99-
if (declaringType !is ParameterizedType) {
100-
error("Could not resolve type variable '${TypeUtils.toLongString(type)}' because declaring type is not parameterized: ${TypeUtils.toString(declaringType)}")
101-
} else {
100+
if (declaringType is ParameterizedType) {
102101
unwrapGenericType(TypeUtils.determineTypeArguments(getRawClass(mostSpecificType), declaringType)[type]
103102
?: error("No type variable found for: ${TypeUtils.toLongString(type)}"))
103+
} else {
104+
val raw = TypeUtils.getRawType(type, declaringType)
105+
if (raw != null) {
106+
unwrapGenericType(raw)
107+
} else {
108+
error("Could not resolve type variable '${TypeUtils.toLongString(type)}' because declaring type is not parameterized: ${TypeUtils.toString(declaringType)}")
109+
}
104110
}
105111
}
106112
is WildcardTypeImpl -> type.upperBounds.firstOrNull()
@@ -110,7 +116,7 @@ internal open class GenericType(protected val mostSpecificType: JavaType, protec
110116
}
111117

112118
private fun replaceTypeVariable(type: JavaType): JavaType {
113-
return when(type) {
119+
return when (type) {
114120
is ParameterizedType -> {
115121
val actualTypeArguments = type.actualTypeArguments.map { replaceTypeVariable(it) }.toTypedArray()
116122
ParameterizedTypeImpl.make(type.rawType as Class<*>?, actualTypeArguments, type.ownerType)
@@ -127,5 +133,10 @@ internal open class GenericType(protected val mostSpecificType: JavaType, protec
127133
}
128134
}
129135
}
136+
137+
private fun parameterizedDeclaringType(): JavaType? {
138+
return declaringType as? ParameterizedType
139+
?: ClassUtils.getAllSuperclasses(declaringType.unwrap()).find { it is ParameterizedType }
140+
}
130141
}
131142
}

src/main/kotlin/com/coxautodev/graphql/tools/MethodFieldResolver.kt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ internal class MethodFieldResolver(field: FieldDefinition, search: FieldResolver
9999

100100
override fun scanForMatches(): List<TypeClassMatcher.PotentialMatch> {
101101
val batched = isBatched(method, search)
102-
// fixme: convert type variables
103-
// val replacedGenericType = replaceTypeVariables(method.genericReturnType)
104102
val unwrappedGenericType = genericType.unwrapGenericType(method.genericReturnType)
105103
val returnValueMatch = TypeClassMatcher.PotentialMatch.returnValue(field.type, unwrappedGenericType, genericType, SchemaClassScanner.ReturnValueReference(method), batched)
106104

@@ -109,14 +107,6 @@ internal class MethodFieldResolver(field: FieldDefinition, search: FieldResolver
109107
} + listOf(returnValueMatch)
110108
}
111109

112-
// private fun replaceTypeVariables(javaType: JavaType): JavaType {
113-
// return when (javaType) {
114-
// is ParameterizedType -> replaceTypeVariables(javaType.rawType)
115-
// is TypeVariable<*> -> genericType.unwrapGenericType(javaType)
116-
// else -> javaType
117-
// }
118-
// }
119-
120110
private fun getIndexOffset(): Int {
121111
return if (resolverInfo is DataClassTypeResolverInfo && !method.declaringClass.isAssignableFrom(resolverInfo.dataClassType)) {
122112
1

src/test/groovy/com/coxautodev/graphql/tools/TypeClassMatcherSpec.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class TypeClassMatcherSpec extends Specification {
6363
then:
6464
noExceptionThrown()
6565
match.type == customDefinition
66-
match.clazz == CustomType
66+
match.classEntry.clazz == CustomType
6767

6868
where:
6969
methodName | type

0 commit comments

Comments
 (0)