diff --git a/codegen/src/main/kotlin/apifi/codegen/ApiBuilder.kt b/codegen/src/main/kotlin/apifi/codegen/ApiBuilder.kt index eec9402..473c4b8 100644 --- a/codegen/src/main/kotlin/apifi/codegen/ApiBuilder.kt +++ b/codegen/src/main/kotlin/apifi/codegen/ApiBuilder.kt @@ -11,7 +11,7 @@ object ApiBuilder { private const val micronautHttpAnnotation = "io.micronaut.http.annotation" - fun build(name: String, paths: List, basePackageName: String, modelMapping: Map): FileSpec { + fun build(name: String, paths: List, basePackageName: String, modelMapping: List>): FileSpec { val baseName = toTitleCase(name) val controllerClassName = "${baseName}Api" @@ -36,7 +36,7 @@ object ApiBuilder { return FileSpec.builder(basePackageName, "$controllerClassName.kt").addType(classSpec.build()).addType(controllerInterfaceClass).build() } - private fun generateOperationFunctions(paths: List, basePackageName: String, modelMapping: Map): List { + private fun generateOperationFunctions(paths: List, basePackageName: String, modelMapping: List>): List { return paths.flatMap { path -> path.operations?.map { operation -> val serviceCallStatement = controllerCallStatement(operation, modelMapping) diff --git a/codegen/src/main/kotlin/apifi/codegen/CodeGenerator.kt b/codegen/src/main/kotlin/apifi/codegen/CodeGenerator.kt index 42f669b..ab88c1f 100644 --- a/codegen/src/main/kotlin/apifi/codegen/CodeGenerator.kt +++ b/codegen/src/main/kotlin/apifi/codegen/CodeGenerator.kt @@ -7,8 +7,8 @@ import com.squareup.kotlinpoet.TypeSpec object CodeGenerator { fun generate(spec: Spec, basePackageName: String): List { - val modelFiles: List = if (spec.models.isNotEmpty()) listOf(ModelFileBuilder.build(spec.models, basePackageName)) else emptyList() - val modelMapping = modelFiles.flatMap { it.members.mapNotNull { m -> (m as TypeSpec).name }.map { name -> name to "${it.packageName}.$name" } }.toMap() + val modelFiles: List = if(spec.models.isNotEmpty()) listOf(ModelFileBuilder.build(spec.models, basePackageName)) else emptyList() + val modelMapping = modelFiles.flatMap { it.members.mapNotNull { m -> (m as TypeSpec).name }.map { name -> name to "${it.packageName}.$name" } } val apiGroups = spec.paths.groupBy { it.operations?.firstOrNull { o -> o.tags != null }?.tags?.firstOrNull() }.filter { it.key != null } diff --git a/codegen/src/main/kotlin/apifi/codegen/ModelFileBuilder.kt b/codegen/src/main/kotlin/apifi/codegen/ModelFileBuilder.kt index d5cb50b..ee097b0 100644 --- a/codegen/src/main/kotlin/apifi/codegen/ModelFileBuilder.kt +++ b/codegen/src/main/kotlin/apifi/codegen/ModelFileBuilder.kt @@ -7,7 +7,7 @@ import com.squareup.kotlinpoet.* object ModelFileBuilder { fun build(models: List, basePackageName: String): FileSpec { val packageName = "$basePackageName.models" - val baseModelMapping = models.map { it.name to "$packageName.${it.name}" }.toMap() + val baseModelMapping = models.map { it.name to "$packageName.${it.name}" } val builder = FileSpec.builder(packageName, "Models.kt") models.forEach { model -> builder.addType( diff --git a/codegen/src/main/kotlin/apifi/codegen/RequestBodyBuilder.kt b/codegen/src/main/kotlin/apifi/codegen/RequestBodyBuilder.kt index 245e0f3..496e77f 100644 --- a/codegen/src/main/kotlin/apifi/codegen/RequestBodyBuilder.kt +++ b/codegen/src/main/kotlin/apifi/codegen/RequestBodyBuilder.kt @@ -5,7 +5,7 @@ import com.squareup.kotlinpoet.ClassName import com.squareup.kotlinpoet.ParameterSpec object RequestBodyBuilder { - fun build(bodyType: String, modelMapping: Map): ParameterSpec = + fun build(bodyType: String, modelMapping: List>): ParameterSpec = ParameterSpec.builder("body", bodyType.toKotlinPoetType(modelMapping)) .addAnnotation(ClassName("io.micronaut.http.annotation", "Body")) .build() diff --git a/codegen/src/main/kotlin/apifi/helpers/Functions.kt b/codegen/src/main/kotlin/apifi/helpers/Functions.kt index 74023b5..449bda2 100644 --- a/codegen/src/main/kotlin/apifi/helpers/Functions.kt +++ b/codegen/src/main/kotlin/apifi/helpers/Functions.kt @@ -15,10 +15,10 @@ fun toCamelCase(s: String): String = CaseUtils.toCamelCase(s, false, '_', '-', ' fun Schema.toCodeGenModel(name: String): CodegenModel = KotlinClientCodegen().fromModel(name, this, null) fun Schema.toCodeGenModel(): CodegenModel = this.toCodeGenModel("any") -fun String.toKotlinPoetType(): TypeName = toKotlinPoetType(emptyMap()) +fun String.toKotlinPoetType(): TypeName = toKotlinPoetType(emptyList()) -fun String.toKotlinPoetType(packageNameMapping: Map): TypeName { - val withPackage = { name: String -> ClassName.bestGuess(packageNameMapping[name] ?: name) } +fun String.toKotlinPoetType(packageNameMapping: List>): TypeName { + val withPackage = { name: String -> ClassName.bestGuess(packageNameMapping.find { name == it.first }?.second ?: name) } return if (this.contains("<")) { val parts = this.split('<') val primaryType = parts[0] diff --git a/codegen/src/test/kotlin/apifi/codegen/ApiBuilderTest.kt b/codegen/src/test/kotlin/apifi/codegen/ApiBuilderTest.kt index 9df51bb..09ca3e7 100644 --- a/codegen/src/test/kotlin/apifi/codegen/ApiBuilderTest.kt +++ b/codegen/src/test/kotlin/apifi/codegen/ApiBuilderTest.kt @@ -138,4 +138,4 @@ class ApiBuilderTest : DescribeSpec({ }) -fun modelMapping() = mapOf("Pet" to "models.Pet", "PetResponse" to "models.PetResponse") +fun modelMapping() = listOf("Pet" to "models.Pet", "PetResponse" to "models.PetResponse") diff --git a/codegen/src/test/kotlin/apifi/codegen/RequestBodyBuilderTest.kt b/codegen/src/test/kotlin/apifi/codegen/RequestBodyBuilderTest.kt index 39eb8c2..9e571cb 100644 --- a/codegen/src/test/kotlin/apifi/codegen/RequestBodyBuilderTest.kt +++ b/codegen/src/test/kotlin/apifi/codegen/RequestBodyBuilderTest.kt @@ -7,7 +7,7 @@ class RequestBodyBuilderTest : DescribeSpec( { describe("Request Body Builder") { it("should generate request body type") { - val requestBody = RequestBodyBuilder.build("Pet", mapOf("Pet" to "models.Pet")) + val requestBody = RequestBodyBuilder.build("Pet", listOf("Pet" to "models.Pet")) requestBody.toString() shouldBe "@io.micronaut.http.annotation.Body body: models.Pet" } }