Generate Kotlin DTOs from one interface
KReplica is a KSP plugin that creates type-safe sealed hierarchies for Create, Data, and Patch variants—eliminating boilerplate and runtime surprises.
package io.availe.demo
import io.availe.Replicate
import io.availe.models.DtoVariant
import java.util.UUID
@Replicate.Model(variants = [DtoVariant.DATA, DtoVariant.CREATE, DtoVariant.PATCH])
private interface UserAccount {
@Replicate.Property(exclude = [DtoVariant.CREATE])
val id: UUID
val email: String
val displayName: String?
}
// Generated by KReplica. Do not edit.
package io.availe.demo
import io.availe.models.KReplicaCreateVariant
import io.availe.models.KReplicaDataVariant
import io.availe.models.KReplicaPatchVariant
import io.availe.models.Patchable
import java.util.UUID
import kotlin.String
/**
* A sealed hierarchy representing all variants of the UserAccount data model.
*/
public sealed interface UserAccountSchema {
public data class Data(
public val id: UUID,
public val email: String,
public val displayName: String?,
) : UserAccountSchema,
KReplicaDataVariant<UserAccountSchema>
public data class CreateRequest(
public val email: String,
public val displayName: String?,
) : UserAccountSchema,
KReplicaCreateVariant<UserAccountSchema>
public data class PatchRequest(
public val id: Patchable<UUID> = Patchable.Unchanged,
public val email: Patchable<String> = Patchable.Unchanged,
public val displayName: Patchable<String?> = Patchable.Unchanged,
) : UserAccountSchema,
KReplicaPatchVariant<UserAccountSchema>
}
- Generates Create, Data & Patch from one interface.
-
Sealed hierarchies allow exhaustive
when
expressions. - Generate versioned DTOs with ease.
- Compile-time codegen powered via KSP and Kotlin Poet.
Bulletproof Your APIs
KReplica uses sealed hierarchies to enable exhaustive when
expressions. If you add a new API
version, your code won't compile until you handle it—no more runtime surprises.
fun handleAllDataVariants(data: UserAccountSchema.DataVariant) {
when (data) {
is UserAccountSchema.V1.Data -> println("Handle V1 Data: ${data.id}")
is UserAccountSchema.V2.Data -> println("Handle V2 Data: ${data.email}")
}
}
fun handleV2Variants(user: UserAccountSchema.V2) {
when (user) {
is UserAccountSchema.V2.CreateRequest -> println("Handle V2 Create: ${user.email}")
is UserAccountSchema.V2.Data -> println("Handle V2 Data: ${user.id}")
is UserAccountSchema.V2.PatchRequest -> println("Handle V2 Patch")
}
}
fun handleAllUserTypes(user: UserAccountSchema) {
when (user) {
is UserAccountSchema.V1.CreateRequest -> println("Handle V1 Create")
is UserAccountSchema.V1.Data -> println("Handle V1 Data")
is UserAccountSchema.V1.PatchRequest -> println("Handle V1 Patch")
is UserAccountSchema.V2.CreateRequest -> println("Handle V2 Create")
is UserAccountSchema.V2.Data -> println("Handle V2 Data")
is UserAccountSchema.V2.PatchRequest -> println("Handle V2 Patch")
}
}
Setup
Add the KSP and KReplica plugins to your module's build.gradle.kts
file to start generating DTOs
automatically.
plugins {
// Use a KSP version compatible with your Kotlin version
id("com.google.devtools.ksp") version "..."
id("io.availe.kreplica") version "5.0.0"
}