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
Gradle plugin
plugins {
id("io.github.hfhbd.kfx") version "LATEST"
}
kfx {
register("myApi", OpenApi::class) {
files.from(file("myApi.json"))
dependencies {
compiler(kotlin())
compiler(kotlinxJson())
compiler(ktorClient())
}
sourceSets.main {
usingSourceSet(kotlin)
}
}
}
To generate code, you need to call register/create and configure the compilation. In the dependencies block, you define what code should be generated:
-
kotlin: Kotlin classes only
-
kotlinxJson: Adds
@Serializable
annotation to Kotlin classes -
ktorClient: Generates ktor client functions
-
ktorServer: Generates ktor server
Route
functions
You also need to call usingSourceSet
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 removes the suffix Type
of a variable name, if possible.
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.
This module adds the SOAP io.github.hfhbd.kfx.soap.Envelope wrapper to each operation.
Runtime artifact containing the soap.Envelope
serializable class.
This module adds app.softwork.validation
support.
This module contains the data model to read a wsdl file.