26
26
27
27
package org.springdoc.core.configuration
28
28
29
- import io.swagger.v3.oas.annotations.Parameter
30
29
import org.springdoc.core.converters.KotlinInlineClassUnwrappingConverter
30
+ import org.springdoc.core.customizers.DelegatingMethodParameterCustomizer
31
31
import org.springdoc.core.customizers.KotlinDeprecatedPropertyCustomizer
32
- import org.springdoc.core.customizers.ParameterCustomizer
32
+ import org.springdoc.core.extractor.DelegatingMethodParameter
33
33
import org.springdoc.core.providers.ObjectMapperProvider
34
34
import org.springdoc.core.utils.Constants
35
- import org.springdoc.core.utils.SchemaUtils
36
35
import org.springdoc.core.utils.SpringDocUtils
37
36
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean
38
37
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
@@ -43,14 +42,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat
43
42
import org.springframework.context.annotation.Bean
44
43
import org.springframework.context.annotation.Configuration
45
44
import org.springframework.context.annotation.Lazy
46
- import org.springframework.core.KotlinDetector
47
- import org.springframework.core.MethodParameter
48
- import org.springframework.core.annotation.AnnotatedElementUtils
49
- import org.springframework.web.bind.annotation.RequestParam
50
- import org.springframework.web.bind.annotation.ValueConstants
51
45
import kotlin.coroutines.Continuation
52
- import kotlin.reflect.KParameter
53
- import kotlin.reflect.jvm.kotlinFunction
46
+ import kotlin.reflect.full.primaryConstructor
54
47
55
48
/* *
56
49
* The type Spring doc kotlin configuration.
@@ -76,60 +69,21 @@ class SpringDocKotlinConfiguration() {
76
69
.addDeprecatedType(Deprecated ::class .java)
77
70
}
78
71
79
- /* *
80
- * Kotlin springdoc-openapi ParameterCustomizer.
81
- * deprecated as not anymore required, use [SchemaUtils.fieldNullable]
82
- *
83
- * @return the nullable Kotlin Request Parameter Customizer
84
- * @see SchemaUtils.fieldNullable()
85
- */
86
- @Deprecated(" Deprecated since 2.8.7" , level = DeprecationLevel .ERROR )
87
- fun nullableKotlinRequestParameterCustomizer (): ParameterCustomizer {
88
- return ParameterCustomizer { parameterModel, methodParameter ->
89
- if (parameterModel == null ) return @ParameterCustomizer null
90
- if (KotlinDetector .isKotlinReflectPresent() && KotlinDetector .isKotlinType(
91
- methodParameter.parameterType
92
- )
93
- ) {
94
- val kParameter = methodParameter.toKParameter()
95
- if (kParameter != null ) {
96
- val parameterDoc = AnnotatedElementUtils .findMergedAnnotation(
97
- AnnotatedElementUtils .forAnnotations(* methodParameter.parameterAnnotations),
98
- Parameter ::class .java
99
- )
100
- val requestParam = AnnotatedElementUtils .findMergedAnnotation(
101
- AnnotatedElementUtils .forAnnotations(* methodParameter.parameterAnnotations),
102
- RequestParam ::class .java
103
- )
104
- // Swagger @Parameter annotation takes precedence
105
- if (parameterDoc != null && parameterDoc.required)
106
- parameterModel.required = parameterDoc.required
107
- // parameter is not required if a default value is provided in @RequestParam
108
- else if (requestParam != null && requestParam.defaultValue != ValueConstants .DEFAULT_NONE )
109
- parameterModel.required = false
110
- else {
111
- val isJavaNullableAnnotationPresent =
112
- methodParameter.parameterAnnotations.any {
113
- it.annotationClass.qualifiedName == " jakarta.annotation.Nullable"
114
- }
115
- parameterModel.required =
116
- kParameter.type.isMarkedNullable == false && ! isJavaNullableAnnotationPresent
117
- }
118
- }
119
- }
120
- return @ParameterCustomizer parameterModel
72
+ @Bean
73
+ @ConditionalOnProperty(
74
+ name = [Constants .SPRINGDOC_NULLABLE_REQUEST_PARAMETER_ENABLED ],
75
+ matchIfMissing = true
76
+ )
77
+ @Lazy(false )
78
+ fun kotlinDefaultsInParamObjects (): DelegatingMethodParameterCustomizer =
79
+ DelegatingMethodParameterCustomizer { _, mp ->
80
+ val kProp = mp.containingClass.kotlin.primaryConstructor
81
+ ?.parameters
82
+ ?.firstOrNull { it.name == mp.parameterName }
83
+ if (kProp?.isOptional == true )
84
+ (mp as DelegatingMethodParameter ).isNotRequired = true
121
85
}
122
- }
123
-
124
- private fun MethodParameter.toKParameter (): KParameter ? {
125
- // ignore return type, see org.springframework.core.MethodParameter.getParameterIndex
126
- if (parameterIndex == - 1 ) return null
127
- val kotlinFunction = method?.kotlinFunction ? : return null
128
- // The first parameter of the kotlin function is the "this" reference and not needed here.
129
- // See also kotlin.reflect.KCallable.getParameters
130
- return kotlinFunction.parameters[parameterIndex + 1 ]
131
- }
132
-
86
+
133
87
@ConditionalOnClass(name = [" kotlin.reflect.full.KClasses" ])
134
88
class KotlinReflectDependingConfiguration {
135
89
0 commit comments