Skip to content

Commit ea509cc

Browse files
authored
fix: allow for abstract properties and functions (ExpediaGroup#205)
* fix: allow for abstract properties and functions * Update src/test/kotlin/com/expedia/graphql/generator/extensions/KClassExtensionsTest.kt Co-Authored-By: smyrick <[email protected]>
1 parent d3091c2 commit ea509cc

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/main/kotlin/com/expedia/graphql/generator/extensions/kClassExtensions.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@ import kotlin.reflect.full.declaredMemberFunctions
1414
import kotlin.reflect.full.declaredMemberProperties
1515
import kotlin.reflect.full.findParameterByName
1616
import kotlin.reflect.full.isSubclassOf
17+
import kotlin.reflect.full.memberFunctions
18+
import kotlin.reflect.full.memberProperties
1719
import kotlin.reflect.full.primaryConstructor
1820
import kotlin.reflect.full.superclasses
1921

2022
private const val INPUT_SUFFIX = "Input"
2123

2224
internal fun KClass<*>.getValidProperties(hooks: SchemaGeneratorHooks): List<KProperty<*>> =
23-
this.declaredMemberProperties
25+
this.memberProperties
2426
.filter { hooks.isValidProperty(it) }
2527
.filter { prop -> propertyFilters.all { it.invoke(prop, this) } }
2628

2729
internal fun KClass<*>.getValidFunctions(hooks: SchemaGeneratorHooks): List<KFunction<*>> =
28-
this.declaredMemberFunctions
30+
this.memberFunctions
2931
.filter { hooks.isValidFunction(it) }
3032
.filter { func -> functionFilters.all { it.invoke(func) } }
3133

src/test/kotlin/com/expedia/graphql/generator/extensions/KClassExtensionsTest.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ import kotlin.test.assertTrue
1717
@Suppress("Detekt.UnusedPrivateClass")
1818
open class KClassExtensionsTest {
1919

20+
interface SomeInterface {
21+
val someField: String?
22+
23+
fun someFunction(): String?
24+
}
25+
26+
abstract class SomeAbstractClass : SomeInterface {
27+
override val someField: String = "Hello"
28+
29+
override fun someFunction(): String? = "Goodbye"
30+
31+
abstract val justAnotherField: String?
32+
}
33+
34+
class SomeConcreteClass : SomeAbstractClass() {
35+
override val justAnotherField: String? = "Default value"
36+
}
37+
2038
@Suppress("Detekt.FunctionOnlyReturningConstant", "Detekt.UnusedPrivateMember")
2139
private class MyTestClass(
2240
val publicProperty: String = "public",
@@ -90,6 +108,12 @@ open class KClassExtensionsTest {
90108
assertEquals(listOf("publicProperty"), properties.map { it.name })
91109
}
92110

111+
@Test
112+
fun `test getting valid properties from abstract classes`() {
113+
val concreteProperties = SomeConcreteClass::class.getValidProperties(noopHooks)
114+
assertEquals(listOf("justAnotherField", "someField"), concreteProperties.map { it.name })
115+
}
116+
93117
@Test
94118
fun `test getting valid functions with no hooks`() {
95119
val properties = MyTestClass::class.getValidFunctions(noopHooks)
@@ -102,6 +126,12 @@ open class KClassExtensionsTest {
102126
assertEquals(listOf("publicFunction"), properties.map { it.name })
103127
}
104128

129+
@Test
130+
fun `test getting functions from abstract classes`() {
131+
val properties = SomeConcreteClass::class.getValidFunctions(noopHooks)
132+
assertEquals(listOf("someFunction"), properties.map { it.name })
133+
}
134+
105135
@Test
106136
fun `test getting valid superclass with no hooks`() {
107137
val superclasses = InterfaceSuperclass::class.getValidSuperclasses(noopHooks)

0 commit comments

Comments
 (0)