49
49
import io .swagger .v3 .oas .models .media .Content ;
50
50
import io .swagger .v3 .oas .models .media .MediaType ;
51
51
import io .swagger .v3 .oas .models .media .Schema ;
52
+ import io .swagger .v3 .oas .models .media .StringSchema ;
52
53
import io .swagger .v3 .oas .models .parameters .Parameter ;
53
54
import io .swagger .v3 .oas .models .parameters .RequestBody ;
54
55
import org .apache .commons .lang3 .StringUtils ;
55
56
import org .springdoc .core .customizers .OperationCustomizer ;
56
57
import org .springdoc .core .customizers .ParameterCustomizer ;
57
58
58
59
import org .springframework .core .LocalVariableTableParameterNameDiscoverer ;
60
+ import org .springframework .core .MethodParameter ;
59
61
import org .springframework .core .annotation .AnnotatedElementUtils ;
60
62
import org .springframework .core .annotation .AnnotationUtils ;
61
63
import org .springframework .http .HttpMethod ;
@@ -84,7 +86,7 @@ public abstract class AbstractRequestBuilder {
84
86
private static final List <Class > PARAM_TYPES_TO_IGNORE = new ArrayList <>();
85
87
86
88
// using string litterals to support both validation-api v1 and v2
87
- private static final String [] ANNOTATIONS_FOR_REQUIRED = { NotNull .class .getName (), org .springframework .web .bind .annotation .RequestBody .class .getName (),"javax.validation.constraints.NotBlank" , "javax.validation.constraints.NotEmpty" };
89
+ private static final String [] ANNOTATIONS_FOR_REQUIRED = { NotNull .class .getName (), org .springframework .web .bind .annotation .RequestBody .class .getName (), "javax.validation.constraints.NotBlank" , "javax.validation.constraints.NotEmpty" };
88
90
89
91
private static final String POSITIVE_OR_ZERO = "javax.validation.constraints.PositiveOrZero" ;
90
92
@@ -139,13 +141,12 @@ public Operation build(Components components, HandlerMethod handlerMethod, Reque
139
141
// Documentation
140
142
String operationId = operationBuilder .getOperationId (handlerMethod .getMethod ().getName (),
141
143
operation .getOperationId (), openAPI );
142
-
143
144
operation .setOperationId (operationId );
144
145
// requests
145
146
LocalVariableTableParameterNameDiscoverer d = parameterBuilder .getLocalSpringDocParameterNameDiscoverer ();
146
147
String [] pNames = d .getParameterNames (handlerMethod .getMethod ());
147
- java . lang . reflect . Parameter [] parameters = handlerMethod .getMethod (). getParameters ();
148
- String [] reflectionParametersNames = Arrays .stream (parameters ).map (java . lang . reflect . Parameter :: getName ).toArray (String []::new );
148
+ MethodParameter [] parameters = handlerMethod .getMethodParameters ();
149
+ String [] reflectionParametersNames = Arrays .stream (parameters ).map (MethodParameter :: getParameterName ).toArray (String []::new );
149
150
if (pNames == null ) {
150
151
pNames = reflectionParametersNames ;
151
152
}
@@ -158,8 +159,7 @@ public Operation build(Components components, HandlerMethod handlerMethod, Reque
158
159
// check if query param
159
160
Parameter parameter = null ;
160
161
final String pName = pNames [i ] == null ? reflectionParametersNames [i ] : pNames [i ];
161
- io .swagger .v3 .oas .annotations .Parameter parameterDoc = parameterBuilder .getParameterAnnotation (
162
- handlerMethod , parameters [i ], i , io .swagger .v3 .oas .annotations .Parameter .class );
162
+ io .swagger .v3 .oas .annotations .Parameter parameterDoc = parameters [i ].getParameterAnnotation (io .swagger .v3 .oas .annotations .Parameter .class );
163
163
if (parameterDoc == null ) {
164
164
parameterDoc = parametersDocMap .get (pName );
165
165
}
@@ -172,21 +172,21 @@ public Operation build(Components components, HandlerMethod handlerMethod, Reque
172
172
methodAttributes .getJsonViewAnnotation ());
173
173
}
174
174
175
- if (!isParamToIgnore (parameters [i ])) {
175
+ if (!isParamToIgnore (parameters [i ]. getParameter () )) {
176
176
ParameterInfo parameterInfo = new ParameterInfo (pName , parameters [i ], parameter , i );
177
- parameter = buildParams (parameterInfo , components , handlerMethod , requestMethod ,
177
+ parameter = buildParams (parameterInfo , components , requestMethod ,
178
178
methodAttributes .getJsonViewAnnotation ());
179
179
// Merge with the operation parameters
180
180
parameter = parameterBuilder .mergeParameter (operationParameters , parameter );
181
181
if (isValidParameter (parameter )) {
182
- applyBeanValidatorAnnotations (parameter , Arrays .asList (parameters [i ].getAnnotations ()));
182
+ applyBeanValidatorAnnotations (parameter , Arrays .asList (parameters [i ].getParameterAnnotations ()));
183
183
}
184
184
else if (!RequestMethod .GET .equals (requestMethod )) {
185
185
if (operation .getRequestBody () != null )
186
186
requestBodyInfo .setRequestBody (operation .getRequestBody ());
187
- requestBodyBuilder .calculateRequestBodyInfo (components , handlerMethod , methodAttributes , i ,
187
+ requestBodyBuilder .calculateRequestBodyInfo (components , methodAttributes ,
188
188
parameterInfo , requestBodyInfo );
189
- applyBeanValidatorAnnotations (requestBodyInfo .getRequestBody (), Arrays .asList (parameters [i ].getAnnotations ()));
189
+ applyBeanValidatorAnnotations (requestBodyInfo .getRequestBody (), Arrays .asList (parameters [i ].getParameterAnnotations ()));
190
190
}
191
191
customiseParameter (parameter , parameterInfo , handlerMethod );
192
192
}
@@ -220,6 +220,16 @@ private LinkedHashMap<String, Parameter> getParameterLinkedHashMap(Components co
220
220
map .put (entry .getKey (), parameter );
221
221
}
222
222
}
223
+
224
+ for (Map .Entry <String , String > entry : methodAttributes .getHeaders ().entrySet ()) {
225
+ Parameter parameter = new Parameter ().in (ParameterIn .HEADER .toString ()).name (entry .getKey ()).schema (new StringSchema ().addEnumItem (entry .getValue ()));
226
+ if (map .containsKey (entry .getKey ())) {
227
+ parameter = map .get (entry .getKey ());
228
+ parameter .getSchema ().addEnumItemObject (entry .getValue ());
229
+ parameter .setSchema (parameter .getSchema ());
230
+ }
231
+ map .put (entry .getKey (), parameter );
232
+ }
223
233
return map ;
224
234
}
225
235
@@ -252,19 +262,14 @@ private boolean isValidParameter(Parameter parameter) {
252
262
return parameter != null && (parameter .getName () != null || parameter .get$ref () != null );
253
263
}
254
264
255
- private Parameter buildParams (ParameterInfo parameterInfo , Components components , HandlerMethod handlerMethod ,
265
+ private Parameter buildParams (ParameterInfo parameterInfo , Components components ,
256
266
RequestMethod requestMethod , JsonView jsonView ) {
257
- java .lang .reflect .Parameter parameters = parameterInfo .getParameter ();
258
- int index = parameterInfo .getIndex ();
259
-
260
- RequestHeader requestHeader = parameterBuilder .getParameterAnnotation (handlerMethod , parameters , index ,
261
- RequestHeader .class );
262
- RequestParam requestParam = parameterBuilder .getParameterAnnotation (handlerMethod , parameters , index ,
263
- RequestParam .class );
264
- PathVariable pathVar = parameterBuilder .getParameterAnnotation (handlerMethod , parameters , index ,
265
- PathVariable .class );
266
- CookieValue cookieValue = parameterBuilder .getParameterAnnotation (handlerMethod , parameters , index ,
267
- CookieValue .class );
267
+ MethodParameter methodParameter = parameterInfo .getMethodParameter ();
268
+
269
+ RequestHeader requestHeader = methodParameter .getParameterAnnotation (RequestHeader .class );
270
+ RequestParam requestParam = methodParameter .getParameterAnnotation (RequestParam .class );
271
+ PathVariable pathVar = methodParameter .getParameterAnnotation (PathVariable .class );
272
+ CookieValue cookieValue = methodParameter .getParameterAnnotation (CookieValue .class );
268
273
269
274
Parameter parameter = null ;
270
275
RequestInfo requestInfo ;
@@ -276,7 +281,7 @@ private Parameter buildParams(ParameterInfo parameterInfo, Components components
276
281
277
282
}
278
283
else if (requestParam != null && !parameterBuilder .isFile (parameterInfo .getParameter ())) {
279
- boolean isOptional = Optional .class .equals (parameters . getType ());
284
+ boolean isOptional = Optional .class .equals (methodParameter . getParameterType ());
280
285
requestInfo = new RequestInfo (ParameterIn .QUERY .toString (), requestParam .value (), requestParam .required () && !isOptional ,
281
286
requestParam .defaultValue ());
282
287
parameter = buildParam (parameterInfo , components , requestInfo , jsonView );
0 commit comments