
Generates code from API formats by transforming them into an intermediate representation and utilizing a code generator. Supports WSDL, Swagger, OpenAPI, with customizable plugin mechanisms.
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.
API formats:
Code generators:
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:
@Serializable annotation to Kotlin classesRoute functionsCoRouterFunctionDsl functional endpointsYou also need to call usingKotlinSourceSet to connect the generated code to a Gradle SourceSet.
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.
flowchart TD
Input(Schema File) -->|Read Schema|M
M --> |Fir-Transformer|M
M[Model] --> IR(IR Tree)
IR -->|Ir-Transformer|IR(IR Tree)
IR -->C(CodeGen Tree)
C -->|CodeGen-Transformer|C(CodeGen Tree)
C -->K(Kotlin Poet)
K --> O(Output File)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.
API formats:
Code generators:
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:
@Serializable annotation to Kotlin classesRoute functionsCoRouterFunctionDsl functional endpointsYou also need to call usingKotlinSourceSet to connect the generated code to a Gradle SourceSet.
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.
flowchart TD
Input(Schema File) -->|Read Schema|M
M --> |Fir-Transformer|M
M[Model] --> IR(IR Tree)
IR -->|Ir-Transformer|IR(IR Tree)
IR -->C(CodeGen Tree)
C -->|CodeGen-Transformer|C(CodeGen Tree)
C -->K(Kotlin Poet)
K --> O(Output File)