diff --git a/src/main/kotlin/apifi/codegen/ApiBuilder.kt b/src/main/kotlin/apifi/codegen/ApiBuilder.kt index b8e5e7f..7904e30 100644 --- a/src/main/kotlin/apifi/codegen/ApiBuilder.kt +++ b/src/main/kotlin/apifi/codegen/ApiBuilder.kt @@ -90,7 +90,7 @@ object ApiBuilder { val requestParamNames = operation.request?.let { req -> if (req.consumes?.contains("multipart/form-data") == true) "java.io.File.createTempFile(body.filename, \"\").also { it.writeBytes(body.bytes) }" else requestBodyParams.joinToString { it.name } }?.let { listOf(it) } ?: emptyList() - return "HttpResponse.ok(service.${operation.name}(${(queryParamNames + pathParamNames + requestParamNames).joinToString()}))" + return "HttpResponse.ok(controller.${operation.name}(${(queryParamNames + pathParamNames + requestParamNames).joinToString()}))" } } diff --git a/src/test-res/codegen/expected-pet-api b/src/test-res/codegen/expected-pet-api index 62127e6..4447e7b 100644 --- a/src/test-res/codegen/expected-pet-api +++ b/src/test-res/codegen/expected-pet-api @@ -22,11 +22,11 @@ class PetsApi @Inject constructor( @Post(value = "/pets") @Consumes("application/json") fun createPets(@Body body: Array, httpRequest: HttpRequest): HttpResponse = - HttpResponse.ok(service.createPets(body)) + HttpResponse.ok(controller.createPets(body)) @Get(value = "/pets/{petId}") fun showPetById(@PathVariable petId: String, httpRequest: HttpRequest): HttpResponse = - HttpResponse.ok(service.showPetById(petId)) + HttpResponse.ok(controller.showPetById(petId)) } interface PetsController { diff --git a/src/test-res/codegen/expected-store-api b/src/test-res/codegen/expected-store-api index d076599..85e0300 100644 --- a/src/test-res/codegen/expected-store-api +++ b/src/test-res/codegen/expected-store-api @@ -15,7 +15,7 @@ class StoreApi @Inject constructor( ) { @Get(value = "/store/inventory") fun get(httpRequest: HttpRequest): HttpResponse> = - HttpResponse.ok(service.get()) + HttpResponse.ok(controller.get()) } interface StoreController { diff --git a/src/test/kotlin/apifi/codegen/ApiBuilderTest.kt b/src/test/kotlin/apifi/codegen/ApiBuilderTest.kt index 10fe3a2..37f8d3d 100644 --- a/src/test/kotlin/apifi/codegen/ApiBuilderTest.kt +++ b/src/test/kotlin/apifi/codegen/ApiBuilderTest.kt @@ -15,10 +15,10 @@ class ApiBuilderTest : DescribeSpec({ describe("Api Builder") { it("generate api class with controller annotation") { val path = Path("/pets", listOf(Operation(PathItem.HttpMethod.GET, "listPets", emptyList(), null, null, null))) - val controller = ApiBuilder.build("pets", listOf(path), emptyList(), "apifi.gen", modelMapping()) - val controllerClass = controller.members[0] as TypeSpec - controllerClass.name shouldBe "PetsApi" - controllerClass.annotationSpecs[0].toString() shouldBe "@io.micronaut.http.annotation.Controller" + val api = ApiBuilder.build("pets", listOf(path), emptyList(), "apifi.gen", modelMapping()) + val apiClass = api.members[0] as TypeSpec + apiClass.name shouldBe "PetsApi" + apiClass.annotationSpecs[0].toString() shouldBe "@io.micronaut.http.annotation.Controller" } it("generate api method based on spec operation method and url") { @@ -29,15 +29,15 @@ class ApiBuilderTest : DescribeSpec({ val path2 = Path("/pets/{petId}", listOf( Operation(PathItem.HttpMethod.GET, "getPet", emptyList(), null, null, null, SecurityDefinitionType.BASIC_AUTH) )) - val controller = ApiBuilder.build("pets", listOf(path1, path2), emptyList(), "apifi.gen", modelMapping()) - val controllerClass = controller.members[0] as TypeSpec - controllerClass.funSpecs.size shouldBe 3 - controllerClass.funSpecs[0].toString() shouldBe "@io.micronaut.http.annotation.Get(value = \"/pets\")\n" + - "fun getOpName(httpRequest: io.micronaut.http.HttpRequest) = HttpResponse.ok(service.getOpName())\n" - controllerClass.funSpecs[1].toString() shouldBe "@io.micronaut.http.annotation.Post(value = \"/pets\")\n" + - "fun postOpName(httpRequest: io.micronaut.http.HttpRequest) = HttpResponse.ok(service.postOpName())\n" - controllerClass.funSpecs[2].toString() shouldBe "@io.micronaut.http.annotation.Get(value = \"/pets/{petId}\")\n" + - "fun getPet(httpRequest: io.micronaut.http.HttpRequest) = HttpResponse.ok(service.getPet())\n" + val api = ApiBuilder.build("pets", listOf(path1, path2), emptyList(), "apifi.gen", modelMapping()) + val apiClass = api.members[0] as TypeSpec + apiClass.funSpecs.size shouldBe 3 + apiClass.funSpecs[0].toString() shouldBe "@io.micronaut.http.annotation.Get(value = \"/pets\")\n" + + "fun getOpName(httpRequest: io.micronaut.http.HttpRequest) = HttpResponse.ok(controller.getOpName())\n" + apiClass.funSpecs[1].toString() shouldBe "@io.micronaut.http.annotation.Post(value = \"/pets\")\n" + + "fun postOpName(httpRequest: io.micronaut.http.HttpRequest) = HttpResponse.ok(controller.postOpName())\n" + apiClass.funSpecs[2].toString() shouldBe "@io.micronaut.http.annotation.Get(value = \"/pets/{petId}\")\n" + + "fun getPet(httpRequest: io.micronaut.http.HttpRequest) = HttpResponse.ok(controller.getPet())\n" } it("generate api method with query, path and header params") { val queryParam = Param("limit", "kotlin.Int", true, ParamType.Query) @@ -45,10 +45,10 @@ class ApiBuilderTest : DescribeSpec({ val headerParam = Param("x-header", "kotlin.String", true, ParamType.Header) val operation = Operation(PathItem.HttpMethod.POST, "createPet", emptyList(), listOf(queryParam, pathParam, headerParam), null, null) - val controller = ApiBuilder.build("pets", listOf(Path("/pets", listOf(operation))), emptyList(), "apifi.gen", modelMapping()) + val api = ApiBuilder.build("pets", listOf(Path("/pets", listOf(operation))), emptyList(), "apifi.gen", modelMapping()) - val controllerClass = controller.members[0] as TypeSpec - controllerClass.funSpecs[0].parameters.map { it.toString() } shouldContainExactlyInAnyOrder + val apiClass = api.members[0] as TypeSpec + apiClass.funSpecs[0].parameters.map { it.toString() } shouldContainExactlyInAnyOrder listOf("@io.micronaut.http.annotation.QueryValue limit: kotlin.Int", "@io.micronaut.http.annotation.PathVariable petId: kotlin.Int", "@io.micronaut.http.annotation.Header(value = \"x-header\") xHeader: kotlin.String", @@ -58,15 +58,15 @@ class ApiBuilderTest : DescribeSpec({ it("generate api method with request and response") { val operation = Operation(PathItem.HttpMethod.POST, "createPet", emptyList(), emptyList(), Request("Pet", listOf("application/json", "text/plain")), listOf("PetResponse")) - val controller = ApiBuilder.build("pets", listOf(Path("/pets", listOf(operation))), emptyList(), "apifi.gen", modelMapping()) + val api = ApiBuilder.build("pets", listOf(Path("/pets", listOf(operation))), emptyList(), "apifi.gen", modelMapping()) - val controllerClass = controller.members[0] as TypeSpec - controllerClass.funSpecs[0].annotations[0].toString() shouldBe "@io.micronaut.http.annotation.Post(value = \"/pets\")" - controllerClass.funSpecs[0].annotations[1].toString() shouldBe "@io.micronaut.http.annotation.Consumes(\"application/json\", \"text/plain\")" - controllerClass.funSpecs[0].parameters.map { it.toString() } shouldContainExactlyInAnyOrder + val apiClass = api.members[0] as TypeSpec + apiClass.funSpecs[0].annotations[0].toString() shouldBe "@io.micronaut.http.annotation.Post(value = \"/pets\")" + apiClass.funSpecs[0].annotations[1].toString() shouldBe "@io.micronaut.http.annotation.Consumes(\"application/json\", \"text/plain\")" + apiClass.funSpecs[0].parameters.map { it.toString() } shouldContainExactlyInAnyOrder listOf("@io.micronaut.http.annotation.Body body: models.Pet", "httpRequest: io.micronaut.http.HttpRequest") - controllerClass.funSpecs[0].returnType.toString() shouldBe "io.micronaut.http.HttpResponse" + apiClass.funSpecs[0].returnType.toString() shouldBe "io.micronaut.http.HttpResponse" } it("generate api method block with all blocks when security dependencies are present") { @@ -75,10 +75,10 @@ class ApiBuilderTest : DescribeSpec({ val headerParam = Param("x-header", "kotlin.String", true, ParamType.Header) val operation = Operation(PathItem.HttpMethod.POST, "listPets", emptyList(), listOf(queryParam, pathParam, headerParam), Request("Pet", listOf("application/json")), listOf("PetResponse")) - val controller = ApiBuilder.build("pets", listOf(Path("/pets", listOf(operation))), listOf(SecurityDependency("httpBasic", "security", SecurityDefinitionType.BASIC_AUTH)), "apifi.gen", modelMapping()) + val api = ApiBuilder.build("pets", listOf(Path("/pets", listOf(operation))), listOf(SecurityDependency("httpBasic", "security", SecurityDefinitionType.BASIC_AUTH)), "apifi.gen", modelMapping()) - val controllerClass = controller.members[0] as TypeSpec - controllerClass.funSpecs[0].body.toString().trimIndent() shouldBe "return basicauthorizer.authorize(httpRequest.headers.authorization){HttpResponse.ok(service.listPets(limit, petId, body))}" + val apiClass = api.members[0] as TypeSpec + apiClass.funSpecs[0].body.toString().trimIndent() shouldBe "return basicauthorizer.authorize(httpRequest.headers.authorization){HttpResponse.ok(controller.listPets(limit, petId, body))}" } it("generate api method block with all blocks when security dependencies are not present") { @@ -87,27 +87,27 @@ class ApiBuilderTest : DescribeSpec({ val headerParam = Param("x-header", "kotlin.String", true, ParamType.Header) val operation = Operation(PathItem.HttpMethod.POST, "createPet", emptyList(), listOf(queryParam, pathParam, headerParam), Request("Pet", null), listOf("PetResponse")) - val controller = ApiBuilder.build("pets", listOf(Path("/pets", listOf(operation))), emptyList(), "apifi.gen", modelMapping()) + val api = ApiBuilder.build("pets", listOf(Path("/pets", listOf(operation))), emptyList(), "apifi.gen", modelMapping()) - val controllerClass = controller.members[0] as TypeSpec - controllerClass.funSpecs[0].body.toString().trimIndent() shouldBe "return HttpResponse.ok(service.createPet(limit, petId, body))" + val apiClass = api.members[0] as TypeSpec + apiClass.funSpecs[0].body.toString().trimIndent() shouldBe "return HttpResponse.ok(controller.createPet(limit, petId, body))" } it("inject controller & security dependencies") { val operation = Operation(PathItem.HttpMethod.POST, "listPets", listOf(), emptyList(), Request("Pet", null), listOf("PetResponse")) - val controller = ApiBuilder.build("pets", listOf(Path("/pets", listOf(operation))), listOf(SecurityDependency("BasicAuthorizer", "security", SecurityDefinitionType.BASIC_AUTH)), "apifi.gen", modelMapping()) + val api = ApiBuilder.build("pets", listOf(Path("/pets", listOf(operation))), listOf(SecurityDependency("BasicAuthorizer", "security", SecurityDefinitionType.BASIC_AUTH)), "apifi.gen", modelMapping()) - val controllerClass = controller.members[0] as TypeSpec - val serviceClass = controller.members[1] as TypeSpec - controllerClass.name shouldBe "PetsApi" - serviceClass.name shouldBe "PetsController" + val apiClass = api.members[0] as TypeSpec + val controllerClass = api.members[1] as TypeSpec + apiClass.name shouldBe "PetsApi" + controllerClass.name shouldBe "PetsController" - controllerClass.propertySpecs[0].name shouldBe "controller" - controllerClass.propertySpecs[0].type.toString() shouldBe "apifi.gen.PetsController" - controllerClass.propertySpecs[0].modifiers shouldContain KModifier.PRIVATE + apiClass.propertySpecs[0].name shouldBe "controller" + apiClass.propertySpecs[0].type.toString() shouldBe "apifi.gen.PetsController" + apiClass.propertySpecs[0].modifiers shouldContain KModifier.PRIVATE - controllerClass.toString() shouldContain "@io.micronaut.http.annotation.Controller\n" + + apiClass.toString() shouldContain "@io.micronaut.http.annotation.Controller\n" + "class PetsApi @javax.inject.Inject constructor(\n" + " private val controller: apifi.gen.PetsController,\n" + " private val basicauthorizer: security.BasicAuthorizer\n" + @@ -117,10 +117,10 @@ class ApiBuilderTest : DescribeSpec({ it("should convert multipart file to java file") { val operation = Operation(PathItem.HttpMethod.POST, "uploadDocument", emptyList(), emptyList(), Request("io.micronaut.http.multipart.CompleteFileUpload", listOf("multipart/form-data")), null) - val controller = ApiBuilder.build("pets", listOf(Path("/pets", listOf(operation))), listOf(SecurityDependency("BasicAuthorizer", "security", SecurityDefinitionType.BASIC_AUTH)), "apifi.gen", modelMapping()) + val api = ApiBuilder.build("pets", listOf(Path("/pets", listOf(operation))), listOf(SecurityDependency("BasicAuthorizer", "security", SecurityDefinitionType.BASIC_AUTH)), "apifi.gen", modelMapping()) - val controllerClass = controller.members[0] as TypeSpec - controllerClass.funSpecs[0].body.toString().trimIndent() shouldBe "return basicauthorizer.authorize(httpRequest.headers.authorization){HttpResponse.ok(service.uploadDocument(java.io.File.createTempFile(body.filename, \"\").also { it.writeBytes(body.bytes) }))}" + val apiClass = api.members[0] as TypeSpec + apiClass.funSpecs[0].body.toString().trimIndent() shouldBe "return basicauthorizer.authorize(httpRequest.headers.authorization){HttpResponse.ok(controller.uploadDocument(java.io.File.createTempFile(body.filename, \"\").also { it.writeBytes(body.bytes) }))}" } }