Skip to main content

Using the OpenAPI Generator

Basic Usage​

import typr.openapi.codegen._

val spec = OpenApiParser.parse(Paths.get("api.yaml"))

val options = OpenApiCodegenOptions(
pkg = "com.example.api",
lang = LangJava,
serverFramework = Some(ServerFramework.JaxRs),
clientFramework = Some(ClientFramework.JdkHttpClient)
)

val files = ApiCodegen.generate(spec, options)

files.foreach { file =>
Files.write(file.path, file.content.getBytes)
}

Configuration Options​

Language Selection​

// Java
lang = LangJava

// Kotlin
lang = LangKotlin

// Scala
lang = LangScala(scalaVersion = "3")

Server Frameworks​

// JAX-RS (Java/Kotlin)
serverFramework = Some(ServerFramework.JaxRs)

// Spring Boot (Java/Kotlin/Scala)
serverFramework = Some(ServerFramework.Spring)

// Quarkus Reactive (Java/Kotlin) - uses Mutiny Uni<T>
serverFramework = Some(ServerFramework.QuarkusReactive)

// Http4s (Scala) - uses Cats Effect IO
serverFramework = Some(ServerFramework.Http4s)

Client Frameworks​

// JDK HttpClient (all languages)
clientFramework = Some(ClientFramework.JdkHttpClient)

// Http4s Client (Scala only)
clientFramework = Some(ClientFramework.Http4s)

Generated File Structure​

com/example/api/
β”œβ”€β”€ api/
β”‚ β”œβ”€β”€ PetsApi.java # Shared interface
β”‚ β”œβ”€β”€ PetsApiServer.java # Server with annotations
β”‚ β”œβ”€β”€ PetsApiClient.java # HTTP client
β”‚ β”œβ”€β”€ Ok.java # Response type
β”‚ β”œβ”€β”€ NotFound.java # Response type
β”‚ └── Response200404.java # Sealed interface
└── model/
β”œβ”€β”€ Pet.java # Schema types
β”œβ”€β”€ PetCreate.java
β”œβ”€β”€ PetId.java # Type-safe ID
└── Error.java

Integration with Build Tools​

SBT (Scala)​

lazy val generateApi = taskKey[Seq[File]]("Generate API code")

generateApi := {
val spec = OpenApiParser.parse(baseDirectory.value / "api.yaml")
val options = OpenApiCodegenOptions(...)
ApiCodegen.generate(spec, options).map { file =>
val target = (Compile / sourceManaged).value / file.relativePath
IO.write(target, file.content)
target
}
}

Compile / sourceGenerators += generateApi

Gradle (Kotlin/Java)​

tasks.register("generateApi") {
doLast {
// Call Typr generator via CLI or embedded
}
}

tasks.named("compileKotlin") {
dependsOn("generateApi")
}