Skip to content

Commit

Permalink
Merge pull request #10 from ashwini-desai/refactoring
Browse files Browse the repository at this point in the history
Introduce codegen module
  • Loading branch information
ashwini-desai authored Jul 9, 2020
2 parents 4a44ec9 + a1f0aa6 commit 92edcb4
Show file tree
Hide file tree
Showing 58 changed files with 135 additions and 264 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ out
.idea

# Ignore mac specific directories
.DS_Store
.DS_Store

#Ignore .kotlintest specific directory which lists test failures
**/.kotlintest/
68 changes: 35 additions & 33 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.61'
id 'application'
id 'org.jetbrains.kotlin.jvm' version '1.3.72' apply false
}

repositories {
jcenter()
maven { url 'https://jitpack.io' }
}
subprojects {
apply plugin: 'org.jetbrains.kotlin.jvm'

dependencies {
implementation platform('org.jetbrains.kotlin:kotlin-bom')
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
group = 'com.medly.apifi'

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'
repositories {
jcenter()
}

implementation 'com.github.medly:ktee:master-SNAPSHOT'
dependencies {
implementation platform('org.jetbrains.kotlin:kotlin-bom')
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'

implementation 'org.slf4j:slf4j-simple:1.7.30'
implementation 'org.apache.commons:commons-text:1.8'
testImplementation 'io.kotest:kotest-runner-junit5-jvm:4.1.1'
testImplementation 'io.kotest:kotest-assertions-core-jvm:4.1.1'
testImplementation 'io.kotest:kotest-property-jvm:4.1.1'
testImplementation 'io.kotest:kotest-runner-console-jvm:4.1.1'
}

testImplementation 'org.jetbrains.kotlin:kotlin-test'
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit5'
testImplementation 'io.kotlintest:kotlintest-runner-junit5:3.4.2'
}
test {
useJUnitPlatform()
test {
useJUnitPlatform()

testLogging {
events "PASSED", "FAILED", "SKIPPED"
exceptionFormat "full"
testLogging {
events "PASSED", "FAILED", "SKIPPED"
exceptionFormat "full"
}
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
kotlinOptions {
jvmTarget = "1.8"
}
}
}
application {
// Define the main class for the application.
mainClassName = 'apifi.AppKt'
}

jar {
manifest {
attributes 'Main-Class': "${mainClassName}"
project(":codegen") {
dependencies {
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'

implementation 'org.slf4j:slf4j-simple:1.7.30'
implementation 'org.apache.commons:commons-text:1.8'
}
from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package apifi.codegen

import apifi.codegen.exceptions.Non200ResponseHandler
import apifi.helpers.toKotlinPoetType
import apifi.helpers.toTitleCase
import apifi.parser.models.Operation
import apifi.parser.models.ParamType
import apifi.parser.models.Path
import apifi.models.Operation
import apifi.models.ParamType
import apifi.models.Path
import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package apifi.codegen

import apifi.parser.models.Spec
import apifi.codegen.exceptions.Non200ResponseHandler
import apifi.models.Spec
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.TypeSpec

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package apifi.codegen

import apifi.helpers.toKotlinPoetType
import apifi.parser.models.ParamType
import apifi.parser.models.Path
import apifi.models.ParamType
import apifi.models.Path
import com.squareup.kotlinpoet.*

object ControllerInterfaceBuilder {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package apifi.codegen

import apifi.parser.models.Param
import apifi.models.Param
import apifi.helpers.toCamelCase
import apifi.helpers.toKotlinPoetType
import com.squareup.kotlinpoet.AnnotationSpec
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package apifi.codegen

import apifi.helpers.toKotlinPoetType
import apifi.parser.models.Model
import apifi.models.Model
import com.squareup.kotlinpoet.*

object ModelFileBuilder {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package apifi.codegen

import apifi.helpers.toKotlinPoetType
import apifi.parser.models.Param
import apifi.models.Param
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.ParameterSpec

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package apifi.codegen

import apifi.helpers.toKotlinPoetType
import apifi.parser.models.Param
import apifi.models.Param
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.ParameterSpec

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package apifi.codegen
package apifi.codegen.exceptions

import apifi.codegen.exceptions.ExceptionDetailsHolder
import apifi.helpers.toKotlinPoetType
import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package apifi.codegen
package apifi.codegen.exceptions

import org.apache.http.HttpStatus

object Non200ResponseHandler {

private val allExceptionDetailsHolder = listOf(
ExceptionDetailsHolder(HttpStatus.SC_BAD_REQUEST, "BadRequestException", "Bad Request"),
ExceptionDetailsHolder(HttpStatus.SC_UNAUTHORIZED, "UnauthorizedException", "Unauthorized Request"),
ExceptionDetailsHolder(HttpStatus.SC_FORBIDDEN, "ForbiddenException", "Forbidden Request"),
ExceptionDetailsHolder(HttpStatus.SC_NOT_FOUND, "NotFoundException", "Request Not Found"),
ExceptionDetailsHolder(HttpStatus.SC_INTERNAL_SERVER_ERROR, "InternalServerErrorException", "Internal server error occured")
ExceptionDetailsHolder(HttpStatus.SC_BAD_REQUEST, "BadRequestException", "Bad Request"),
ExceptionDetailsHolder(HttpStatus.SC_UNAUTHORIZED, "UnauthorizedException", "Unauthorized Request"),
ExceptionDetailsHolder(HttpStatus.SC_FORBIDDEN, "ForbiddenException", "Forbidden Request"),
ExceptionDetailsHolder(HttpStatus.SC_NOT_FOUND, "NotFoundException", "Request Not Found"),
ExceptionDetailsHolder(HttpStatus.SC_INTERNAL_SERVER_ERROR, "InternalServerErrorException", "Internal server error occured")
)

fun getExceptionClassFor(statuses: List<Int>) = statuses.map { status -> allExceptionDetailsHolder.find { it.status == status }?.exceptionClassName ?: "InternalServerErrorException" }
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package apifi.parser.models
package apifi.models

data class Model(
val name: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package apifi.parser.models
package apifi.models

import io.swagger.v3.oas.models.PathItem
import io.swagger.v3.oas.models.security.SecurityScheme
import org.apache.http.HttpStatus

data class Spec(val paths: List<Path>, val models: List<Model>, val securityRequirements: List<String>, val securityDefinitions: List<SecurityDefinition>)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package apifi.parser

import apifi.helpers.toCodeGenModel
import apifi.helpers.toTitleCase
import apifi.parser.models.Model
import apifi.parser.models.Property
import apifi.models.Model
import apifi.models.Property
import io.swagger.v3.oas.models.media.ArraySchema
import io.swagger.v3.oas.models.media.ObjectSchema
import io.swagger.v3.oas.models.media.Schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package apifi.parser
import apifi.helpers.toCamelCase
import apifi.helpers.toCodeGenModel
import apifi.helpers.toTitleCase
import apifi.parser.models.*
import apifi.models.*
import io.swagger.v3.oas.models.PathItem
import io.swagger.v3.oas.models.Paths

Expand All @@ -20,7 +20,8 @@ object PathsParser {
val responses = ResponseBodyParser.parse(operation.responses, operationSpecifier)
models.addAll(request?.second ?: emptyList())
models.addAll(responses?.second ?: emptyList())
Operation(httpMethod, operation.operationId ?: toCamelCase(httpMethod.toString()),
Operation(httpMethod, operation.operationId
?: toCamelCase(httpMethod.toString()),
operation.tags, params, request?.first, responses?.first)
}
Path(endpoint, operations)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package apifi.parser

import apifi.parser.ModelParser.parseReference
import apifi.parser.ModelParser.shouldCreateModel
import apifi.parser.models.Model
import apifi.parser.models.Request
import apifi.models.Model
import apifi.models.Request
import io.swagger.v3.oas.models.media.ArraySchema
import io.swagger.v3.oas.models.media.Schema
import io.swagger.v3.oas.models.parameters.RequestBody
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package apifi.parser

import apifi.parser.ModelParser.parseReference
import apifi.parser.models.Model
import apifi.parser.models.Response
import apifi.models.Model
import apifi.models.Response
import io.swagger.v3.oas.models.media.ArraySchema
import io.swagger.v3.oas.models.media.Schema
import io.swagger.v3.oas.models.responses.ApiResponses
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package apifi.parser

import apifi.parser.models.SecurityDefinition
import apifi.parser.models.SecurityDefinitionType
import apifi.parser.models.Spec
import apifi.models.SecurityDefinition
import apifi.models.SecurityDefinitionType
import apifi.models.Spec
import io.swagger.v3.oas.models.OpenAPI

object SpecFileParser {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package apifi.codegen

import apifi.parser.models.*
import apifi.models.*
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.TypeSpec
import io.kotlintest.matchers.collections.shouldContain
import io.kotlintest.matchers.collections.shouldContainExactlyInAnyOrder
import io.kotlintest.matchers.string.shouldContain
import io.kotlintest.shouldBe
import io.kotlintest.specs.DescribeSpec
import io.kotest.matchers.collections.shouldContain
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder
import io.kotest.matchers.shouldBe
import io.kotest.core.spec.style.DescribeSpec
import io.swagger.v3.oas.models.PathItem

class ApiBuilderTest : DescribeSpec({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package apifi.codegen

import apifi.parser.SpecFileParser
import io.kotlintest.matchers.collections.shouldNotContainInOrder
import io.kotlintest.shouldBe
import io.kotlintest.specs.DescribeSpec
import io.kotest.matchers.collections.shouldNotContainInOrder
import io.kotest.matchers.shouldBe
import io.kotest.core.spec.style.DescribeSpec
import io.swagger.v3.parser.OpenAPIV3Parser
import org.apache.commons.io.FileUtils

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package apifi.codegen

import apifi.parser.models.*
import io.kotlintest.shouldBe
import io.kotlintest.specs.DescribeSpec
import apifi.models.*
import io.kotest.matchers.shouldBe
import io.kotest.core.spec.style.DescribeSpec
import io.swagger.v3.oas.models.PathItem

class ControllerInterfaceBuilderTest : DescribeSpec({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package apifi.codegen

import apifi.codegen.exceptions.ExceptionDetailsHolder
import apifi.codegen.exceptions.ExceptionFileBuilder
import com.squareup.kotlinpoet.TypeSpec
import io.kotlintest.matchers.collections.shouldNotContainInOrder
import io.kotlintest.matchers.string.shouldContain
import io.kotlintest.shouldBe
import io.kotlintest.specs.DescribeSpec
import io.kotest.matchers.collections.shouldNotContainInOrder
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.shouldBe
import io.kotest.core.spec.style.DescribeSpec

class ExceptionFileBuilderTest : DescribeSpec( {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package apifi.codegen

import apifi.parser.models.Param
import apifi.parser.models.ParamType
import io.kotlintest.shouldBe
import io.kotlintest.specs.DescribeSpec
import apifi.models.Param
import apifi.models.ParamType
import io.kotest.matchers.shouldBe
import io.kotest.core.spec.style.DescribeSpec

class HeaderBuilderTest : DescribeSpec({
describe("HeaderBuilder") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package apifi.codegen

import apifi.parser.models.*
import apifi.models.Model
import apifi.models.Property
import com.squareup.kotlinpoet.TypeSpec
import io.kotlintest.shouldBe
import io.kotlintest.specs.DescribeSpec
import io.kotest.matchers.shouldBe
import io.kotest.core.spec.style.DescribeSpec

class ModelFileBuilderTest : DescribeSpec({

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package apifi.codegen

import io.kotlintest.specs.StringSpec
import io.kotlintest.shouldBe
import apifi.codegen.exceptions.Non200ResponseHandler
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe

class Non200ResponseHandlerTest : StringSpec() {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package apifi.codegen

import apifi.parser.models.Param
import apifi.parser.models.ParamType
import io.kotlintest.shouldBe
import io.kotlintest.specs.DescribeSpec
import apifi.models.Param
import apifi.models.ParamType
import io.kotest.matchers.shouldBe
import io.kotest.core.spec.style.DescribeSpec

class PathVariableBuilderTest : DescribeSpec({
describe("PathVariableBuilder") {
Expand Down
Loading

0 comments on commit 92edcb4

Please sign in to comment.