Skip to content

Commit 0f41f55

Browse files
committed
Exception during WebFlux tests: NoClassDefFoundError: KotlinModule$Builder. Fixes #2114
1 parent 841f1db commit 0f41f55

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/configuration/SpringDocKotlinConfiguration.kt

+32-26
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat
1717
import org.springframework.context.annotation.Bean
1818
import org.springframework.context.annotation.Configuration
1919
import org.springframework.context.annotation.Lazy
20+
import org.springframework.core.KotlinDetector
2021
import org.springframework.core.MethodParameter
2122
import org.springframework.core.annotation.AnnotatedElementUtils
2223
import kotlin.coroutines.Continuation
@@ -35,6 +36,23 @@ import kotlin.reflect.jvm.kotlinFunction
3536
@ConditionalOnWebApplication
3637
@ConditionalOnBean(SpringDocConfiguration::class)
3738
open class SpringDocKotlinConfiguration(objectMapperProvider: ObjectMapperProvider) {
39+
40+
/**
41+
* SpringDoc Kotlin Module Configuration
42+
*
43+
* @param objectMapperProvider Object Mapper Provider
44+
* @return the nullable Kotlin Request Parameter Customizer
45+
*/
46+
@Lazy(false)
47+
@Configuration(proxyBeanMethods = false)
48+
@ConditionalOnClass(KotlinModule::class)
49+
class SpringDocKotlinModuleConfiguration(objectMapperProvider: ObjectMapperProvider) {
50+
init {
51+
objectMapperProvider.jsonMapper()
52+
.registerModule(KotlinModule.Builder().build())
53+
}
54+
}
55+
3856
/**
3957
* Instantiates a new Spring doc kotlin configuration.
4058
*
@@ -74,17 +92,20 @@ open class SpringDocKotlinConfiguration(objectMapperProvider: ObjectMapperProvid
7492
open fun nullableKotlinRequestParameterCustomizer(): ParameterCustomizer {
7593
return ParameterCustomizer { parameterModel, methodParameter ->
7694
if (parameterModel == null) return@ParameterCustomizer null
77-
val kParameter = methodParameter.toKParameter()
78-
if (kParameter != null) {
79-
val parameterDoc = AnnotatedElementUtils.findMergedAnnotation(
80-
AnnotatedElementUtils.forAnnotations(*methodParameter.parameterAnnotations),
81-
Parameter::class.java
82-
)
83-
// Swagger @Parameter annotation takes precedence
84-
if (parameterDoc != null && parameterDoc.required)
85-
parameterModel.required = parameterDoc.required
86-
else
87-
parameterModel.required = kParameter.type.isMarkedNullable == false
95+
if (KotlinDetector.isKotlinReflectPresent()) {
96+
val kParameter = methodParameter.toKParameter()
97+
if (kParameter != null) {
98+
val parameterDoc = AnnotatedElementUtils.findMergedAnnotation(
99+
AnnotatedElementUtils.forAnnotations(*methodParameter.parameterAnnotations),
100+
Parameter::class.java
101+
)
102+
// Swagger @Parameter annotation takes precedence
103+
if (parameterDoc != null && parameterDoc.required)
104+
parameterModel.required = parameterDoc.required
105+
else
106+
parameterModel.required =
107+
kParameter.type.isMarkedNullable == false
108+
}
88109
}
89110
return@ParameterCustomizer parameterModel
90111
}
@@ -99,19 +120,4 @@ open class SpringDocKotlinConfiguration(objectMapperProvider: ObjectMapperProvid
99120
return kotlinFunction.parameters[parameterIndex + 1]
100121
}
101122

102-
/**
103-
* SpringDoc Kotlin Module Configuration
104-
*
105-
* @param objectMapperProvider Object Mapper Provider
106-
* @return the nullable Kotlin Request Parameter Customizer
107-
*/
108-
@Lazy(false)
109-
@Configuration(proxyBeanMethods = false)
110-
@ConditionalOnClass(KotlinModule::class)
111-
class SpringDocKotlinModuleConfiguration(objectMapperProvider: ObjectMapperProvider) {
112-
init {
113-
objectMapperProvider.jsonMapper()
114-
.registerModule(KotlinModule.Builder().build())
115-
}
116-
}
117123
}

0 commit comments

Comments
 (0)