|
| 1 | +package org.springdoc.core.configuration |
| 2 | + |
| 3 | +import com.fasterxml.jackson.module.kotlin.KotlinModule |
| 4 | +import io.swagger.v3.oas.models.media.ByteArraySchema |
| 5 | +import org.springdoc.core.customizers.ParameterCustomizer |
| 6 | +import org.springdoc.core.parsers.KotlinCoroutinesReturnTypeParser |
| 7 | +import org.springdoc.core.providers.ObjectMapperProvider |
| 8 | +import org.springdoc.core.utils.Constants |
| 9 | +import org.springdoc.core.utils.SpringDocUtils |
| 10 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean |
| 11 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean |
| 12 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty |
| 13 | +import org.springframework.context.annotation.Bean |
| 14 | +import org.springframework.context.annotation.Configuration |
| 15 | +import org.springframework.context.annotation.Lazy |
| 16 | +import org.springframework.core.MethodParameter |
| 17 | +import kotlin.coroutines.Continuation |
| 18 | +import kotlin.reflect.KParameter |
| 19 | +import kotlin.reflect.jvm.kotlinFunction |
| 20 | + |
| 21 | +/** |
| 22 | + * The type Spring doc kotlin configuration. |
| 23 | + * @author bnasslahsen |
| 24 | + */ |
| 25 | +@Lazy(false) |
| 26 | +@Configuration(proxyBeanMethods = false) |
| 27 | +@ConditionalOnProperty(name = [Constants.SPRINGDOC_ENABLED], matchIfMissing = true) |
| 28 | +@ConditionalOnBean( |
| 29 | + SpringDocConfiguration::class |
| 30 | +) |
| 31 | +open class SpringDocKotlinConfiguration(objectMapperProvider: ObjectMapperProvider) { |
| 32 | + /** |
| 33 | + * Instantiates a new Spring doc kotlin configuration. |
| 34 | + * |
| 35 | + */ |
| 36 | + init { |
| 37 | + SpringDocUtils.getConfig() |
| 38 | + .addRequestWrapperToIgnore(Continuation::class.java) |
| 39 | + .replaceWithSchema(ByteArray::class.java, ByteArraySchema()) |
| 40 | + .addDeprecatedType(Deprecated::class.java) |
| 41 | + objectMapperProvider.jsonMapper().registerModule(KotlinModule.Builder().build()) |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Kotlin coroutines return type parser kotlin coroutines return type parser. |
| 46 | + * |
| 47 | + * @return the kotlin coroutines return type parser |
| 48 | + */ |
| 49 | + @Bean |
| 50 | + @Lazy(false) |
| 51 | + @ConditionalOnMissingBean |
| 52 | + open fun kotlinCoroutinesReturnTypeParser(): KotlinCoroutinesReturnTypeParser { |
| 53 | + return KotlinCoroutinesReturnTypeParser() |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Kotlin springdoc-openapi ParameterCustomizer |
| 58 | + * |
| 59 | + * @return the nullable Kotlin Request Parameter Customizer |
| 60 | + */ |
| 61 | + @Bean |
| 62 | + @Lazy(false) |
| 63 | + @ConditionalOnMissingBean |
| 64 | + open fun nullableKotlinRequestParameterCustomizer(): ParameterCustomizer { |
| 65 | + return ParameterCustomizer { parameterModel, methodParameter -> |
| 66 | + if (parameterModel == null) return@ParameterCustomizer null |
| 67 | + val kParameter = methodParameter.toKParameter() |
| 68 | + if (kParameter != null) { |
| 69 | + parameterModel.required = kParameter.type.isMarkedNullable == false |
| 70 | + } |
| 71 | + return@ParameterCustomizer parameterModel |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private fun MethodParameter.toKParameter(): KParameter? { |
| 76 | + // ignore return type, see org.springframework.core.MethodParameter.getParameterIndex |
| 77 | + if (parameterIndex == -1) return null |
| 78 | + val kotlinFunction = method?.kotlinFunction ?: return null |
| 79 | + // The first parameter of the kotlin function is the "this" reference and not needed here. |
| 80 | + // See also kotlin.reflect.KCallable.getParameters |
| 81 | + return kotlinFunction.parameters[parameterIndex + 1] |
| 82 | + } |
| 83 | +} |
0 commit comments