Skip to content

Commit

Permalink
Remove dependency on runtime-micronaut project entirely from codegen (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
ashwini-desai authored Jul 22, 2020
1 parent b76da80 commit 1c0cf9d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 33 deletions.
4 changes: 0 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
7 changes: 4 additions & 3 deletions codegen/src/main/kotlin/apifi/codegen/ApiBuilder.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -63,10 +63,11 @@ object ApiBuilder {

private fun operationExceptionAnnotations(responses: List<Response>): List<AnnotationSpec> {
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()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Int>) = statuses.map { status -> allExceptionDetailsHolder.find { it.status == status }?.exceptionClassPath ?: "InternalServerErrorException" }

}


Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package apifi.micronaut.exceptions
package apifi.helpers

import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe
Expand All @@ -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")
}

}
Expand Down

This file was deleted.

0 comments on commit 1c0cf9d

Please sign in to comment.