All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.lancom.openapi.refs.Reference.kt Maven / Gradle / Ivy

Go to download

This open-source project provides an OpenAPI 3.0 Parser implemented in Kotlin, utilizing immutable data classes

There is a newer version: 2.1.1
Show newest version
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))
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy