-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Remove Response and ResponseType classes, won't be needed * Remove Response and ResponseType classes, won't be needed * Remove security dependencies from codegen, available in parsed object * Remove default injection of httpRequest param in all controller routes which was done for basic auth header value * Basic structure for generating exception classes and their default global exception handlers * Basic structure for generating exception classes and their default global exception handlers * Introduce status in Response parsed from spec * Add @throws annotation to controller with exceptions mapping to non 200 responses of the operation * Add test for Non200ResponseHandler * Add missing annotations to exception handler class
- Loading branch information
1 parent
bfdd88c
commit 4a44ec9
Showing
25 changed files
with
339 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,21 @@ | ||
package apifi.codegen | ||
|
||
import apifi.codegen.security.BasicAuthSecurityStubBuilder | ||
import apifi.codegen.security.BearerAuthSecurityStubBuilder | ||
import apifi.parser.models.SecurityDefinition | ||
import apifi.parser.models.SecurityDefinitionType | ||
import apifi.parser.models.Spec | ||
import com.squareup.kotlinpoet.FileSpec | ||
import com.squareup.kotlinpoet.TypeSpec | ||
|
||
object CodeGenerator { | ||
fun generate(spec: Spec, basePackageName: String): List<FileSpec> { | ||
val modelFiles: List<FileSpec> = if(spec.models.isNotEmpty()) listOf(ModelFileBuilder.build(spec.models, basePackageName)) else emptyList() | ||
val responseModelFile = ResponseModelBuilder.build(basePackageName) | ||
val modelMapping = (modelFiles + responseModelFile).flatMap { it.members.mapNotNull { m -> (m as TypeSpec).name }.map { name -> name to "${it.packageName}.$name" } } | ||
val securityFiles = spec.securityDefinitions.fold<SecurityDefinition, Map<SecurityDefinition, FileSpec>>(mapOf(), { acc, securityDefinition -> | ||
when(securityDefinition.type) { | ||
SecurityDefinitionType.BASIC_AUTH -> acc + mapOf(securityDefinition to BasicAuthSecurityStubBuilder.build(basePackageName)) | ||
SecurityDefinitionType.BEARER -> acc + mapOf(securityDefinition to BearerAuthSecurityStubBuilder.build(basePackageName)) | ||
} | ||
}) | ||
|
||
val securityDependencies = securityFiles | ||
.filter { spec.securityRequirements.contains(it.key.name) } | ||
.map { (def, spec) -> SecurityDependency((spec.members.first() as TypeSpec).name!!, spec.packageName, def.type) } | ||
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 } | ||
|
||
val apiClassFiles = apiGroups.map { ApiBuilder.build(it.key!!, it.value, securityDependencies, basePackageName, modelMapping) } | ||
val apiClassFiles = apiGroups.map { ApiBuilder.build(it.key!!, it.value, basePackageName, modelMapping) } | ||
|
||
val exceptionClassesAndHandlerFiles = Non200ResponseHandler.generateExceptionClassesAndHandlers(basePackageName) | ||
|
||
return (apiClassFiles + modelFiles + responseModelFile) | ||
return (apiClassFiles + modelFiles + exceptionClassesAndHandlerFiles) | ||
} | ||
|
||
} | ||
|
||
data class SecurityDependency(val name: String, val packageName: String, val securityDefinitionType: SecurityDefinitionType) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package apifi.codegen | ||
|
||
import apifi.helpers.toKotlinPoetType | ||
import com.squareup.kotlinpoet.* | ||
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy | ||
|
||
object ExceptionFileBuilder { | ||
|
||
fun build(exception: ExceptionDetailsHolder, basePackageName: String): FileSpec { | ||
val packageName = "$basePackageName.exceptions" | ||
val exceptionClassName = exception.exceptionClassName | ||
|
||
val builder = FileSpec.builder(packageName, "$exceptionClassName.kt") | ||
builder.addType( | ||
TypeSpec.classBuilder(ClassName(packageName, exceptionClassName)) | ||
.superclass(Exception::class) | ||
.addSuperclassConstructorParameter("%L", "message") | ||
.primaryConstructor( | ||
FunSpec.constructorBuilder() | ||
.addParameter(ParameterSpec.builder("message", String::class).build()).build() | ||
).build()) | ||
|
||
builder.addType( | ||
TypeSpec.classBuilder(ClassName(packageName, "Global${exceptionClassName}Handler")) | ||
.addAnnotation(ClassName("javax.inject", "Singleton")) | ||
.addAnnotation(ClassName("io.micronaut.http.annotation", "Produces")) | ||
.addAnnotation( | ||
AnnotationSpec.builder(ClassName("io.micronaut.context.annotation", "Requires")) | ||
.addMember("classes = [%T::class, %T::class]", ClassName(packageName, exceptionClassName), ClassName("io.micronaut.http.server.exceptions", "ExceptionHandler")) | ||
.build()) | ||
.addSuperinterface( | ||
ClassName("io.micronaut.http.server.exceptions", "ExceptionHandler") | ||
.parameterizedBy(ClassName(packageName, exceptionClassName), | ||
ClassName("io.micronaut.http", "HttpResponse").parameterizedBy("String".toKotlinPoetType())) | ||
) | ||
.addFunction(FunSpec.builder("handle") | ||
.addParameter("request", ClassName("io.micronaut.http", "HttpRequest").parameterizedBy("Any".toKotlinPoetType()).copy(nullable = true)) | ||
.addParameter("exception", ClassName(packageName, exceptionClassName).copy(nullable = true)) | ||
.returns(ClassName("io.micronaut.http", "HttpResponse").parameterizedBy("String".toKotlinPoetType())) | ||
.addStatement("val msg = exception?.conversionError?.cause?.localizedMessage ?: \"${exception.defaultExceptionMessage}\"") | ||
.addStatement("HttpResponse.status<String>(HttpStatus.valueOf(${exception.status}), msg)") | ||
.build() | ||
) | ||
.build() | ||
) | ||
return builder.build() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package apifi.codegen | ||
|
||
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") | ||
) | ||
|
||
fun getExceptionClassFor(statuses: List<Int>) = statuses.map { status -> allExceptionDetailsHolder.find { it.status == status }?.exceptionClassName ?: "InternalServerErrorException" } | ||
|
||
fun generateExceptionClassesAndHandlers(basePackageName: String) = | ||
allExceptionDetailsHolder.map { exception -> | ||
ExceptionFileBuilder.build(exception, basePackageName) | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
data class ExceptionDetailsHolder( | ||
val status: Int, | ||
val exceptionClassName: String, | ||
val defaultExceptionMessage: String | ||
) |
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
src/main/kotlin/apifi/codegen/security/BasicAuthSecurityStubBuilder.kt
This file was deleted.
Oops, something went wrong.
32 changes: 0 additions & 32 deletions
32
src/main/kotlin/apifi/codegen/security/BearerAuthSecurityStubBuilder.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.