A powerful Gradle plugin that transforms OpenAPI v3 specifications into production-ready Kotlin Ktor client code. You can customize the generated clients and models to match your project's specific needs.
- JDK 17+
- Gradle 9+
Add the plugin to your build.gradle.kts:
plugins {
id("org.litote.openapi.ktor.client.generator.gradle") version "<last version>"
}Configure the plugin in your build.gradle.kts:
apiClientGenerator {
generators {
create("openapi") { // create a new task name: generateOpenapi
outputDirectory = file("build/generated")
openApiFile = file("src/main/openapi/openapi.json")
basePackage = "com.example.api"
// Optional: Add more configuration options below
}
// You can create multiple tasks with different names
}
}For a complete example with all available options, see e2e/build.gradle.kts.
Run the generation task directly:
./gradlew generateOpenapiOr trigger generation as part of the build process:
./gradlew buildThis will generate Ktor client code based on your OpenAPI specification and plugin configuration. The generated code will be placed in the configured outputDirectory.
| Property | Description | Default value | Allowed values |
|---|---|---|---|
generators |
Generators configuration | {} |
Any configuration |
skip |
Skip all clients generation | false | Boolean |
| Property | Description | Default value | Allowed values |
|---|---|---|---|
openApiFile |
OpenAPI v3 source file | file("src/main/openapi/${name}.json") |
Any existing OpenAPI file |
outputDirectory |
Target directory for generated sources (a src/main/kotlin subdirectory will be added) |
file("build/api-${name}") |
Any relative directory |
basePackage |
Base package for all generated classes | org.example |
Any valid package name |
allowedPaths |
Restrict generation to a subset of OpenAPI paths | empty (all paths are generated) | Any subset of paths defined in the OpenAPI spec |
modulesIds |
Extra generation modules to enable | Empty (no modules) | UnknownEnumValueModule, LoggingSl4jModule |
skip |
Skip this client generation | false | Boolean |
If you get an error with this message, the generator tasks have implicit dependencies with other tasks. (see https://docs.gradle.org/current/userguide/validation_problems.html#implicit_dependency)
Add the dependencies explicitly in the gradle.kts file:
project
.tasks
.named { name ->
//any condition to match the tasks names
name.contains("whatever")
}
.configureEach {
project.tasks.withType(org.litote.openapi.ktor.client.generator.plugin.GenerateTask::class.java).forEach {
dependsOn(it)
}
}Generated code is not linted. Just ignore the errors by adding a .editorconfig file to your project,
for example for ktlint:
[build/**/*]
ktlint = disabled