Runtime Reflection
Table of contents
note
If your classes are @Serializable, use the Serialization-Based Generator
instead — it works
on all platforms without reflection.
For JVM-only scenarios with classes you don’t own or can’t annotate, use
ReflectionClassJsonSchemaGenerator
and ReflectionFunctionCallingSchemaGenerator
with Kotlin reflection.
Why Runtime Generation?
Primary use case: Third-party library classes
The compile-time (KSP) approach requires you to annotate classes with @Schema, which isn’t possible for:
- Library classes (Spring entities, Ktor models, database classes)
- Framework-provided models
- Classes from dependencies you don’t control
Runtime generation solves this by using reflection to analyze any class at runtime.
important
Limitations:
- KDoc annotations are not available at runtime
- Function parameter defaults (e.g.,
fun foo(x: Int = 5)) cannot be extracted via reflection - Data class property defaults (e.g.,
data class Config(val port: Int = 8080)) ARE supported
Usage
kotlin
// Works with ANY class, even from third-party libraries
import com.thirdparty.library.User // Not your code!
val generator = me.kpavlov.kt.schema.generator.json.ReflectionClassJsonSchemaGenerator.Default
val schema: JsonSchema = generator.generateSchema(User::class)
val schemaString: String = generator.generateSchemaString(User::class)Add dependency: me.kpavlov.kt.schema:kt-schema-generator-json:<version>
See Also
- Generated Schemas — what the schema looks like for each Kotlin type
- Function Calling
—
ReflectionFunctionCallingSchemaGeneratorfor LLM tool schemas - Serialization-Based Schema Generation
— multiplatform alternative for
@Serializableclasses