Skip to content

hfhbd/kfx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

197 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

import io.github.hfhbd.kfx.openapi.OpenApi

plugins {
    id("io.github.hfhbd.kfx") version "LATEST"
}

kfx {
    register<OpenApi>("myApi") {
        files.from("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 @Serializable annotation to Kotlin classes
  • ktorClient: Generates ktor client functions
  • ktorServer: Generates ktor server Route functions
  • springServer: Generates Spring webflux CoRouterFunctionDsl functional 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.

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)
Loading

About

OpenAPI generator for Kotlin

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors