de.lancom.openapi.refs.Reference.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openapi-parser Show documentation
Show all versions of openapi-parser Show documentation
This open-source project provides an OpenAPI 3.0 Parser implemented in Kotlin, utilizing immutable data classes
package de.lancom.openapi.refs
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.annotation.JsonSerialize
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.fasterxml.jackson.databind.ser.std.StdSerializer
import de.lancom.openapi.field.Field
import de.lancom.openapi.jackson.Wrapper
@JsonSerialize(using = Reference.Companion.Serializer::class)
@JsonDeserialize(using = Reference.Companion.Deserializer::class)
data class Reference(
val _ref: Field,
) : ReferenceOrInstance {
val ref: String? = _ref.orNull
companion object {
class Serializer : StdSerializer>(Reference::class.java) {
override fun serialize(value: Reference<*>?, gen: JsonGenerator?, provider: SerializerProvider?) {
gen!!.writeObject(
mapOf(
"\$ref" to value!!.ref
)
)
}
}
class Deserializer : StdDeserializer>(Reference::class.java) {
override fun deserialize(p: JsonParser?, ctxt: DeserializationContext?): Reference<*> {
val node: JsonNode = p!!.codec!!.readTree(p)!!
val wrapper = Wrapper(node)
val ref = wrapper["\$ref"].getString().getOrError()
return Reference(ref)
}
}
operator fun invoke(ref: String): Reference {
return Reference(Field(ref))
}
}
}