From 1c0cf9dee5ba8eb096b3f254f8b07619f55d34fe Mon Sep 17 00:00:00 2001 From: "Ashwini A. Mutalik Desai" Date: Wed, 22 Jul 2020 17:10:45 +0530 Subject: [PATCH] Remove dependency on runtime-micronaut project entirely from codegen (#14) * Add exception classes and handlers in runtime module * Remove codegen logic for generating excepiton classes, move HttpStatusToExceptionClassMapper to runtime module * Fix import in test * Set dependency of codegen on runtime-micronaut to 'compileOnly' * remove runtime dependency on codegen as it would result in getting all micronaut dependencies in codegen. Using string paths of exception files * Move basePackage name of exception classes inside mapper --- build.gradle | 4 --- .../main/kotlin/apifi/codegen/ApiBuilder.kt | 7 +++--- .../HttpStatusToExceptionClassMapper.kt | 25 +++++++++++++++++++ .../HttpStatusToExceptionClassMapperTest.kt | 6 ++--- .../HttpStatusToExceptionClassMapper.kt | 23 ----------------- 5 files changed, 32 insertions(+), 33 deletions(-) create mode 100644 codegen/src/main/kotlin/apifi/helpers/HttpStatusToExceptionClassMapper.kt rename runtime-micronaut/src/test/kotlin/apifi/micronaut/exceptions/Non200ResponseHandlerTest.kt => codegen/src/test/kotlin/apifi/helpers/HttpStatusToExceptionClassMapperTest.kt (72%) delete mode 100644 runtime-micronaut/src/main/kotlin/apifi/micronaut/exceptions/HttpStatusToExceptionClassMapper.kt diff --git a/build.gradle b/build.gradle index 5a2add2..6a4326d 100644 --- a/build.gradle +++ b/build.gradle @@ -43,12 +43,8 @@ subprojects { } project(":codegen") { - configurations { - testCompile.extendsFrom compileOnly - } dependencies { - compileOnly project(":runtime-micronaut") implementation 'io.swagger.parser.v3:swagger-parser-v3:2.0.16' implementation 'org.openapitools:openapi-generator-cli:3.3.4' implementation 'com.squareup:kotlinpoet:1.5.0' diff --git a/codegen/src/main/kotlin/apifi/codegen/ApiBuilder.kt b/codegen/src/main/kotlin/apifi/codegen/ApiBuilder.kt index fd2417e..6d1ef6a 100644 --- a/codegen/src/main/kotlin/apifi/codegen/ApiBuilder.kt +++ b/codegen/src/main/kotlin/apifi/codegen/ApiBuilder.kt @@ -1,7 +1,7 @@ package apifi.codegen +import apifi.helpers.HttpStatusToExceptionClassMapper import apifi.helpers.toTitleCase -import apifi.micronaut.exceptions.HttpStatusToExceptionClassMapper import apifi.parser.models.Operation import apifi.parser.models.Path import apifi.parser.models.Response @@ -63,10 +63,11 @@ object ApiBuilder { private fun operationExceptionAnnotations(responses: List): List { val non2xxStatusResponseFromOperation = responses.filter { it.defaultOrStatus != "default" && it.defaultOrStatus != "200" && it.defaultOrStatus != "201" }.map { it.defaultOrStatus.toInt() } - val exceptionClassesForNon2xxResponses = non2xxStatusResponseFromOperation.let { HttpStatusToExceptionClassMapper().getExceptionClassFor(it) } + val httpStatusToExceptionClassMapper = HttpStatusToExceptionClassMapper() + val exceptionClassesForNon2xxResponses = non2xxStatusResponseFromOperation.let { httpStatusToExceptionClassMapper.getExceptionClassFor(it) } return exceptionClassesForNon2xxResponses.map { exceptionClass -> AnnotationSpec.builder(Throws::class) - .addMember("%T::class", exceptionClass.asClassName()) + .addMember("%T::class", ClassName(httpStatusToExceptionClassMapper.basePackageName, exceptionClass)) .build() } } diff --git a/codegen/src/main/kotlin/apifi/helpers/HttpStatusToExceptionClassMapper.kt b/codegen/src/main/kotlin/apifi/helpers/HttpStatusToExceptionClassMapper.kt new file mode 100644 index 0000000..72ee9bd --- /dev/null +++ b/codegen/src/main/kotlin/apifi/helpers/HttpStatusToExceptionClassMapper.kt @@ -0,0 +1,25 @@ +package apifi.helpers + + +class HttpStatusToExceptionClassMapper { + + val basePackageName = "apifi.micronaut.exceptions" + + private val allExceptionDetailsHolder = listOf( + ExceptionDetailsHolder(400, "BadRequestException"), + ExceptionDetailsHolder(401, "UnauthorizedException"), + ExceptionDetailsHolder(403, "ForbiddenException"), + ExceptionDetailsHolder(404, "NotFoundException"), + ExceptionDetailsHolder(500, "InternalServerErrorException") + ) + + class ExceptionDetailsHolder( + val status: Int, + val exceptionClassPath: String + ) + + fun getExceptionClassFor(statuses: List) = statuses.map { status -> allExceptionDetailsHolder.find { it.status == status }?.exceptionClassPath ?: "InternalServerErrorException" } + +} + + diff --git a/runtime-micronaut/src/test/kotlin/apifi/micronaut/exceptions/Non200ResponseHandlerTest.kt b/codegen/src/test/kotlin/apifi/helpers/HttpStatusToExceptionClassMapperTest.kt similarity index 72% rename from runtime-micronaut/src/test/kotlin/apifi/micronaut/exceptions/Non200ResponseHandlerTest.kt rename to codegen/src/test/kotlin/apifi/helpers/HttpStatusToExceptionClassMapperTest.kt index 19dc3ec..3715ac8 100644 --- a/runtime-micronaut/src/test/kotlin/apifi/micronaut/exceptions/Non200ResponseHandlerTest.kt +++ b/codegen/src/test/kotlin/apifi/helpers/HttpStatusToExceptionClassMapperTest.kt @@ -1,4 +1,4 @@ -package apifi.micronaut.exceptions +package apifi.helpers import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.shouldBe @@ -8,12 +8,12 @@ class HttpStatusToExceptionClassMapperTest : StringSpec() { init { "should return exception classes given a list of status" { HttpStatusToExceptionClassMapper().getExceptionClassFor(listOf(400, 500, 401)) shouldBe - listOf(BadRequestException::class, InternalServerErrorException::class, UnauthorizedException::class) + listOf("BadRequestException", "InternalServerErrorException", "UnauthorizedException") } "should return class for internal server error exception if no specific exception class found for a status" { HttpStatusToExceptionClassMapper().getExceptionClassFor(listOf(405, 302, 507)).distinct() shouldBe - listOf(InternalServerErrorException::class) + listOf("InternalServerErrorException") } } diff --git a/runtime-micronaut/src/main/kotlin/apifi/micronaut/exceptions/HttpStatusToExceptionClassMapper.kt b/runtime-micronaut/src/main/kotlin/apifi/micronaut/exceptions/HttpStatusToExceptionClassMapper.kt deleted file mode 100644 index f30b76a..0000000 --- a/runtime-micronaut/src/main/kotlin/apifi/micronaut/exceptions/HttpStatusToExceptionClassMapper.kt +++ /dev/null @@ -1,23 +0,0 @@ -package apifi.micronaut.exceptions - -import kotlin.reflect.KClass - -class HttpStatusToExceptionClassMapper { - - private val allExceptionDetailsHolder = listOf( - ExceptionDetailsHolder(400, BadRequestException::class), - ExceptionDetailsHolder(401, UnauthorizedException::class), - ExceptionDetailsHolder(403, ForbiddenException::class), - ExceptionDetailsHolder(404, NotFoundException::class), - ExceptionDetailsHolder(500, InternalServerErrorException::class) - ) - - fun getExceptionClassFor(statuses: List) = statuses.map { status -> allExceptionDetailsHolder.find { it.status == status }?.exceptionClass ?: InternalServerErrorException::class } - -} - - -data class ExceptionDetailsHolder( - val status: Int, - val exceptionClass: KClass<*> -) \ No newline at end of file