kfx
Generate code from supported API formats.
kfx parses API files, transforms them into an intermediate representation (IR) and uses a code generator to actually create the code by consuming the IR.
kfx supports some API formats and code generators out of the box, but due to its plugin mechanisms it's easy to support other api formats or code generators too.
What's included
API formats:
-
WSDL (including XSD)
-
Swagger
-
OpenAPI
Code generators:
-
Kotlin classes using Kotlinx.serialization
-
Ktor client functions
-
Ktor server functions
-
Spring server webflux functional endpoints
Gradle plugin
plugins {
id("io.github.hfhbd.kfx") version "LATEST"
}
kfx {
register("myApi", OpenApi::class) {
files.from(file("myApi.json"))
dependencies {
compiler(kotlinClasses())
compiler(kotlinxJson())
compiler(ktorClient())
}
usingKotlinSourceSet(kotlin.sourceSets.main)
}
}
To generate code, you need to call register/create and configure the compilation. In the dependencies block, you define what code should be generated:
-
kotlinClasses: Kotlin classes only
-
kotlinxJson: Adds
@Serializableannotation to Kotlin classes -
ktorClient: Generates ktor client functions
-
ktorServer: Generates ktor server
Routefunctions -
springServer: Generates Spring webflux
CoRouterFunctionDslfunctional endpoints
You also need to call usingKotlinSourceSet to connect the generated code to a Gradle SourceSet.
Custom Transformer
Often, api files need some transformation, to change the Kotlin type, to add some documentation or custom types, or just to fix some wrong apis until a new version will be released.
There are three kind of transformers, format specific ones, IR transformers and code gen transformers. The transformers are loaded using JVM ServiceLoader mechanism and needs to be added to the format dependency configuration.
All modules:
This module transforms the wsdl to IR, which will be used by a CodeGenerator to generate actual classes.
This module creates the CodegenTree from the IRTree.
Integrates the WSDL generator with a Kotlin JVM and Kotlin Multiplatform project.
This module adds OData transformation support.
This module adds the package name to all IR classes if there is no package name yet.
This module generates the Kotlin base classes.
This module contains helper methods for Kotlin based generators using kotlinpoet.
This module generates the Ktor client functions.
This module generates the Ktor server functions.
This module generates the Ktor client functions.
Runtime artifact containing the soap action Routing.
This module contains util functions shared between ktor-client and ktor-server.
Runtime artifact containing the OAuth2Token serializable class.
This module contains the data model to read a openAPI file.
Runtime artifact containing the soap.Envelope serializable class.
This module adds the SOAP 1.1 io.github.hfhbd.kfx.soap11.Envelope wrapper to each operation.
This module generates the Spring server webflux functional endpoints.
This module adds app.softwork.validation support.
This module contains the data model to read a wsdl file.
This module contains the data model to read a XSD file.