I want to enforce that my controller endpoints ret...
# konsist
e
I want to enforce that my controller endpoints return
ResponseEntity<Page<Foo>>
instead of
ResponseEntity<List<Foo>>
(or any other collection, like
Set
etc.). How can I make this assertion on a
KoReturnProvider
?
This is working, but ugly.
Copy code
@Test
    fun `test rest functions do not return a ResponseEntity containing a Collection`() {
        controllers
            .restFunctions()
            .assertFalse { function ->
                val className = (function.containingDeclaration as KoClassDeclaration).fullyQualifiedName
                val clazz = Class.forName(className)
                val functions = clazz.kotlin.functions.filter { it.name == function.name }
                val functionReturnRegex =
                    ".*${function.returnType?.name.orEmpty().replace("<", "<.*")}".replace("?", "\\?").toRegex()
                val f = functions.first { it.returnType.toString().matches(functionReturnRegex) }
                f.returnType.arguments.any { genericType ->
                    (genericType.type?.classifier as? KClass<*>)?.let { Collection::class.isSuperclassOf(it) } == true
                }
            }
    }
n
Konsist
0.17.0
has been released! This release includes this improvement :) The following test should be helpful in your case:
Copy code
Konsist
    .scopeFromProduction()
    .functions()
    .returnTypes
    .assertTrue {
        it
            .typeArguments
            ?.firstOrNull()
            ?.hasSourceDeclarationOf(Page::class)
    }
or more general:
Copy code
Konsist
    .scopeFromProduction()
    .functions()
    .withName("sampleTest")
    .returnTypes
    .assertFalse {
        it
            .typeArguments
            ?.firstOrNull()
            ?.hasSourceDeclaration { it.isKotlinCollectionType }
    }
❤️ 1