|
| 1 | +package org.springdoc.core.converters |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JavaType |
| 4 | +import io.swagger.v3.core.converter.AnnotatedType |
| 5 | +import io.swagger.v3.core.converter.ModelConverter |
| 6 | +import io.swagger.v3.core.converter.ModelConverterContext |
| 7 | +import io.swagger.v3.oas.models.media.Schema |
| 8 | +import org.springdoc.core.providers.ObjectMapperProvider |
| 9 | +import kotlin.reflect.full.findAnnotation |
| 10 | +import kotlin.reflect.full.primaryConstructor |
| 11 | +import kotlin.reflect.jvm.jvmErasure |
| 12 | + |
| 13 | +class KotlinInlineClassUnwrappingConverter( |
| 14 | + private val objectMapperProvider: ObjectMapperProvider |
| 15 | +) : ModelConverter { |
| 16 | + |
| 17 | + override fun resolve( |
| 18 | + type: AnnotatedType?, |
| 19 | + context: ModelConverterContext?, |
| 20 | + chain: Iterator<ModelConverter> |
| 21 | + ): Schema<*>? { |
| 22 | + if (type?.type == null || context == null || !chain.hasNext()) { |
| 23 | + return null |
| 24 | + } |
| 25 | + val javaType: JavaType = objectMapperProvider.jsonMapper().constructType(type.type) |
| 26 | + val kClass = javaType.rawClass.kotlin |
| 27 | + if (kClass.findAnnotation<JvmInline>() != null) { |
| 28 | + val constructor = kClass.primaryConstructor |
| 29 | + val param = constructor?.parameters?.firstOrNull() |
| 30 | + val unwrappedClass = param?.type?.jvmErasure?.java |
| 31 | + if (unwrappedClass != null) { |
| 32 | + val unwrappedType = AnnotatedType() |
| 33 | + .type(unwrappedClass) |
| 34 | + .ctxAnnotations(type.ctxAnnotations) |
| 35 | + .jsonViewAnnotation(type.jsonViewAnnotation) |
| 36 | + .resolveAsRef(false) |
| 37 | + |
| 38 | + return chain.next().resolve(unwrappedType, context, chain) |
| 39 | + } |
| 40 | + } |
| 41 | + return chain.next().resolve(type, context, chain) |
| 42 | + } |
| 43 | +} |
0 commit comments