feat: schema configurator to enable field name overrides and transient field omission (#302)
This commit is contained in:
@ -4,6 +4,7 @@ import io.bkbn.kompendium.core.metadata.GetInfo
|
||||
import io.bkbn.kompendium.core.plugin.NotarizedApplication
|
||||
import io.bkbn.kompendium.core.plugin.NotarizedRoute
|
||||
import io.bkbn.kompendium.core.routes.redoc
|
||||
import io.bkbn.kompendium.json.schema.KotlinXSchemaConfigurator
|
||||
import io.bkbn.kompendium.json.schema.definition.TypeDefinition
|
||||
import io.bkbn.kompendium.oas.payload.Parameter
|
||||
import io.bkbn.kompendium.oas.serialization.KompendiumSerializersModule
|
||||
@ -42,6 +43,9 @@ private fun Application.mainModule() {
|
||||
}
|
||||
install(NotarizedApplication()) {
|
||||
spec = baseSpec
|
||||
// Adds support for @Transient and @SerialName
|
||||
// If you are not using them this is not required.
|
||||
schemaConfigurator = KotlinXSchemaConfigurator()
|
||||
}
|
||||
routing {
|
||||
redoc(pageTitle = "Simple API Docs")
|
||||
|
@ -1,9 +1,12 @@
|
||||
package io.bkbn.kompendium.playground
|
||||
|
||||
import com.google.gson.annotations.Expose
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import io.bkbn.kompendium.core.metadata.GetInfo
|
||||
import io.bkbn.kompendium.core.plugin.NotarizedApplication
|
||||
import io.bkbn.kompendium.core.plugin.NotarizedRoute
|
||||
import io.bkbn.kompendium.core.routes.redoc
|
||||
import io.bkbn.kompendium.json.schema.SchemaConfigurator
|
||||
import io.bkbn.kompendium.json.schema.definition.TypeDefinition
|
||||
import io.bkbn.kompendium.oas.payload.Parameter
|
||||
import io.bkbn.kompendium.playground.util.ExampleResponse
|
||||
@ -21,6 +24,10 @@ import io.ktor.server.routing.Route
|
||||
import io.ktor.server.routing.get
|
||||
import io.ktor.server.routing.route
|
||||
import io.ktor.server.routing.routing
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.full.memberProperties
|
||||
import kotlin.reflect.jvm.javaField
|
||||
|
||||
fun main() {
|
||||
embeddedServer(
|
||||
@ -38,6 +45,7 @@ private fun Application.mainModule() {
|
||||
}
|
||||
install(NotarizedApplication()) {
|
||||
spec = baseSpec
|
||||
schemaConfigurator = GsonSchemaConfigurator()
|
||||
}
|
||||
routing {
|
||||
redoc(pageTitle = "Simple API Docs")
|
||||
@ -71,3 +79,27 @@ private fun Route.locationDocumentation() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Adds support for Expose and SerializedName annotations,
|
||||
// if you are not using them this is not required
|
||||
class GsonSchemaConfigurator(
|
||||
private val excludeFieldsWithoutExposeAnnotation: Boolean = false
|
||||
): SchemaConfigurator.Default() {
|
||||
|
||||
override fun serializableMemberProperties(clazz: KClass<*>): Collection<KProperty1<out Any, *>> {
|
||||
return if(excludeFieldsWithoutExposeAnnotation) clazz.memberProperties
|
||||
.filter { it.hasJavaAnnotation<Expose>() }
|
||||
else clazz.memberProperties
|
||||
}
|
||||
|
||||
override fun serializableName(property: KProperty1<out Any, *>): String =
|
||||
property.getJavaAnnotation<SerializedName>()?.value?: property.name
|
||||
}
|
||||
|
||||
private inline fun <reified T: Annotation> KProperty1<*, *>.hasJavaAnnotation(): Boolean {
|
||||
return javaField?.isAnnotationPresent(T::class.java)?: false
|
||||
}
|
||||
|
||||
private inline fun <reified T: Annotation> KProperty1<*, *>.getJavaAnnotation(): T? {
|
||||
return javaField?.getDeclaredAnnotation(T::class.java)
|
||||
}
|
||||
|
@ -1,11 +1,14 @@
|
||||
package io.bkbn.kompendium.playground
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore
|
||||
import com.fasterxml.jackson.annotation.JsonInclude
|
||||
import com.fasterxml.jackson.annotation.JsonProperty
|
||||
import com.fasterxml.jackson.databind.SerializationFeature
|
||||
import io.bkbn.kompendium.core.metadata.GetInfo
|
||||
import io.bkbn.kompendium.core.plugin.NotarizedApplication
|
||||
import io.bkbn.kompendium.core.plugin.NotarizedRoute
|
||||
import io.bkbn.kompendium.core.routes.redoc
|
||||
import io.bkbn.kompendium.json.schema.SchemaConfigurator
|
||||
import io.bkbn.kompendium.json.schema.definition.TypeDefinition
|
||||
import io.bkbn.kompendium.oas.payload.Parameter
|
||||
import io.bkbn.kompendium.playground.util.ExampleResponse
|
||||
@ -23,6 +26,10 @@ import io.ktor.server.routing.Route
|
||||
import io.ktor.server.routing.get
|
||||
import io.ktor.server.routing.route
|
||||
import io.ktor.server.routing.routing
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.full.memberProperties
|
||||
import kotlin.reflect.jvm.javaField
|
||||
|
||||
fun main() {
|
||||
embeddedServer(
|
||||
@ -41,6 +48,7 @@ private fun Application.mainModule() {
|
||||
}
|
||||
install(NotarizedApplication()) {
|
||||
spec = baseSpec
|
||||
schemaConfigurator = JacksonSchemaConfigurator()
|
||||
}
|
||||
routing {
|
||||
redoc(pageTitle = "Simple API Docs")
|
||||
@ -75,3 +83,26 @@ private fun Route.locationDocumentation() {
|
||||
}
|
||||
}
|
||||
|
||||
// Adds support for JsonIgnore and JsonProperty annotations,
|
||||
// if you are not using them this is not required
|
||||
// This also does not support class level configuration
|
||||
private class JacksonSchemaConfigurator: SchemaConfigurator.Default() {
|
||||
|
||||
override fun serializableMemberProperties(clazz: KClass<*>): Collection<KProperty1<out Any, *>> =
|
||||
clazz.memberProperties
|
||||
.filterNot {
|
||||
it.hasJavaAnnotation<JsonIgnore>()
|
||||
}
|
||||
|
||||
override fun serializableName(property: KProperty1<out Any, *>): String =
|
||||
property.getJavaAnnotation<JsonProperty>()?.value?: property.name
|
||||
|
||||
}
|
||||
|
||||
private inline fun <reified T: Annotation> KProperty1<*, *>.hasJavaAnnotation(): Boolean {
|
||||
return javaField?.isAnnotationPresent(T::class.java)?: false
|
||||
}
|
||||
|
||||
private inline fun <reified T: Annotation> KProperty1<*, *>.getJavaAnnotation(): T? {
|
||||
return javaField?.getDeclaredAnnotation(T::class.java)
|
||||
}
|
||||
|
Reference in New Issue
Block a user