Skip to content

feat: try isAssignableFrom instead of DFS isSubclassOf #2114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Expedia, Inc
* Copyright 2025 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,7 +29,6 @@ import kotlin.reflect.KVisibility
import kotlin.reflect.full.declaredMemberFunctions
import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.full.findParameterByName
import kotlin.reflect.full.isSubclassOf
import kotlin.reflect.full.memberFunctions
import kotlin.reflect.full.memberProperties
import kotlin.reflect.full.primaryConstructor
Expand Down Expand Up @@ -67,7 +66,7 @@ private fun KClass<*>.isDeclaredUnion() = this.isInterface() && this.declaredMem
internal fun KClass<*>.isAnnotationUnion(fieldAnnotations: List<Annotation>): Boolean = (this.isInstance(Any::class) || this.isAnnotation()) &&
fieldAnnotations.getUnionAnnotation() != null

internal fun KClass<*>.isAnnotation(): Boolean = this.isSubclassOf(Annotation::class)
internal fun KClass<*>.isAnnotation(): Boolean = Annotation::class.java.isAssignableFrom(this.java)

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

internal fun KClass<*>.isEnum(): Boolean = this.isSubclassOf(Enum::class)
internal fun KClass<*>.isEnum(): Boolean = Enum::class.java.isAssignableFrom(this.java)

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

@Throws(CouldNotGetNameOfKClassException::class)
internal fun KClass<*>.getSimpleName(isInputClass: Boolean = false): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Expedia, Inc
* Copyright 2025 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Expedia, Inc
* Copyright 2025 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,15 +20,14 @@ import com.expediagroup.graphql.generator.exceptions.InvalidWrappedTypeException
import com.expediagroup.graphql.generator.execution.OptionalInput
import kotlin.reflect.KClass
import kotlin.reflect.KType
import kotlin.reflect.full.isSubclassOf
import kotlin.reflect.full.withNullability
import kotlin.reflect.jvm.jvmErasure

internal fun KType.getKClass() = this.jvmErasure

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

internal fun KType.isSubclassOf(kClass: KClass<*>) = this.getKClass().isSubclassOf(kClass)
internal fun KType.isSubclassOf(kClass: KClass<*>) = kClass.java.isAssignableFrom(this.getJavaClass())

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import java.io.Closeable
import kotlin.reflect.KClass
import kotlin.reflect.KType
import kotlin.reflect.full.createType
import kotlin.reflect.full.isSubclassOf
import kotlin.reflect.full.starProjectedType

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

when {
kClass.isListType(cacheKey.isDirective) -> null
kClass.isSubclassOf(Enum::class) -> kClass.getSimpleName()
Enum::class.java.isAssignableFrom(kClass.java) -> kClass.getSimpleName()
isTypeNotSupported(type) -> throw TypeNotSupportedException(type, supportedPackages)
else -> type.getSimpleName(cacheKey.inputType)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Expedia, Inc
* Copyright 2025 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Expedia, Inc
* Copyright 2015 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Expedia, Inc
* Copyright 2025 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -46,6 +46,8 @@ class KTypeExtensionsKtTest {

class SimpleClass(val id: String) : SimpleInterface

enum class SimpleEnum { ONE, TWO, THREE }

@Test
fun getTypeOfFirstArgument() {
assertEquals(String::class.starProjectedType, MyClass::listFun.findParameterByName("list")?.type?.getTypeOfFirstArgument())
Expand Down Expand Up @@ -92,6 +94,7 @@ class KTypeExtensionsKtTest {
fun isSubclassOf() {
assertTrue(MyClass::class.starProjectedType.isSubclassOf(MyClass::class))
assertTrue(SimpleClass::class.starProjectedType.isSubclassOf(SimpleInterface::class))
assertTrue(SimpleEnum::class.starProjectedType.isSubclassOf(Enum::class))
assertFalse(SimpleInterface::class.starProjectedType.isSubclassOf(SimpleClass::class))
assertFalse(MyClass::class.starProjectedType.isSubclassOf(SimpleInterface::class))
}
Expand Down
Loading