21
21
* * * *
22
22
* * *
23
23
* *
24
- *
24
+ *
25
25
*/
26
26
package org .springdoc .core .extractor ;
27
27
50
50
import java .util .concurrent .atomic .AtomicInteger ;
51
51
import java .util .concurrent .atomic .AtomicLong ;
52
52
import java .util .function .Predicate ;
53
- import java .util .stream .Collectors ;
54
53
import java .util .stream .Stream ;
55
54
56
55
import io .swagger .v3 .core .util .PrimitiveType ;
57
56
import io .swagger .v3 .oas .annotations .Parameter ;
58
- import io .swagger .v3 .oas .annotations .media .Schema ;
59
- import org .springdoc .core .service .AbstractRequestService ;
60
57
61
58
import org .springframework .core .GenericTypeResolver ;
62
59
import org .springframework .core .MethodParameter ;
66
63
/**
67
64
* The type Method parameter pojo extractor.
68
65
*
69
- * @author bnasslahsen, michael.clarke
66
+ * @author bnasslahsen
70
67
*/
71
68
public class MethodParameterPojoExtractor {
72
69
@@ -116,21 +113,20 @@ private MethodParameterPojoExtractor() {
116
113
* @return the stream
117
114
*/
118
115
static Stream <MethodParameter > extractFrom (Class <?> clazz ) {
119
- return extractFrom (clazz , "" , true );
116
+ return extractFrom (clazz , "" );
120
117
}
121
118
122
119
/**
123
120
* Extract from stream.
124
121
*
125
122
* @param clazz the clazz
126
123
* @param fieldNamePrefix the field name prefix
127
- * @param parentRequired whether the field that hold the class currently being inspected was required or optional
128
124
* @return the stream
129
125
*/
130
- private static Stream <MethodParameter > extractFrom (Class <?> clazz , String fieldNamePrefix , boolean parentRequired ) {
126
+ private static Stream <MethodParameter > extractFrom (Class <?> clazz , String fieldNamePrefix ) {
131
127
return allFieldsOf (clazz ).stream ()
132
128
.filter (field -> !field .getType ().equals (clazz ))
133
- .flatMap (f -> fromGetterOfField (clazz , f , fieldNamePrefix , parentRequired ))
129
+ .flatMap (f -> fromGetterOfField (clazz , f , fieldNamePrefix ))
134
130
.filter (Objects ::nonNull );
135
131
}
136
132
@@ -140,96 +136,20 @@ private static Stream<MethodParameter> extractFrom(Class<?> clazz, String fieldN
140
136
* @param paramClass the param class
141
137
* @param field the field
142
138
* @param fieldNamePrefix the field name prefix
143
- * @param parentRequired whether the field that holds the class currently being examined was required or optional
144
139
* @return the stream
145
140
*/
146
- private static Stream <MethodParameter > fromGetterOfField (Class <?> paramClass , Field field , String fieldNamePrefix , boolean parentRequired ) {
141
+ private static Stream <MethodParameter > fromGetterOfField (Class <?> paramClass , Field field , String fieldNamePrefix ) {
147
142
Class <?> type = extractType (paramClass , field );
148
143
149
144
if (Objects .isNull (type ))
150
145
return Stream .empty ();
151
146
152
147
if (isSimpleType (type ))
153
- return fromSimpleClass (paramClass , field , fieldNamePrefix , parentRequired );
148
+ return fromSimpleClass (paramClass , field , fieldNamePrefix );
154
149
else {
155
- Parameter parameter = field .getAnnotation (Parameter .class );
156
- Schema schema = field .getAnnotation (Schema .class );
157
- boolean visible = resolveVisible (parameter , schema );
158
- if (!visible ) {
159
- return Stream .empty ();
160
- }
161
- String prefix = fieldNamePrefix + resolveName (parameter , schema ).orElse (field .getName ()) + DOT ;
162
- Set <String > annotationSimpleNames = Arrays .stream (field .getDeclaredAnnotations ())
163
- .map (Annotation ::annotationType )
164
- .map (Class ::getSimpleName )
165
- .collect (Collectors .toSet ());
166
- boolean notNullAnnotationsPresent = AbstractRequestService .hasNotNullAnnotation (annotationSimpleNames );
167
- return extractFrom (type , prefix , resolveRequired (schema , parameter , !notNullAnnotationsPresent ));
168
- }
169
- }
170
-
171
- private static Optional <String > resolveName (Parameter parameter , Schema schema ) {
172
- if (parameter != null ) {
173
- return resolveNameFromParameter (parameter );
174
- }
175
- if (schema != null ) {
176
- return resolveNameFromSchema (schema );
177
- }
178
- return Optional .empty ();
179
- }
180
-
181
- private static Optional <String > resolveNameFromParameter (Parameter parameter ) {
182
- if (parameter .name ().isEmpty ()) {
183
- return Optional .empty ();
184
- }
185
- return Optional .of (parameter .name ());
186
- }
187
-
188
- private static Optional <String > resolveNameFromSchema (Schema schema ) {
189
- if (schema .name ().isEmpty ()) {
190
- return Optional .empty ();
150
+ String prefix = fieldNamePrefix + field .getName () + DOT ;
151
+ return extractFrom (type , prefix );
191
152
}
192
- return Optional .of (schema .name ());
193
- }
194
-
195
- private static boolean resolveVisible (Parameter parameter , Schema schema ) {
196
- if (parameter != null ) {
197
- return !parameter .hidden ();
198
- }
199
- if (schema != null ) {
200
- return !schema .hidden ();
201
- }
202
- return true ;
203
- }
204
-
205
- private static boolean resolveRequired (Schema schema , Parameter parameter , boolean nullable ) {
206
- if (parameter != null ) {
207
- return resolveRequiredFromParameter (parameter , nullable );
208
- }
209
- if (schema != null ) {
210
- return resolveRequiredFromSchema (schema , nullable );
211
- }
212
- return !nullable ;
213
- }
214
-
215
- private static boolean resolveRequiredFromParameter (Parameter parameter , boolean nullable ) {
216
- if (parameter .required ()) {
217
- return true ;
218
- }
219
- return !nullable ;
220
- }
221
-
222
- private static boolean resolveRequiredFromSchema (Schema schema , boolean nullable ) {
223
- if (schema .required ()) {
224
- return true ;
225
- }
226
- else if (schema .requiredMode () == Schema .RequiredMode .REQUIRED ) {
227
- return true ;
228
- }
229
- else if (schema .requiredMode () == Schema .RequiredMode .NOT_REQUIRED ) {
230
- return false ;
231
- }
232
- return !nullable ;
233
153
}
234
154
235
155
/**
@@ -261,32 +181,18 @@ private static Class<?> extractType(Class<?> paramClass, Field field) {
261
181
* @param fieldNamePrefix the field name prefix
262
182
* @return the stream
263
183
*/
264
- private static Stream <MethodParameter > fromSimpleClass (Class <?> paramClass , Field field , String fieldNamePrefix , boolean isParentRequired ) {
184
+ private static Stream <MethodParameter > fromSimpleClass (Class <?> paramClass , Field field , String fieldNamePrefix ) {
265
185
Annotation [] fieldAnnotations = field .getDeclaredAnnotations ();
266
186
try {
267
187
Parameter parameter = field .getAnnotation (Parameter .class );
268
- Schema schema = field .getAnnotation (Schema .class );
269
- boolean visible = resolveVisible (parameter , schema );
270
- if (!visible ) {
271
- return Stream .empty ();
272
- }
273
-
274
- Set <String > annotationSimpleNames = Arrays .stream (field .getDeclaredAnnotations ())
275
- .map (Annotation ::annotationType )
276
- .map (Class ::getSimpleName )
277
- .collect (Collectors .toSet ());
278
-
279
- boolean isNotRequired = !resolveRequired (schema , parameter , !AbstractRequestService .hasNotNullAnnotation (annotationSimpleNames ));
280
- Annotation [] notNullFieldAnnotations = Arrays .stream (fieldAnnotations )
281
- .filter (annotation -> AbstractRequestService .hasNotNullAnnotation (List .of (annotation .annotationType ().getSimpleName ())))
282
- .toArray (Annotation []::new );
188
+ boolean isNotRequired = parameter == null || !parameter .required ();
283
189
if (paramClass .getSuperclass () != null && paramClass .isRecord ()) {
284
190
return Stream .of (paramClass .getRecordComponents ())
285
191
.filter (d -> d .getName ().equals (field .getName ()))
286
192
.map (RecordComponent ::getAccessor )
287
193
.map (method -> new MethodParameter (method , -1 ))
288
194
.map (methodParameter -> DelegatingMethodParameter .changeContainingClass (methodParameter , paramClass ))
289
- .map (param -> new DelegatingMethodParameter (param , fieldNamePrefix + field .getName (), fieldAnnotations , param .getMethodAnnotations (), notNullFieldAnnotations , true , isNotRequired ));
195
+ .map (param -> new DelegatingMethodParameter (param , fieldNamePrefix + field .getName (), fieldAnnotations , param .getMethodAnnotations (), true , isNotRequired ));
290
196
291
197
}
292
198
else
@@ -296,7 +202,7 @@ private static Stream<MethodParameter> fromSimpleClass(Class<?> paramClass, Fiel
296
202
.filter (Objects ::nonNull )
297
203
.map (method -> new MethodParameter (method , -1 ))
298
204
.map (methodParameter -> DelegatingMethodParameter .changeContainingClass (methodParameter , paramClass ))
299
- .map (param -> new DelegatingMethodParameter (param , fieldNamePrefix + field .getName (), fieldAnnotations , param .getMethodAnnotations (), notNullFieldAnnotations , true , isNotRequired ));
205
+ .map (param -> new DelegatingMethodParameter (param , fieldNamePrefix + field .getName (), fieldAnnotations , param .getMethodAnnotations (), true , isNotRequired ));
300
206
}
301
207
catch (IntrospectionException e ) {
302
208
return Stream .of ();
0 commit comments