Skip to content

Commit 3af7dd3

Browse files
feat: try isAssignableFrom instead of DFS isSubclassOf (#2114)
### 📝 Description cherry pick #2109 for 7.x.x branch ### 🔗 Related Issues #1570 Co-authored-by: Samuel Vazquez <sam_2f@hotmail.com>
1 parent 259d478 commit 3af7dd3

File tree

7 files changed

+14
-14
lines changed

7 files changed

+14
-14
lines changed

generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/extensions/kClassExtensions.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Expedia, Inc
2+
* Copyright 2025 Expedia, Inc
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,7 +29,6 @@ import kotlin.reflect.KVisibility
2929
import kotlin.reflect.full.declaredMemberFunctions
3030
import kotlin.reflect.full.declaredMemberProperties
3131
import kotlin.reflect.full.findParameterByName
32-
import kotlin.reflect.full.isSubclassOf
3332
import kotlin.reflect.full.memberFunctions
3433
import kotlin.reflect.full.memberProperties
3534
import kotlin.reflect.full.primaryConstructor
@@ -67,7 +66,7 @@ private fun KClass<*>.isDeclaredUnion() = this.isInterface() && this.declaredMem
6766
internal fun KClass<*>.isAnnotationUnion(fieldAnnotations: List<Annotation>): Boolean = (this.isInstance(Any::class) || this.isAnnotation()) &&
6867
fieldAnnotations.getUnionAnnotation() != null
6968

70-
internal fun KClass<*>.isAnnotation(): Boolean = this.isSubclassOf(Annotation::class)
69+
internal fun KClass<*>.isAnnotation(): Boolean = Annotation::class.java.isAssignableFrom(this.java)
7170

7271
/**
7372
* Do not add interfaces as additional types if it expects all the types
@@ -78,9 +77,9 @@ internal fun KClass<*>.isAnnotation(): Boolean = this.isSubclassOf(Annotation::c
7877
*/
7978
internal fun KClass<*>.isValidAdditionalType(inputType: Boolean): Boolean = !(inputType && this.isInterface()) && !this.isGraphQLIgnored()
8079

81-
internal fun KClass<*>.isEnum(): Boolean = this.isSubclassOf(Enum::class)
80+
internal fun KClass<*>.isEnum(): Boolean = Enum::class.java.isAssignableFrom(this.java)
8281

83-
internal fun KClass<*>.isListType(isDirective: Boolean = false): Boolean = this.isSubclassOf(List::class) || (isDirective && this.java.isArray)
82+
internal fun KClass<*>.isListType(isDirective: Boolean = false): Boolean = List::class.java.isAssignableFrom(this.java) || (isDirective && this.java.isArray)
8483

8584
@Throws(CouldNotGetNameOfKClassException::class)
8685
internal fun KClass<*>.getSimpleName(isInputClass: Boolean = false): String {

generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/extensions/kParameterExtensions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Expedia, Inc
2+
* Copyright 2025 Expedia, Inc
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/extensions/kTypeExtensions.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Expedia, Inc
2+
* Copyright 2025 Expedia, Inc
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,15 +20,14 @@ import com.expediagroup.graphql.generator.exceptions.InvalidWrappedTypeException
2020
import com.expediagroup.graphql.generator.execution.OptionalInput
2121
import kotlin.reflect.KClass
2222
import kotlin.reflect.KType
23-
import kotlin.reflect.full.isSubclassOf
2423
import kotlin.reflect.full.withNullability
2524
import kotlin.reflect.jvm.jvmErasure
2625

2726
internal fun KType.getKClass() = this.jvmErasure
2827

2928
internal fun KType.getJavaClass(): Class<*> = this.getKClass().java
3029

31-
internal fun KType.isSubclassOf(kClass: KClass<*>) = this.getKClass().isSubclassOf(kClass)
30+
internal fun KType.isSubclassOf(kClass: KClass<*>) = kClass.java.isAssignableFrom(this.getJavaClass())
3231

3332
internal fun KType.isList() = this.isSubclassOf(List::class)
3433

generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/internal/state/TypesCache.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import java.io.Closeable
3535
import kotlin.reflect.KClass
3636
import kotlin.reflect.KType
3737
import kotlin.reflect.full.createType
38-
import kotlin.reflect.full.isSubclassOf
3938
import kotlin.reflect.full.starProjectedType
4039

4140
internal class TypesCache(private val supportedPackages: List<String>) : Closeable {
@@ -126,7 +125,7 @@ internal class TypesCache(private val supportedPackages: List<String>) : Closeab
126125

127126
when {
128127
kClass.isListType(cacheKey.isDirective) -> null
129-
kClass.isSubclassOf(Enum::class) -> kClass.getSimpleName()
128+
Enum::class.java.isAssignableFrom(kClass.java) -> kClass.getSimpleName()
130129
isTypeNotSupported(type) -> throw TypeNotSupportedException(type, supportedPackages)
131130
else -> type.getSimpleName(cacheKey.inputType)
132131
}

generator/graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/internal/extensions/KClassExtensionsTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Expedia, Inc
2+
* Copyright 2025 Expedia, Inc
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

generator/graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/internal/extensions/KParameterExtensionsKtTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Expedia, Inc
2+
* Copyright 2015 Expedia, Inc
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

generator/graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/internal/extensions/KTypeExtensionsKtTest.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Expedia, Inc
2+
* Copyright 2025 Expedia, Inc
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,6 +46,8 @@ class KTypeExtensionsKtTest {
4646

4747
class SimpleClass(val id: String) : SimpleInterface
4848

49+
enum class SimpleEnum { ONE, TWO, THREE }
50+
4951
@Test
5052
fun getTypeOfFirstArgument() {
5153
assertEquals(String::class.starProjectedType, MyClass::listFun.findParameterByName("list")?.type?.getTypeOfFirstArgument())
@@ -92,6 +94,7 @@ class KTypeExtensionsKtTest {
9294
fun isSubclassOf() {
9395
assertTrue(MyClass::class.starProjectedType.isSubclassOf(MyClass::class))
9496
assertTrue(SimpleClass::class.starProjectedType.isSubclassOf(SimpleInterface::class))
97+
assertTrue(SimpleEnum::class.starProjectedType.isSubclassOf(Enum::class))
9598
assertFalse(SimpleInterface::class.starProjectedType.isSubclassOf(SimpleClass::class))
9699
assertFalse(MyClass::class.starProjectedType.isSubclassOf(SimpleInterface::class))
97100
}

0 commit comments

Comments
 (0)