diff --git a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/model/ClassData.kt b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/model/ClassData.kt index c110211fb..12701c002 100644 --- a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/model/ClassData.kt +++ b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/model/ClassData.kt @@ -26,7 +26,7 @@ data class ClassData( val name: String, val packageName: String, val functions: List, - val imports: List, + val imports: Set, val superClasses: List = emptyList(), val properties: List = emptyList(), val modifiers: List = emptyList(), @@ -45,10 +45,9 @@ data class ClassData( fun KSClassDeclaration.toClassData(logger: KSPLogger): ClassData { val ksClassDeclaration = this val imports = - mutableListOf( + mutableSetOf( "io.ktor.util.reflect.typeInfo", "io.ktor.client.request.HttpRequestBuilder", - "io.ktor.client.request.setBody", "io.ktor.client.request.headers", "io.ktor.client.request.parameter", "io.ktor.http.URLBuilder", @@ -69,6 +68,15 @@ fun KSClassDeclaration.toClassData(logger: KSPLogger): ClassData { return@map funcDeclaration.toFunctionData(logger) } + if (functionDataList.any { + it.parameterDataList.any { + it.hasAnnotation() + } + } + ) { + imports.add("io.ktor.client.request.setBody") + } + if (functionDataList.any { it -> it.annotations.any { it is FormUrlEncoded || it is Multipart } || it.parameterDataList.any { param -> param.hasAnnotation() || param.hasAnnotation() } diff --git a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/utils/Utils.kt b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/utils/Utils.kt index 5d778a58d..cbbf242eb 100644 --- a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/utils/Utils.kt +++ b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/utils/Utils.kt @@ -20,7 +20,7 @@ fun String.surroundIfNotEmpty( fun String.removeWhiteSpaces(): String = this.replace("\\s".toRegex(), "") -fun FileSpec.Builder.addImports(imports: List): FileSpec.Builder { +fun FileSpec.Builder.addImports(imports: Set): FileSpec.Builder { imports.forEach { /** * Wildcard imports are not allowed by KotlinPoet, as a workaround * is replaced with WILDCARDIMPORT, and it will be replaced again